・ Windows8.1 ・ Surface pro 2
-Installation of Anaconda (python2.7) https://www.continuum.io/downloads Install Anaconda that suits your environment from the above site.
・ Installation of OpenCV http://opencv.blog.jp/python/ver3_install Install OpenCV referring to the above site. The "cv2.pyd" file is 「C:\Users\nobu\Anaconda2\Lib\site-packages\Anaconda2\Lib\site-packages」 Please copy it to (this path is for my environment, so please adapt it to your environment).
-Cascade file copy There is "opencv \ build \ etc \ haarcascades" in the OpenCV installation folder. A file called "haarcascade_frontalface_alt.xml" in that folder Copy it to "C: \ Users \ nobu \ Documents \ Python Scripts". By the way, roughly speaking, a cascade file is a configuration file for detecting objects. Reference URL: http://www.pro-s.co.jp/engineerblog/opencv/post_6202.html
(1) Open Spyder (python IDE) that was installed when you installed Anaconda. ② Select "File"-> "New file ..." to open a new file. ② Copy the following code.
camera_face_rec.py
# -*- coding: UTF-8 -*-
import cv2
import os
cascade_path = "haarcascade_frontalface_alt.xml"
#Acquire the features of the cascade classifier
cascade = cv2.CascadeClassifier(cascade_path)
#Capture from camera
cap = cv2.VideoCapture(0)
color = (255, 255, 255) #White
while(True):
#Get a frame from a video stream
ret, frame = cap.read()
#Execution of object recognition (face recognition)
facerect = cascade.detectMultiScale(frame, scaleFactor=1.2, minNeighbors=2, minSize=(10, 10))
for rect in facerect:
#Create a rectangle that surrounds the detected face
cv2.rectangle(frame, tuple(rect[0:2]),tuple(rect[0:2] + rect[2:4]), color, thickness=2)
#display
cv2.imshow("Show FLAME Image", frame)
#Press q to finish.
k = cv2.waitKey(1)
if k == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
③ With the "IPython console" tab on the right side of the spyder open, press the "F5" key. Then, when the camera set by default is activated and the face of the person is projected It will look like the video linked below (I didn't know how to embed the video, so I linked it). Press the "q" key to exit the program.
・ Some faces are not recognized, so I wish I could recognize them more accurately. ・ I think that something interesting will be created when combined with machine learning.
· Python3 OpenCV3 (with MacBook Air camera) frame diff http://blog.umentu.work/%E3%80%90%E5%8B%95%E7%94%BB%E3%81%82%E3%82%8A%E3%80%91python3-opencv3%E3%81%A7macbookair%E3%81%AE%E3%82%AB%E3%83%A1%E3%83%A9%E3%81%A7%E3%83%95%E3%83%AC%E3%83%BC%E3%83%A0%E5%B7%AE%E5%88%86/
・ Getting Started with Videos http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html#display-video
Recommended Posts