Since I captured the image with the camera module V2.1 attached to Raspberry Pi 4 last time, this time I will try to detect the face etc. from that image.
I tried using the face detection program in openCV. Since only the face facing the front is detected, the profile cannot be detected. Also, the image capture program is the same as last time, so the explanation is omitted.
face.py
import cv2
camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH,640)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
cascade = cv2.CascadeClassifier('/home/pi/.local/lib/python3.7/site-packages/cv2/data/haarcascade_frontalface_alt.xml')
while True:
_,image = camera.read()
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
face = cascade.detectMultiScale(gray,minSize=(50,50))
for x,y,w,h in face:
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow('camera',image)
key = cv2.waitKey(1)
if key != -1:
break
camera.release()
cv2.destroyAllWindows()
The face detection part is described below.
python
cascade = cv2.CascadeClassifier('/home/pi/.local/lib/python3.7/site-packages/cv2/data/haarcascade_frontalface_alt.xml')
...
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
face = cascade.detectMultiScale(gray,minSize=(50,50))
The xml file included in CascadeClassifier
is a file for face recognition, and the detection target (face, eyes, nose, etc.) can be changed by changing this file. I think the path will change depending on the environment where openCV is installed, but in my case (I installed openCV with pip3), the files were placed in this path.
After converting the image to be detected to black and white with cvtColor
, the face is detected with detectMultiScale
.
The part where the detected face is surrounded by a green frame is described below.
python
for x,y,w,h in face:
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
The face coordinates detected by detectMultiScale
are received in list format. The last 2 shows the thickness of the line.
I also tried using the detection program in openCV. Unfortunately, the detection accuracy was not as high as I expected.
person.py
import cv2
camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH,640)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
while True:
_,image = camera.read()
person,_ = hog.detectMultiScale(image)
for x,y,w,h in person:
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow('camera',image)
key = cv2.waitKey(1)
if key != -1:
break
camera.release()
cv2.destroyAllWindows()
The person detection part is described below.
python
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
...
person,_ = hog.detectMultiScale(image)
Unlike face detection, the color image is passed as it is.
I also made a QR code decoding program.
qr.py
import cv2
from pyzbar.pyzbar import decode
camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH,640)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
while True:
_,image = camera.read()
qrdata = decode(image)
for qr in qrdata:
cv2.rectangle(image,(qr.rect.left,qr.rect.top),(qr.rect.left+qr.rect.width,qr.rect.top+qr.rect.height),(0,255,0),2)
cv2.putText(image,qr.data.decode('utf-8'),(0,20),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0))
cv2.imshow('camera',image)
key = cv2.waitKey(1)
if key != -1:
break
camera.release()
cv2.destroyAllWindows()
The decoding part is described below.
python
qrdata = decode(image)
The character display part is described below.
python
cv2.putText(image,qr.data.decode('utf-8'),(0,20),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0))
data
is the string,(0,20)
is the display coordinates, 1
is the size of the character, and (0,255,0)
is the BGR.
Recommended Posts