Java+OpenCV實(shí)現(xiàn)人臉檢測(cè)并自動(dòng)拍照
java+opencv實(shí)現(xiàn)人臉檢測(cè),調(diào)用筆記本攝像頭實(shí)時(shí)抓拍,人臉會(huì)用紅色邊框標(biāo)識(shí)出來,并且將抓拍的目錄存放在src下,圖片名稱是時(shí)間戳。
環(huán)境配置:win7 64位,jdk1.8
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; /** * 動(dòng)態(tài)人臉檢測(cè)并裁剪 * @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();//創(chuàng)建Opencv中的視頻捕捉對(duì)象 camera.open(0);//open函數(shù)中的0代表當(dāng)前計(jì)算機(jī)中索引為0的攝像頭,如果你的計(jì)算機(jī)有多個(gè)攝像頭,那么一次1,2,3…… if (!camera.isOpened()) {//isOpened函數(shù)用來判斷攝像頭調(diào)用是否成功 System.out.println("Camera Error");//如果攝像頭調(diào)用失敗,輸出錯(cuò)誤信息 } else { Mat frame = new Mat();//創(chuàng)建一個(gè)輸出幀 while (flag == 0) { camera.read(frame);//read方法讀取攝像頭的當(dāng)前幀 // 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("檢測(cè)到 %s 個(gè)人臉! ", 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); } } //轉(zhuǎn)換圖像格式并輸出 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實(shí)現(xiàn)人臉檢測(cè)并拍照
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用spring動(dòng)態(tài)獲取接口的不同實(shí)現(xiàn)類
這篇文章主要介紹了使用spring動(dòng)態(tài)獲取接口的不同實(shí)現(xiàn)類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Springboot使用jxls實(shí)現(xiàn)excel模板導(dǎo)出excel方式
這篇文章主要介紹了Springboot使用jxls實(shí)現(xiàn)excel模板導(dǎo)出excel方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08手動(dòng)添加jar包進(jìn)Maven本地庫(kù)內(nèi)的方法
這篇文章主要介紹了手動(dòng)添加jar包進(jìn)Maven本地庫(kù)內(nèi)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08java、js中實(shí)現(xiàn)無限層級(jí)的樹形結(jié)構(gòu)方法(類似遞歸)
下面小編就為大家?guī)硪黄猨ava、js中實(shí)現(xiàn)無限層級(jí)的樹形結(jié)構(gòu)方法(類似遞歸)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11Jmeter參數(shù)化實(shí)現(xiàn)方法及應(yīng)用實(shí)例
這篇文章主要介紹了Jmeter參數(shù)化實(shí)現(xiàn)方法及應(yīng)用實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08JavaWeb禁止瀏覽器緩存當(dāng)前Web頁(yè)面的方法
所謂瀏覽器緩存,是指當(dāng)?shù)谝淮卧L問網(wǎng)頁(yè)時(shí),瀏覽器會(huì)將這些網(wǎng)頁(yè)緩存到本地,當(dāng)下一次再訪問這些被緩存的網(wǎng)頁(yè)時(shí),瀏覽器就會(huì)直接從本地讀取這些網(wǎng)頁(yè)的內(nèi)容,而無需再?gòu)木W(wǎng)絡(luò)上獲取2017-11-11