问题描述
我正在尝试制作一个使用 OpenCV、HaarCascade 和 Pyttsx3 检测面罩的项目。该项目的流程是,当脸上没有检测到嘴巴时,它应该说“检测到口罩”,否则应该说“请戴口罩”。我面临的问题是,当它第一次说话时帧冻结并且循环在那里结束。请帮我解决问题。
import cv2
import pyttsx3
camera=cv2.VideoCapture(0)
face_cascade=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
mouth_cascade=cv2.CascadeClassifier('haarcascade_mcs_mouth.xml')
sum=0
count=0
display=""
color=(0,255)
#function to use text to speech
def speak(display): #here the issue is it speaks first time and then programs ends
engine = pyttsx3.init()
engine.say(display)
engine.runAndWait()
engine.stop()
while True:
cap,frame = camera.read()
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces=face_cascade.detectMultiScale(gray,1.3,1)
font = cv2.FONT_HERShey_SIMPLEX
cv2.putText(frame,display,(50,50),font,1,color,2,cv2.LINE_4)
#loop to create rectangle around faces and finding the roi to apply mouth detection
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0),5)
roi_gray = gray[y:y + h,x:x + w]
roi_color = frame[y:y + h,x:x + w]
mouth=mouth_cascade.detectMultiScale(roi_gray,1.7,6)
for (mx,my,mw,mh) in mouth:
cv2.rectangle(roi_color,(mx,my),(mx + mw,my + mh),(0,255,2)
cv2.imshow("Test",frame)
w=cv2.waitKey(1)
#if mouth is not detected in face then person is wearing face mask
if len(mouth) == 0:
display="Mask Detected"
color=(0,0)
speak(display)
else:
display="Please Wear Face Mask"
color = (0,255)
speak(display)
if w==ord('q'):
break
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)