我在下面有完整的代码,但是在找到面部后,希望显示一组文本(例如" Name")。我尝试了这个,但我没有到达任何地方:
if show_window:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV Tuts!',(0,130), font, 1, (200,255,155),
#!/usr/bin/env python
# opencv-picam-face.py - Opencv using picamera for face tracking using pan/tilt search and lock
# Uses pipan.py module from openelectrons.com RPI camera pan/tilt to control
# Also picamera python module must be installed as well as opencv
# sudo apt-get install libopencv-dev python-opencv
# sudo apt-get install python-picamera
# v4l2 driver is not used since stream is created using picamera module
print("initializing program. Please wait")
import io
import time
import picamera
import picamera.array
import cv2
# openelectron.com python module and files from the OpenElectron RPI camera pan/tilt
# make sure pipan.py is in same folder as this script.
import pipan
p = pipan.PiPan()
# Approx Center of Pan/Tilt motion
pan_x_c = 150
pan_y_c = 140
# bounds checking for pan/tilt search.
limit_y_bottom = 120
limit_y_top = 170
limit_y_level = 140
limit_x_left = 80
limit_x_right = 200
# To speed things up, lower the resolution of the camera
CAMERA_WIDTH = 320
CAMERA_HEIGHT = 240
inch_conv = 7.0
verbose = True # display text messages in console window
if not verbose:
print("verbose is set to False so no status messages will be displayed")
show_window = True # if desktop startx is running then show a window with image and face rectangle
if show_window:
print("You must start this script from a terminal on the startx desktop")
# Camera center of image
cam_cx = CAMERA_WIDTH / 2
cam_cy = CAMERA_HEIGHT / 2
# Face detection opencv center of face box
face_cx = cam_cx
face_cy = cam_cy
# Pan/Tilt motion center point
pan_cx = pan_x_c
pan_cy = pan_y_c
# Amount pan/tilt moves when searching
pan_move_x = 30
pan_move_y = 20
# Timer seconds to wait before starting pan/tilt search for face.
wait_time = 30
# load a cascade file for detecting faces. This file must be in
# same folder as this script. Can usually be found as part of opencv
face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_de fault.xml')
# Saving the picture to an in-program stream rather than a file
stream = io.BytesIO()
#--------------------------------------------------------------------------- --------------------
def pan_goto(x,y): # Move the pan/tilt to a specific location. has built in limit checks.
p.do_pan (int(x))
p.do_tilt (int(y))
if verbose:
print("move camera to pan_cx=%i pan_cy=%i" % ( x, y))
# Start Main Program
with picamera.PiCamera() as camera:
camera.resolution = (CAMERA_WIDTH, CAMERA_HEIGHT)
camera.vflip = True
time.sleep(2)
# Put camera in a known good position.
pan_goto(pan_cx, pan_cy)
face_found = False
start_time = time.time()
loop_cnt = 0
while(True):
with picamera.array.PiRGBArray(camera) as stream:
camera.capture(stream, format='bgr')
time.sleep(.2)
# At this point the image is available as stream.array
image = stream.array
# Convert to grayscale, which is easier
gray = cv2.cvtColor(image, cv2. COLOR_BGR2GRAY)
# Look for max two faces in image stream using the loaded cascade file
faces = face_cascade.detectMultiScale(gray, 1.2, 2)
for (x, y, w, h) in faces:
if w > 0 :
face_found = True
loop_cnt = 0
start_time = time.time()
face_dist = int(( CAMERA_WIDTH / w ) * inch_conv) # Calculate distance from camera to Face
face_cx = x + w/2
face_cy = y + h/2
Nav_LR = cam_cx - face_cx
Nav_UD = cam_cy - face_cy
pan_cx = pan_cx - Nav_LR /5
pan_cy = pan_cy - Nav_UD /4
pan_goto(pan_cx, pan_cy)
# Print Navigation required to center face in image
if verbose:
print("Found: face_dist=%i inches at pan_cx=%i pan_cy=%i Nav_LR=%i Nav_UD=%i " % (face_dist, pan_cx, pan_cy, Nav_LR, Nav_UD))
if show_window:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image,'name',(0,130), font, 1, (200,255,155))
# start pan/tilt search for face if timer runs out
elapsed_time = time.time() - start_time
if elapsed_time > wait_time:
loop_cnt += 1
if loop_cnt > 3: # give camera a few cycles to find a face.
loop_cnt = 0
else:
pan_cx = pan_cx + pan_move_x
if pan_cx > limit_x_right:
pan_cx = limit_x_left
pan_cy = pan_cy + pan_move_y
if pan_cy > limit_y_top:
pan_cy = limit_y_bottom
if verbose:
print("Face Search: loop_cnt=%i Timer=%d > %s seconds" % (loop_cnt, elapsed_time, wait_time))
pan_goto (pan_cx, pan_cy)
# Use opencv built in window to show the image in startx desktop window
# Leave out if your Raspberry Pi isn't set up to display windows
if show_window:
cv2.imshow('openCV Image', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
# cursor over opencv window on desktop then press ctrl-q to Close Window
cv2.destroyAllWindows()
exit
答案 0 :(得分:0)
以下代码对此进行了解释:
face_found = False #---Initially set the flag to be False
for (x, y, w, h) in faces:
if w > 0 : #--- Set the flag True if w>0 (i.e, if face is detected)
face_found = True
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2) #--- highlight the face
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image,'name',(0,130), font, 1, (200,255,155)) #---write the text
cv2.imshow('Face having name', image)