problem in displaying video stream window in face detection python

asked 2016-10-03 02:26:09 -0600

irum gravatar image

updated 2016-10-03 02:32:46 -0600

berak gravatar image

hey i just want some help this is my code but i do not know how to display the video using imshow or anyother method. it was working fine but as soon as i put all the code into processes it is running in chunks and secondly i am not able to display video. can someone please help me ? i do not know what i am doing wrong.

import cv2
import os, numpy
import sys
import time
import multiprocessing as mp
from multiprocessing import Process, Queue

#CASCADE_FILE = 'haarcascade_frontalface_default.xml'
fn_dir = 'att_faces'
fn_name = sys.argv[1]
path = os.path.join(fn_dir, fn_name)
if not os.path.isdir(path):
    os.mkdir(path)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

#capture = cv2.VideoCapture(0)
def setup_camera(q):

    capture = cv2.VideoCapture(0)
        ret, frame = capture.read()
    frame = cv2.flip(frame, 1)
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    q.put( frame)
    return frame

def faceDetect(frame):

    print "frame" , frame
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 2)
    print "FACE LENGTH"
    print len(faces)

    if( len(faces)>0):

        for (x,y,w,h) in faces:
            cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
            cv2.putText(frame, 'unknown', (x - 10, y - 10), cv2.FONT_HERSHEY_PLAIN,
                   1,(0, 255, 0))

            roi_gray = gray[y:y+h, x:x+w]
            roi_color = frame[y:y+h, x:x+w]

            eyes = eye_cascade.detectMultiScale(roi_gray)
            for (ex,ey,ew,eh) in eyes:
                    cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

        pin=sorted([int(n[:n.find('.')]) for n in os.listdir(path)
               if n[0]!='.' ]+[0])[-1] + 1
        cv2.imwrite('%s/%s.png' % (path, pin), roi_gray)



    else:
        print"not found"





if __name__ == '__main__':

    while True:




        #capture = cv2.VideoCapture(0)
        print "7 queue"
        q = mp.Queue()


        p1 = Process(target=setup_camera, args=(q,))
            p1.start()
        print "process 1 started"

        fr=q.get()
        print "got frame from queue"
        p1.join()

        print "process 2"
            p2 = Process(target=faceDetect, args=(fr,))
            p2.start()
        print "process 2 started"
        cv2.imshow('img',fr)


#cv2.imshow('img',fr)
cv2.waitKey(0)
#capture.release()
#cv2.destroyAllWindows()
# When everything is done, release the capture
capture.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

2
  • please avoid multi-threading/processing with opencv in general
  • imshow() won't update, unless you call waitKey()
  • don't start a new VideoCapture per image
berak gravatar imageberak ( 2016-10-03 02:34:58 -0600 )edit
1

To expand the comment please avoid multi-threading/processing with opencv in general, in general OpenCV optimizes the functions already internally to use multicore thread support. If you are then manually doing this also, you create a double bottleneck, because the backends are way better at load distribution then manual processing.

StevenPuttemans gravatar imageStevenPuttemans ( 2016-10-03 08:50:04 -0600 )edit