您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關如何在Java中使用OpenCV實現人臉檢測,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
CameraBasic.java
package com.njupt.zhb.test; import java.awt.EventQueue; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import org.opencv.core.*; import org.opencv.highgui.Highgui; import org.opencv.highgui.VideoCapture; import org.opencv.imgproc.Imgproc; import org.opencv.objdetect.CascadeClassifier; /** * 動態人臉檢測并裁剪 * @author hyj * */ public class CameraBasic { static { System.out.println(System.getProperty("java.library.path")); System.loadLibrary(Core.NATIVE_LIBRARY_NAME); } private JFrame frame; private static JLabel label; private static int flag = 0; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { CameraBasic window = new CameraBasic(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); VideoCapture camera = new VideoCapture();//創建Opencv中的視頻捕捉對象 camera.open(0);//open函數中的0代表當前計算機中索引為0的攝像頭,如果你的計算機有多個攝像頭,那么一次1,2,3…… if (!camera.isOpened()) {//isOpened函數用來判斷攝像頭調用是否成功 System.out.println("Camera Error");//如果攝像頭調用失敗,輸出錯誤信息 } else { Mat frame = new Mat();//創建一個輸出幀 while (flag == 0) { camera.read(frame);//read方法讀取攝像頭的當前幀 // CascadeClassifier faceDetector = new CascadeClassifier("src/com/njupt/zhb/test/lbpcascade_frontalface.xml"); CascadeClassifier faceDetector = new CascadeClassifier("src/com/njupt/zhb/test/haarcascade_frontalface_alt.xml"); MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(frame, faceDetections); Rect [] rectArray = faceDetections.toArray(); if (rectArray.length > 0) { for (int i=0;i<rectArray.length;i++) { Rect rect = rectArray[i]; Rect rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height); if (rect.width + rect.height > rectCrop.height + rectCrop.width) { rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height); } System.out.println(String.format("檢測到 %s 個人臉! ", rectArray.length)); Mat imageRoi = new Mat(frame, rectCrop); String name = System.currentTimeMillis()+".png"; Highgui.imwrite(name, imageRoi); Core.rectangle(frame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255), 2); } } //轉換圖像格式并輸出 label.setIcon(new ImageIcon(mat2BufferedImage.matToBufferedImage(frame))); try { Thread.sleep(500);//線程暫停500ms } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // if (faceCount > 0) { // faceSerialCount++; // System.out.println(faceSerialCount); // } else { // faceSerialCount = 0; // } // // if (faceSerialCount > 6) { // Mat imageRoi = new Mat(frame, rectCrop); // Highgui.imwrite("haha.png", imageRoi); // faceSerialCount = 0; // } } } } private CameraBasic() { initialize(); } private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 1000, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); label = new JLabel(""); label.setBounds(0, 0, 1000, 500); frame.getContentPane().add(label); } }
看完上述內容,你們對如何在Java中使用OpenCV實現人臉檢測有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。