2

I am running a face recognition code and my issue is that after running the code, my webcam doesn't close. The green light is still activated and in order to close it, i have to close my computer in order to have the cam ok. What do you suggest, why is like that?

from imutils.video import VideoStream
import argparse, imutils, time, cv2, os, sys
import time
import smtplib, os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.message import Message
from email.mime.audio import MIMEAudio
from email import encoders


   detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# vs = VideoStream(src=0).start()
vs = VideoStream
vs(src=0).start()
# time.sleep(2.0)
total = 0

while True:
    frame = vs.read()
    orig = frame.copy()
    frame = imutils.resize(frame, width=600)
    rects = detector.detectMultiScale(
        cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), scaleFactor=1.1, 
        minNeighbors=5, minSize=(30, 30))
    for (x, y, w, h) in rects:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        if rects is not None and total <5:
            p="/Users/Desktop/atentie/mail/"+str(total)+".png"
            cv2.imwrite(p, orig)
            total+=1

    break   

cv2.destroyAllWindows()
vs(src=0).stop()
time.sleep(5)
Cohen
  • 944
  • 3
  • 13
  • 40

1 Answers1

0

VideoStream from imutils library is a class object, so when you assign it to vs you ought to treat it like one.

Here is the fix:

#--- import libraries ---
from imutils.video import VideoStream

vs = VideoStream
#--- start the video stream ---
vs(src=0).start()

#--
# rest of your program 
#--

#--- stop the video stream ---
vs(src=0).stop()
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • Thank you Luke! still doesn't do the trick and the camera doesn't close. the green light is still open. – Cohen Jul 11 '18 at 21:11
  • @Cohen silly me I made a mistake. You are supposed to add `vs = VideoStream` before starting the stream. Check the update – Jeru Luke Jul 11 '18 at 21:17
  • i did, i did! i got it, but still, it doesn't close...dunno what to do. – Cohen Jul 11 '18 at 21:20
  • this wouldn't change anything, you are copying the class, and using it later, forgetting affecting the instantiation. – Loïc Jul 11 '18 at 21:20
  • @Cohen You are doing it wrong. Please look closely it is `vs = VideoStream` not `vs = VideoStream(src=0)`. I tried it and it works, that is why I am suggesting it. – Jeru Luke Jul 11 '18 at 21:25
  • I've changed it because i get this error odule> frame = vs.read() TypeError: read() missing 1 required positional argument: 'self' – Cohen Jul 11 '18 at 21:52
  • @Cohen I'm sorry but it works fine for me. Can you share the updated code again? – Jeru Luke Jul 11 '18 at 21:55
  • @Cohen I tried the same on another system and the camera actually turns off. I still don't understand why it doesn't work for you :( – Jeru Luke Jul 12 '18 at 06:16
  • Me neither. I am on a mac. – Cohen Jul 12 '18 at 06:17
  • @Cohen The two systems that I tried on are Windows 8 and 10 – Jeru Luke Jul 12 '18 at 06:18
  • am on Mac so Unix environment. 10x – Cohen Jul 12 '18 at 06:59
  • 1
    Yes. I changed the code. Somehow there was a loop that didnt want to break so i had to use raise Syserror or something like this. Thanks for help tough! – Cohen Jul 13 '18 at 06:17