pythonopencvraspberry-piomxplayer

Raspberry Pi Omxplayer OpenCV


Hi i am trying the improve myself and i am interested with Raspberry Pi. I want to develop a student project with raspberry pi, raspberry pi camera and tft screen. It includes, when raspi cam detect a face, display one movie and while not detect any face display other movie. i wrote a code like below. I used python opencv omxplayer libs. When I run the code, if no face is detected, no video is playing but if the face is detected, the video turns on and off very seriously, the video does not appear, only black shadows come and go on the screen quickly. Thanks for helps. Regards

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 )

Solution

  • The problem here is within the while instruction. When the program detects a face within the while loop. Here the program continues to kill the omxplayer and start the movie.
    Try to remove the while loop and see if the code works.