该程序的目标是实时检测感兴趣的物体(人脸),并保持对同一物体的跟踪。这是一个如何在Python中检测人脸的简单示例。您可以尝试使用您选择的任何其他对象的训练样本,通过在所需对象上训练分类器来检测。
null
以下是下载以下要求的步骤。
步骤:
- 下载Python 2.7。x版本、numpy和Opencv 2.7。x版。检查Windows 32位或64位是否兼容,并相应安装。
- 确保numpy在python中运行,然后尝试安装opencv。
- 把哈卡斯卡德的眼睛放进去。xml&haarcascade_frontalface_默认值。同一文件夹中的xml文件(以下代码中给出了链接)。
实施
# OpenCV program to detect face in real time # import libraries of python OpenCV # where its functionality resides import cv2 # load the required trained XML classifiers # data/haarcascades/haarcascade_frontalface_default.xml # Trained XML classifiers describes some features of some # object we want to detect a cascade function is trained # from a lot of positive(faces) and negative(non-faces) # images. face_cascade = cv2.CascadeClassifier( 'haarcascade_frontalface_default.xml' ) # /data/haarcascades/haarcascade_eye.xml # Trained XML file for detecting eyes eye_cascade = cv2.CascadeClassifier( 'haarcascade_eye.xml' ) # capture frames from a camera cap = cv2.VideoCapture( 0 ) # loop runs if capturing has been initialized. while 1 : # reads frames from a camera ret, img = cap.read() # convert to gray scale of each frames gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Detects faces of different sizes in the input image faces = face_cascade.detectMultiScale(gray, 1.3 , 5 ) for (x,y,w,h) in faces: # To draw a rectangle in a face cv2.rectangle(img,(x,y),(x + w,y + h),( 255 , 255 , 0 ), 2 ) roi_gray = gray[y:y + h, x:x + w] roi_color = img[y:y + h, x:x + w] # Detects eyes of different sizes in the input image eyes = eye_cascade.detectMultiScale(roi_gray) #To draw a rectangle in eyes for (ex,ey,ew,eh) in eyes: cv2.rectangle(roi_color,(ex,ey),(ex + ew,ey + eh),( 0 , 127 , 255 ), 2 ) # Display an image in a window cv2.imshow( 'img' ,img) # Wait for Esc key to stop k = cv2.waitKey( 30 ) & 0xff if k = = 27 : break # Close the window cap.release() # De-allocate any associated memory usage cv2.destroyAllWindows() |
输出:
下一篇文章: OpenCV C++人脸检测程序
参考资料:
- https://www.youtube.com/v=WfdYYNamHZ8
- http://docs.opencv.org/2.4/modules/objdetect/doc/cascade_classification.html?highlight=cascadeclassifier#cascadeclassifier
- http://www.multimedia-computing.de/mediawiki//images/5/52/MRL-TR-May02-revised-Dec02.pdf
本文由 阿夫扎尔·安萨里 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END