這篇文章主要講解了“怎么用Java+OpenCV實(shí)現(xiàn)人臉檢測(cè)并自動(dòng)拍照”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“怎么用Java+OpenCV實(shí)現(xiàn)人臉檢測(cè)并自動(dòng)拍照”吧!
創(chuàng)新互聯(lián)從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元海曙做網(wǎng)站,已為上家服務(wù),為海曙各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
java+opencv實(shí)現(xiàn)人臉檢測(cè),調(diào)用筆記本攝像頭實(shí)時(shí)抓拍,人臉會(huì)用紅色邊框標(biāo)識(shí)出來(lái),并且將抓拍的目錄存放在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ù)用來(lái)判斷攝像頭調(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;irectCrop.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è)并自動(dòng)拍照”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)怎么用Java+OpenCV實(shí)現(xiàn)人臉檢測(cè)并自動(dòng)拍照這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!