Use OpenCV in Python on Raspberry Pi to detect people.
If the OpenCV module is not installed, install it with the following command.
sudo apt update
sudo apt upgrade -y
sudo apt install python3-pip -y
sudo pip3 --default-timeout=1000 install opencv-contrib-python
sudo apt-get install libhdf5-dev libhdf5-serial-dev libatlas-base-dev libjasper-dev libqtgui4 libqt4-test libgstreamer1.0-0 libwebp-dev libilmbase-dev libopenexr-dev libavcodec-dev libavformat-dev libswscale-dev libharfbuzz-dev
sudo pip3 install opencv-python==4.1.0.25
sudo pip3 install opencv-contrib-python==4.1.0.25
If the Pillow module is not installed, install it with the following command.
sudo apt-get update
sudo apt-get install libjpeg-dev -y
sudo apt-get install zlib1g-dev -y
sudo apt-get install libfreetype6-dev -y
sudo apt-get install liblcms1-dev -y
sudo apt-get install libopenjp2-7 -y
sudo apt-get install libtiff5 -y
sudo pip install Pillow
If you get the following error
from PIL import Image, ImageTk
ImportError: cannot import name 'ImageTk' from 'PIL' (/usr/lib/python3/dist-packages/PIL/__init__.py)
sudo apt-get install python3-pil.imaget
I will try to detect the face.
detect.py and haarcascade_frontalface_default.xml (compressed with ZIP)
import os
import cv2
os.chdir(os.path.dirname(os.path.abspath(__file__)))
face_cascade_path = 'haarcascade_frontalface_default.xml'
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)
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:
break
cap.release()
cv2.destroyAllWindows()
YouTube: Thermal Camera (Thermo AI Device TiD) Python Edition web: Thermo AI device TiD Python OpenCV (URL is subject to change.)
Recommended Posts