基于java實現(xiàn)簡單的圖片類別識別
更新時間:2023年12月01日 11:11:51 作者:默默努力的小老弟
這篇文章主要為大家詳細介紹了如何基于java實現(xiàn)簡單的圖片類別識別功能,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學習一下
下面是java實現(xiàn)簡單的圖片類別識別的示例代碼,希望對大家有所幫助
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 表示第一個攝像頭設(shè)備,如果有多個攝像頭可能需要調(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)建用于顯示圖像的標簽 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; } }
到此這篇關(guān)于基于java實現(xiàn)簡單的圖片類別識別的文章就介紹到這了,更多相關(guān)java圖片識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot集成Session的實現(xiàn)示例
Session是一個在Web開發(fā)中常用的概念,它表示服務器和客戶端之間的一種狀態(tài)管理機制,用于跟蹤用戶在網(wǎng)站或應用程序中的狀態(tài)和數(shù)據(jù),本文主要介紹了SpringBoot集成Session的實現(xiàn)示例,感興趣的可以了解一下2023-09-09總結(jié)一下關(guān)于在Java8中使用stream流踩過的一些坑
java8新增了stream流式處理,對于list的各種操作處理提供了好多方法 ,用過的都知道,方便極了.比如篩選、排序、合并、類型轉(zhuǎn)換等等.以下是我實際工作中踩過的坑,記錄下避免大家踩坑,需要的朋友可以參考下2021-06-06javafx 如何將項目打包為 Windows 的可執(zhí)行文件exe
文章介紹了三種將JavaFX項目打包為.exe文件的方法:方法1使用jpackage(適用于JDK14及以上版本),方法2使用Launch4j(適用于所有JDK版本),方法3使用InnoSetup(用于創(chuàng)建安裝包),每種方法都有其特點和適用范圍,可以根據(jù)項目需求選擇合適的方法,感興趣的朋友一起看看吧2025-01-01java實現(xiàn)微信小程序登錄態(tài)維護的示例代碼
本篇文章主要介紹了java實現(xiàn)微信小程序登錄態(tài)維護的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-09-09idea pom導入net.sf.json的jar包失敗的解決方案
JSON(JavaScript Object Notation,JS對象簡譜)是一種輕量級的數(shù)據(jù)交換格式,這篇文章主要介紹了idea pom導入net.sf.json的jar包失敗的解決方案,感興趣的朋友一起看看吧2023-11-11