java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java图片识别

基于java实现简单的图片类别识别

作者:默默努力的小老弟

这篇文章主要为大家详细介绍了如何基于java实现简单的图片类别识别功能,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下

下面是java实现简单的图片类别识别的示例代码,希望对大家有所帮助

1.maven依赖

  <dependency>
            <groupId>ai.djl</groupId>
            <artifactId>api</artifactId>
            <version>0.4.0</version>
        </dependency>
        <dependency>
            <groupId>ai.djl</groupId>
            <artifactId>repository</artifactId>
            <version>0.4.0</version>
        </dependency>
        <dependency>
            <groupId>ai.djl.pytorch</groupId>
            <artifactId>pytorch-model-zoo</artifactId>
            <version>0.4.0</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>ai.djl.pytorch</groupId>
            <artifactId>pytorch-native-auto</artifactId>
            <version>1.4.0</version>
            <scope>runtime</scope>
        </dependency>

2.完整代码

import ai.djl.Application;
import ai.djl.ModelException;
import ai.djl.inference.Predictor;
import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.modality.cv.util.BufferedImageUtils;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ModelZoo;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.training.util.ProgressBar;
import ai.djl.translate.TranslateException;
import org.opencv.core.*;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.videoio.VideoCapture;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException, ModelException, TranslateException {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        String url = "d:/path/to/u=2304912836,1229708753&fm=193.jpg";

        VideoCapture camera = new VideoCapture(0); // 0 表示第一个摄像头设备,如果有多个摄像头可能需要调整参数
        if (!camera.isOpened()) {
            System.out.println("Error: Could not open camera");
            return;
        }
        // 创建窗口并设置关闭操作
        JFrame frame = new JFrame("Face Grid Live");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setResizable(false);

        // 创建用于显示图像的标签
        JLabel label = new JLabel();
        frame.getContentPane().add(label, BorderLayout.CENTER);
        Mat frameImage = new Mat();
        while(true){
            camera.read(frameImage);
            BufferedImage bufferedImage = matToBufferedImage(frameImage);



            Criteria<BufferedImage, DetectedObjects> criteria =
                    Criteria.builder()
                            .optApplication(Application.CV.OBJECT_DETECTION)
                            .setTypes(BufferedImage.class, DetectedObjects.class)
                            .optFilter("backbone", "resnet50")
                            .optProgress(new ProgressBar())
                            .build();

            try (ZooModel<BufferedImage, DetectedObjects> model = ModelZoo.loadModel(criteria)) {
                try (Predictor<BufferedImage, DetectedObjects> predictor = model.newPredictor()) {
                    DetectedObjects detection = predictor.predict(bufferedImage);
                    System.out.println(detection);
                }
            }
            // 将图像显示在标签中
            ImageIcon imageIcon = new ImageIcon(bufferedImage);
            label.setIcon(imageIcon);

            // 刷新窗口
            frame.pack();
            frame.setVisible(true);
        }

    }
    public static BufferedImage matToBufferedImage(Mat matrix) {
        int width = matrix.cols();
        int height = matrix.rows();
        int channels = matrix.channels();
        byte[] data = new byte[width * height * channels];
        matrix.get(0, 0, data);

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        System.arraycopy(data, 0, targetPixels, 0, data.length);

        return image;
    }
}

到此这篇关于基于java实现简单的图片类别识别的文章就介绍到这了,更多相关java图片识别内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文