gpt4 book ai didi

Python OpenCV--MAC WebCam 不关闭

转载 作者:太空宇宙 更新时间:2023-11-03 21:50:39 25 4
gpt4 key购买 nike

我正在运行人脸识别代码,我的问题是运行代码后,我的网络摄像头没有关闭。绿灯仍然亮着,为了关闭它,我必须关闭我的电脑才能让摄像头正常工作。你有什么建议,为什么会这样?

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)

最佳答案

imutils 库中的

VideoStream 是一个类对象,因此当您将它分配给 vs 时,您应该将其视为一个对象。

这里是修复:

#--- 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()

关于Python OpenCV--MAC WebCam 不关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51293754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com