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;
}
}
文章评论