Hello everyone! This time, using python and OpenCV, I created a program that can start the camera and write a mosaic on a human face in real time. Maybe someone else has already written the article ... maybe it's out. Let's do it anyway!
The flow of the program is here.
Start the camera using OpenCV.
kido.py
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
cv2.imshow('video image', img)#'video image'Is the name of the camera window
key = cv2.waitKey(10)
if key == 27: #Exit with ESC key
break
cap.release()
cv2.destroyAllWindows()
Now you can orbit the camera. Press the Esc key and the window will close.
OpenCV comes with a Haar-like feature classifier in advance. Convenient! However, it cannot be handled unless the file is saved. Since I want to recognize human faces this time, I will handle haarcascade_frontalface_alt.xml of here.
By the way, the Haar-like feature classifier detects the face by extracting the features while changing the size of the Haar-like filter by the Haar-like features focusing on the difference in brightness from the features that can take the features of the image and AdaBoost. I will. Now, let's start the camera and let it recognize the human face.
ninsiki.py
import cv2
#Haar-like Be able to handle feature classifiers.
face_cascade_path="haarcascade_frontalface_alt.xml" #Please specify the path.
face_cascade = cv2.CascadeClassifier(face_cascade_path)
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
#Rectangle around a person's face
for x, y, w, h in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
face = img[y: y + h, x: x + w]
face_gray = gray[y: y + h, x: x + w]
cv2.imshow('video image', img)
key = cv2.waitKey(10)
if key == 27: #Exit with ESC key
break
cap.release()
cv2.destroyAllWindows()
Now you can detect the human face.
Without thinking that mosaic processing is difficult, the face image is reduced once, then enlarged and pasted on the face image.
mozaic.py
import cv2
#Haar-like Be able to handle feature classifiers.
face_cascade_path="haarcascade_frontalface_alt.xml"
face_cascade = cv2.CascadeClassifier(face_cascade_path)
ratio = 0.07 #Changing the value here changes the roughness of the mosaic.
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
for x, y, w, h in faces:
#The mosaic processing is done in two individual lines.
small = cv2.resize(img[y: y+h, x: x+w], None,fy=ratio, fx=ratio,interpolation=cv2.INTER_NEAREST)
img[y: y + h, x: x + w] = cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST)
cv2.imshow('video image', img)
key = cv2.waitKey(10)
if key == 27: #Exit with ESC key
break
cap.release()
cv2.destroyAllWindows()
It was completed.
This time, we recognized the face in real time and applied mosaic processing to the face. Since it is classified by the difference in brightness, even a caricature will react. (There are quite a few false positives), but OpenCV is fun because you can try face detection (face recognition) with a short code. It may be difficult to understand because I have never sent information such as blogs. From now on, I would like to disseminate information and improve my writing skills, and my dream is to become a person who can play with technology, so please watch with warm eyes. Thank you in advance. Well then!
Recommended Posts