Face Detection Example
今天喵把課題提升到 FaceDetection的 Level感覺FaceDetection是個非常高深的題目
不過, 使用了openCV所提供的 library, 喵一下子就做到了臉部偵測~
主程式為
int main(int argc, char** argv) { // Load preconstructed classifier face_cascade.load("data/haarcascade_frontalface_alt.xml"); Mat inputImage = imread(argv[1]); detectFaces(inputImage); waitKey(0); return 0; }
其中是需要先把 haar的參數下載回來放到指定的路徑下
haar的參數可以在這邊找到
haarcascade_frontalface_alt.xml
呼叫detectFace的function為
void detectFaces(Mat frame) { std::vector<Rect> faces; Mat frame_gray; cvtColor(frame, frame_gray, COLOR_BGR2GRAY); // Convert to gray scale equalizeHist(frame_gray, frame_gray); // Equalize histogram // Detect faces face_cascade.detectMultiScale(frame_gray, faces, 1.1, 3, 0|CASCADE_SCALE_IMAGE, Size(30, 30)); // Iterate over all of the faces for( size_t i = 0; i < faces.size(); i++ ) { // Find center of faces Point center(faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2); // Draw ellipse around face ellipse(frame, center, Size(faces[i].width/2, faces[i].height/2), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 ); } imshow(window_name, frame); // Display frame }
其中argument放入的參數為 lena.png
效果如下
在PC上使用會發覺很快地就可以辨識出人臉
openCV在人臉辨識上是很強大的, 主要的賣點在於可以在PC上面達到 realtime的解析
如果搭配上之前所講述的 camera capture, 馬上就可以realtime的看到人臉辨識效果
這邊也提供相對應的 code
int main(int argc, char** argv) { // Load preconstructed classifier face_cascade.load("data/haarcascade_frontalface_alt.xml"); VideoCapture cap(0); // Open default camera Mat frame; cap.read(frame); printf("frame width %d, height %d\n", frame.cols, frame.rows); while(cap.read(frame)) { detectFaces(frame); // Call function to detect faces if( waitKey(30) >= 0) // Pause key break; } return 0; }
從這邊開始... 才算是做完暖身操, 準備進入主題
Haar-like Adaboost
臉部偵測, 一切緣由, 要從這一篇文章開始說起
2001年的時候, Paul Viola and Michael Jones發表了一個文章, 最主要的亮點是在把人臉偵測的運算時間縮短到可以在當時的PC上面做到 384x288@15 FPS realtime.
這在當時的電腦, 算是一個非常驚人的表現
那主要是怎麼做到的呢?
各位客官先看看 pdf
喵就將這內容留到 FaceDetection - (二) 深入研究
這在當時的電腦, 算是一個非常驚人的表現
那主要是怎麼做到的呢?
各位客官先看看 pdf
喵就將這內容留到 FaceDetection - (二) 深入研究
沒有留言:
張貼留言