Face detection from images taken with Raspberry Pi camera

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.

Face detection

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.

Person detection

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.

QR code decoding

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

Face detection from images taken with Raspberry Pi camera
Display images taken with the Raspberry Pi camera module
GPGPU with Raspberry Pi
Raspberry Pi video camera
How to put OpenCV in Raspberry Pi and easily collect images of face detection results with Python
DigitalSignage with Raspberry Pi
A memorandum when making a surveillance camera with Raspberry Pi
Display USB camera video with Python OpenCV with Raspberry Pi
Mutter plants with Raspberry Pi
Face detection with Python + dlib
Face detection with Python + OpenCV
Face detection with Haar Cascades
Anime face detection with OpenCV
Play with the Raspberry Pi Zero WH camera module Part 1
Create a web surveillance camera with Raspberry Pi and OpenCV
Python beginner opens and closes interlocking camera with Raspberry Pi
Upload images taken with an action camera with a Mapillay script
I made a surveillance camera with my first Raspberry PI.
Face detection with YOLO Face (Windows10, Python3.6)
Use vl53l0x with Raspberry Pi (python)
Servo motor control with Raspberry Pi
Serial communication with Raspberry Pi + PySerial
Face detection with Lambda (Python) + Rekognition
Output from Raspberry Pi to Line
Notify LINE of body temperature from BLE thermometer with Raspberry Pi # 1
OS setup with Raspberry Pi Imager
Try L Chika with raspberry pi
I tried face detection with MTCNN
VPN server construction with Raspberry Pi
Face recognition with camera with opencv3 + python2.7
Get US stock price from Python with Web API with Raspberry Pi
Try moving 3 servos with Raspberry Pi
[Note] Using 16x2-digit character LCD (1602A) from Python with Raspberry Pi
Using a webcam with Raspberry Pi
Try tweeting arXiv's RSS feed on twitter from Raspberry Pi with python
Measure SIM signal strength with Raspberry Pi
Face detection with Python + OpenCV (rotation invariant)
Hello World and face detection with opencv-python 4.2
Pet monitoring with Rekognition and Raspberry pi
Raspberry Pi Security Infrared Camera (Python Edition)
Hello World with Raspberry Pi + Minecraft Pi Edition
Build a Tensorflow environment with Raspberry Pi [2020]
Face detection by collecting images of Angers.
Get BITCOIN LTP information with Raspberry PI
Try fishing for smelt with Raspberry Pi
Programming normally with Node-RED programming on Raspberry Pi 3
Image acquisition from camera with Python + OpenCV
Improved motion sensor made with Raspberry Pi
[python, openCV] base64 Face recognition with images
Power SG-90 servo motor with raspberry pi
Working with sensors on Mathematica on Raspberry Pi
Use PIR motion sensor with raspberry Pi
Make a wash-drying timer with a Raspberry Pi
Infer Custom Vision model with Raspberry Pi
Operate an oscilloscope with a Raspberry Pi
Create a car meter with raspberry pi
Inkbird IBS-TH1 value logged with Raspberry Pi
[Python] Real-time object detection with iPad camera
Working with GPS on Raspberry Pi 3 Python
Try to detect an object with Raspberry Pi ~ Part 1: Comparison of detection speed ~
Discord bot with python raspberry pi zero with [Notes]