嗨,我正在尝试改进自己,我对 Raspberry Pi 很感兴趣。我想用树莓派、树莓派相机和 tft 屏幕开发一个学生项目。它包括,当 raspi cam 检测到面部时,显示一部电影,而不检测到任何面部时显示另一部电影。我写了如下代码。我使用了 python opencv omxplayer 库。当我运行代码时,如果没有检测到人脸,则没有视频正在播放,但如果检测到人脸,则视频打开和关闭非常严重,视频没有出现,只有黑色阴影在屏幕上快速来来去去。感谢您的帮助。问候
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import os
import numpy
from subprocess import Popen
#setup movies
movie1 = ("my_movie1_path")
movie2 = ("my_movie2_path")
camera = PiCamera()
camera.resolution = ( 320, 240 )
camera.framerate = 60
rawCapture = PiRGBArray( camera, size=( 320, 240 ) )
# Load a cascade file for detecting faces
face_cascade = cv2.CascadeClassifier( 'my_path/lbpcascade_frontalface.xml' )
t_start = time.time()
fps = 0
# Capture frames from the camera
for frame in camera.capture_continuous( rawCapture, format="bgr", use_video_port=True ):
image = frame.array
# Use the cascade file we loaded to detect faces
gray = cv2.cvtColor( image, cv2.COLOR_BGR2GRAY )
faces = face_cascade.detectMultiScale( gray )
print "1"
While True:
if len( faces ) > 0 :
os.system('killall omxplayer.bin')
omcx = Popen(['omxplayer', '-b', movie2])
else :
os.system('killall omxplayer.bin')
omcx = Popen(['omxplayer', '-b', movie1])
#print "Found " + str( len( faces ) ) + " face(s)"
print "2"
rawCapture.truncate( 0 )