日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > pytorch >内容正文

pytorch

基于摄像头使用Cascade Classifier做人脸检测的方法及例程

發布時間:2025/7/25 pytorch 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于摄像头使用Cascade Classifier做人脸检测的方法及例程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考文檔:http://docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html

目標:

使用CascadeClassifier(Opencv中的級聯分類器)類在視頻流中進行Object(例如,人臉)檢測。使用的函數如下:

1) load  加載一個 .xml 分類器文件. 可以是Haar分類器或者LBP分類器

2) detectMultiScale 執行檢測功能


代碼:

代碼下載地址 here .? LBP 版本的人臉檢測下載地址 here

#include "opencv2/objdetect/objdetect.hpp"#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/imgproc.hpp"#include <iostream>#include <stdio.h>using namespace std;using namespace cv;/** Function Headers */void detectAndDisplay( Mat frame );/** Global variables */String face_cascade_name = "haarcascade_frontalface_alt.xml";String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";CascadeClassifier face_cascade;CascadeClassifier eyes_cascade;string window_name = "Capture - Face detection";RNG rng(12345);/** @function main */int main( int argc, const char** argv ){CvCapture* capture;Mat frame;//-- 1. Load the cascadesif( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };//-- 2. Read the video streamcapture = cvCaptureFromCAM( -1 );if( capture ){while( true ){frame = cvQueryFrame( capture );//-- 3. Apply the classifier to the frameif( !frame.empty() ){ detectAndDisplay( frame ); }else{ printf(" --(!) No captured frame -- Break!"); break; }int c = waitKey(10);if( (char)c == 'c' ) { break; }}}return 0;}/** @function detectAndDisplay */ void detectAndDisplay( Mat frame ) {std::vector<Rect> faces;Mat frame_gray;cvtColor( frame, frame_gray, CV_BGR2GRAY );equalizeHist( frame_gray, frame_gray );//-- Detect facesface_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );for( size_t i = 0; i < faces.size(); i++ ){Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );Mat faceROI = frame_gray( faces[i] );std::vector<Rect> eyes;//-- In each face, detect eyeseyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );for( size_t j = 0; j < eyes.size(); j++ ){Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );}}//-- Show what you gotimshow( window_name, frame );}


其他參考代碼:
/** Global variables */ String face_cascade_name = "lbpcascade_frontalface.xml";//導入級聯分類器xml文件,并作文件是否存在的判斷 if( !face_cascade.load( face_cascade_name ) ) { printf("--(!)Error loading\n");return -1; }//-- 2. Read the video streamcapture.open( 0 );//參數為“0”,表示打開默認的攝像機if( capture.isOpened() ){for(;;){//代碼段略}}** ----------------- 主要代碼 ---------- **//轉換成灰度圖cvtColor( frame, frame_gray, COLOR_BGR2GRAY );//轉換直方圖equalizeHist( frame_gray, frame_gray );//-- Detect facesface_cascade.detectMultiScale( frame_gray, faces,6,Size(66, 66),Size(300, 300) );** ----------------- 主要函數結構 ----------- ** **CascadeClassifier::detectMultiScale** C++下的原形: void CascadeClassifier::detectMultiScale( const Mat& image, vector<Rect>& objects, double scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size())參數: Image – 原圖像; Object – 存放檢測結果的目標矩陣; ScaleFactor – 圖像衰減比例系數; minNeighbors – 保留最小目標鄰近的長方形; flags – 僅僅用于舊版本的cascade,新版本未用到。 MinSize - 最小的目標尺寸,小于這個尺寸將不會檢測。 MaxSize – 最大的目標尺寸,大于這個尺寸將不會被檢測。**VideoCapture::open** C++:有兩個原行:bool VideoCapture::open(const string& filename)bool VideoCapture::open(int device) 參數: filename - - 打開視頻或圖片文件的名稱,例如video.avi 、img_001.jpg device - - 設備名稱,如:a camera index。Device=0,表示打開默認攝像設備。**VideoCapture::isOpened** isopened() - - 調用時返回一個已經初始化好的的設備,正確返回1 ,錯誤返回非零。 **VideoCapture::release** release() 關閉并釋放設備**Ellipse** C++: 原型1:void ellipse( Mat& img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar& color, int thickness=1, int lineType=8, int shift=0)C++: 原型2: void ellipse( Mat& img, const RotatedRect& box, const Scalar& color, int thickness=1, int line-Type=8)Img - - 圖像 Center – 中心點 Axes - - 半徑 Angle - - 中心角度 startAngle - - 起始角度 endAngle - - 終點角度 color - - 顏色 thickness - - 厚度 linetype - - 線型


下載地址


總結

以上是生活随笔為你收集整理的基于摄像头使用Cascade Classifier做人脸检测的方法及例程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。