problem in displaying video stream window in face detection python

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

irum gravatar image

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

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):
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 =
    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.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:

        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)

        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,))
        print "process 1 started"

        print "got frame from queue"

        print "process 2"
            p2 = Process(target=faceDetect, args=(fr,))
        print "process 2 started"

# When everything is done, release the capture
edit retag flag offensive close merge delete


  • 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 -0500 )edit

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 -0500 )edit