Herunterladen Diese Seite drucken

SunFounder PiCar-X Kit Bedienungsanleitung Seite 74

Werbung

SunFounder PiCar-X Kit
cd ~/picar-x/example
sudo python3 human_face_detect.py
Nachdem der Code ausgeführt wurde, wird das Gesicht auf dem Bildschirm überprüft.
Code
import
cv2
from
picamera.array
from
picamera
import
import
time
def
human_face_detect(img):
resize_img
=
cv2.resize(img, (320,240), interpolation=cv2.INTER_LINEAR)
order to reduce the amount of calculation, resize the image to 320 x 240 size
gray
=
cv2.cvtColor(resize_img, cv2.COLOR_BGR2GRAY)
faces
=
face_cascade.detectMultiScale(gray, 1.3, 2)
images
face_num
=
len(faces)
if
face_num
>
0:
for
(x,y,w,h)
x
=
x*2
the x, y, w, and h must be multiplied by 2.
y
=
y*2
w
=
w*2
h
=
h*2
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
face
return
img
with
as
PiCamera()
camera:
print("start human face
camera.resolution
camera.framerate
rawCapture
=
PiRGBArray(camera, size=camera.resolution)
time.sleep(2)
for
in
frame
camera.capture_continuous(rawCapture, format="bgr",use_video_
port=True):
# use_video_port=True
img
=
frame.array
img
=
human_face_detect(img)
cv2.imshow("video", img)
rawCapture.truncate(0)
k
=
cv2.waitKey(1)
# 27 is the ESC key, which means that if you press the ESC key to exit
if
k
==
27:
break
70
import
PiRGBArray
PiCamera
# Number of detected faces
in
faces:
# Because the image is reduced to one-half of the original
detect")
=
(640,480)
=
24
#OpenCV image show
# Release cache
&
0xFF
# Convert to grayscale
# Detect faces on
grayscale␣
# Draw a rectangle on
(Fortsetzung auf der nächsten Seite)
Kapitel 4. Mit Python spielen
#
In␣
size,␣
the␣

Werbung

loading