基于java實(shí)現(xiàn)簡(jiǎn)單的圖片類(lèi)別識(shí)別
下面是java實(shí)現(xiàn)簡(jiǎn)單的圖片類(lèi)別識(shí)別的示例代碼,希望對(duì)大家有所幫助
1.maven依賴(lài)
<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 表示第一個(gè)攝像頭設(shè)備,如果有多個(gè)攝像頭可能需要調(diào)整參數(shù)
if (!camera.isOpened()) {
System.out.println("Error: Could not open camera");
return;
}
// 創(chuàng)建窗口并設(shè)置關(guān)閉操作
JFrame frame = new JFrame("Face Grid Live");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setResizable(false);
// 創(chuàng)建用于顯示圖像的標(biāo)簽
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);
}
}
// 將圖像顯示在標(biāo)簽中
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;
}
}
到此這篇關(guān)于基于java實(shí)現(xiàn)簡(jiǎn)單的圖片類(lèi)別識(shí)別的文章就介紹到這了,更多相關(guān)java圖片識(shí)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud中的Ribbon負(fù)載均衡器詳細(xì)解析
這篇文章主要介紹了SpringCloud中的Ribbon負(fù)載均衡器詳細(xì)解析,Ribbon 是一個(gè)基于 HTTP 和 TCP 的客戶(hù)端負(fù)載均衡工具,它基于 Netflix Ribbon 實(shí)現(xiàn),通過(guò)封裝可以讓我們輕松地將面向服務(wù)的 REST 模版請(qǐng)求自動(dòng)轉(zhuǎn)換成客戶(hù)端負(fù)載均衡的服務(wù)調(diào)用,需要的朋友可以參考下2024-01-01
SpringBoot集成Session的實(shí)現(xiàn)示例
Session是一個(gè)在Web開(kāi)發(fā)中常用的概念,它表示服務(wù)器和客戶(hù)端之間的一種狀態(tài)管理機(jī)制,用于跟蹤用戶(hù)在網(wǎng)站或應(yīng)用程序中的狀態(tài)和數(shù)據(jù),本文主要介紹了SpringBoot集成Session的實(shí)現(xiàn)示例,感興趣的可以了解一下2023-09-09
JAVA如何使用Math類(lèi)操作數(shù)據(jù)
這篇文章主要介紹了JAVA如何使用Math類(lèi)操作數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
總結(jié)一下關(guān)于在Java8中使用stream流踩過(guò)的一些坑
java8新增了stream流式處理,對(duì)于list的各種操作處理提供了好多方法 ,用過(guò)的都知道,方便極了.比如篩選、排序、合并、類(lèi)型轉(zhuǎn)換等等.以下是我實(shí)際工作中踩過(guò)的坑,記錄下避免大家踩坑,需要的朋友可以參考下2021-06-06
javafx 如何將項(xiàng)目打包為 Windows 的可執(zhí)行文件exe
文章介紹了三種將JavaFX項(xiàng)目打包為.exe文件的方法:方法1使用jpackage(適用于JDK14及以上版本),方法2使用Launch4j(適用于所有JDK版本),方法3使用InnoSetup(用于創(chuàng)建安裝包),每種方法都有其特點(diǎn)和適用范圍,可以根據(jù)項(xiàng)目需求選擇合適的方法,感興趣的朋友一起看看吧2025-01-01
java實(shí)現(xiàn)微信小程序登錄態(tài)維護(hù)的示例代碼
本篇文章主要介紹了java實(shí)現(xiàn)微信小程序登錄態(tài)維護(hù)的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09
idea pom導(dǎo)入net.sf.json的jar包失敗的解決方案
JSON(JavaScript Object Notation,JS對(duì)象簡(jiǎn)譜)是一種輕量級(jí)的數(shù)據(jù)交換格式,這篇文章主要介紹了idea pom導(dǎo)入net.sf.json的jar包失敗的解決方案,感興趣的朋友一起看看吧2023-11-11

