Face recognition with camera with opencv3 + python2.7

【environment】

・ Windows8.1 ・ Surface pro 2

[Preparation]

-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

【procedure】

(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.

pic.twitter.com/gEZY3Jd4Co

[Impression]

・ 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.

[Reference site]

· 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

Face recognition with camera with opencv3 + python2.7
Try face recognition with python + OpenCV
[python, openCV] base64 Face recognition with images
Face recognition with Python's OpenCV
Face detection with Python + OpenCV
Face recognition / cutting with OpenCV
Try face recognition with Python
I tried face recognition with OpenCV
Face detection with Python + OpenCV (rotation invariant)
Image acquisition from camera with Python + OpenCV
Serverless face recognition API made with Python
Face recognition with Edison
Binarization with OpenCV / Python
Python, OpenCV camera capture
Hello World and face detection with OpenCV 4.3 + Python
Performance comparison of face detector with Python + OpenCV
[Python3] [Ubuntu16] [Docker] Try face recognition with OpenFace
Face recognition with Amazon Rekognition
[Python] Using OpenCV with Python (Basic)
Face detection with Python + dlib
Using OpenCV with Python @Mac
Image recognition with Keras + OpenCV
Anime face detection with OpenCV
[Windows] [Python] Camera calibration of fisheye lens with OpenCV
Display USB camera video with Python OpenCV with Raspberry Pi
Number recognition in images with Python
Shining life with Python and OpenCV
Face detection with YOLO Face (Windows10, Python3.6)
Use PointGrey camera with Python (PyCapture2)
[Python] Using OpenCV with Python (Image Filtering)
Neural network with OpenCV 3 and Python 3
Real-time face recognition with video acquired by getUserMedia [HTML5, openCV]
[Python] Using OpenCV with Python (Image transformation)
Face detection with Lambda (Python) + Rekognition
[Python] Using OpenCV with Python (Edge Detection)
A memo when face is detected with Python + OpenCV quickly
Try face recognition with Generated Photos
Easy Python + OpenCV programming with Canopy
[OpenCV] Personal identification with face photo
python x tensoflow x image face recognition
Load gif images with Python + OpenCV
Find image similarity with Python + OpenCV
Use OpenCV with Python 3 in Window
Draw an illustration with Python + OpenCV
Track baseball balls with Python + OpenCV
Graph Based Segmentation with Python + OpenCV
Object recognition with openCV by traincascade
Draw arrows (vectors) with opencv / python
Basic study of OpenCV with Python
How to make a surveillance camera (Security Camera) with Opencv and Python
Replace your face with Twitter icon with openCV face recognition and do ZOOM
The fastest way to get camera images regularly with python opencv
Face recognition using OpenCV (Haar-like feature classifier)
[Python] Face detection by OpenCV (Haar Cascade)
Save video frame by frame with Python OpenCV
Try using the camera with Python's OpenCV
Capturing images with Pupil, python and OpenCV
Easy introduction of speech recognition with Python
I tried non-photorealistic rendering with Python + opencv
Face recognition of anime characters with Keras
Image processing with Python & OpenCV [Tone Curve]