首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将OpenCV与pyttsx3结合使用

如何将OpenCV与pyttsx3结合使用
EN

Stack Overflow用户
提问于 2021-04-04 18:33:38
回答 1查看 147关注 0票数 0

我正在尝试用OpenCV,HaarCascade和Pyttsx3做一个检测面膜的项目。这个项目的流程是,当在脸上没有检测到嘴巴时,它应该说“面膜检测到了”,否则它应该说“请戴口罩”。我面临的问题是,当它第一次讲话时,帧冻结了,循环在那里结束,。请帮我解决这个问题。

代码语言:javascript
复制
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, 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,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, 0), 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, 255, 0)
        speak(display)
    else:
        display="Please Wear Face Mask"
        color = (0, 0, 255)
        speak(display)
    if w==ord('q'):
        break
EN

回答 1

Stack Overflow用户

发布于 2021-09-16 13:47:39

要快速回答v,您需要使用线程。这是医生。https://docs.python.org/3/library/threading.html。您可以将您的count附加到列表中,当没有掩码时,执行count + 1。如果您的count_list >=1,使用线程调用语音。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66944610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档