0

I am developing an application in python(ver-2.7) for real-time face detection using opencv3. Currently i am able to access the video stream using VideoCapture(1) but the live-video loads very slowly.

    import cv2
    import time
    detector=cv2.CascadeClassifier('lbpcascade_frontalface.xml')
    cam=cv2.VideoCapture(1)
    cam.open("http://root:admin@192.168.1.201/mjpg/video.mjpg")
    Id=raw_input('enter your id')
    sampleNum=0
    while(True):
       ret, img = cam.read()
       gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
       faces = detector.detectMultiScale(gray, 1.2,3)
       for (x,y,w,h) in faces:
         cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2)
         #incrementing sample number 
         sampleNum=sampleNum+1
         #saving the captured face in the dataset folder        
         cv2.imwrite("dataset/User."+Id +'.'+ str(sampleNum) + ".jpg", 
         gray[y:y+h,x:x+w])
         cv2.imshow('frame',img)   
      if cv2.waitKey(100) & 0xFF == ord('q'):
         break

     elif sampleNum>19:
         break

    cam.release()
    cv2.destroyAllWindows()
Ashwini Saini
  • 1,324
  • 11
  • 20
  • why are you waiting 100 ms between each frame with `cv2.waitKey(100)`? Use `cv2.waitKey(1)`. Also, if the detector is slow, you should use multithreading – Miki Sep 04 '18 at 10:25
  • i am a newbie in python. How should i go about multilthreading? – Ruchika Mattoo Sep 04 '18 at 11:11
  • you might wanna check this [multithreaded frame reading](https://github.com/rktayal/multithreaded_frame_reading) and [face verification](https://github.com/rktayal/face_recognition_demo) demo. Might be useful – Rachit Tayal May 22 '19 at 06:35

0 Answers0