1

嗨,我正在尝试改进自己,我对 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 )
4

1 回答 1

1

这里的问题是在 while 指令中。当程序在 while 循环中检测到人脸时。在这里,程序继续杀死 omxplayer 并开始播放电影。
尝试删除while循环并查看代码是否有效。

于 2017-01-26T13:42:38.950 回答