python - 如何在while循环中只执行一次for循环中的if else语句

标签 python python-3.x opencv voice-recognition face-recognition

早上好,我是 python 语言的初学者,我想问一个关于 python 代码的问题。仅供引用,目前我正在研究语音人脸识别。目前我在调用 get_frame() 函数时遇到的问题。 speak.tts("your name"+name,lang) 代码不断重复执行。我的问题是,当我在我的 app.py 中调用此函数时,我将如何只执行一次,并且它不会重复发出声音。下面我分享我的代码,如果你不理解代码让我知道我会尽力解释并且也许可以添加更多详细信息代码。希望有人能帮忙谢谢。

app.py

def gen(camera):
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

相机.py

class VideoCamera:
    def __init__(self,app):
        self.known_encoding_faces = aface.known_encoding_faces
        self.user_id = aface.face_user_keys
        self.faces = []
        self.test = []
        self.video_capture = cv2.VideoCapture(0)
        self.face_user_keys = {}
        self.name_face()



    def get_frame(self):
        face_locations = []
        face_encodings = []
        face_names = []
        process_this_frame = True
        success, frame = self.video_capture.read()
        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
        rgb_small_frame = small_frame[:, :, ::-1]
        flag = False

        # Only process every other frame of video to save time
        if process_this_frame:
            # Find all the faces and face encodings in the current frame of video
            face_locations = face_recognition.face_locations(rgb_small_frame,number_of_times_to_upsample=2)
            #print(face_locations)
            face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
            #print(face_encodings)
            if len(face_encodings) > 0:
                face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)[0]

                face_names = []
                for face_encoding in face_encodings:
                    # See if the face is a match for the known face(s)
                    matches = face_recognition.compare_faces(self.known_encoding_faces, face_encodings, tolerance=0.6)
                    #print(matches)
                    name = "Unknown"

                    # If a match was found in known_face_encodings, just use the first one.
                    if True in matches:
                        first_match_index = matches.index(True)
                        name = self.faces[first_match_index]['name']
                    face_names.append(name)
                    #print(face_names)



        process_this_frame = not process_this_frame

        # Display the results
        for (top, right, bottom, left), name in zip(face_locations, face_names):
            #Scale back up face locations since the frame we detected in was scaled to 1/4 size
            top *= 4
            right *= 4
            bottom *= 4
            left *= 4

            # Draw a box around the face
            cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
            # Draw a label with a name below the face
            cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
            font = cv2.FONT_HERSHEY_DUPLEX


                # description = ', '.join(name)
            cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
                # tts = gTTS(name, lang='en')
                # tts.save('tts.mp3')
                # tts = AudioSegment.from_mp3("tts.mp3")
                # subprocess.call(["ffplay", "-nodisp", "-autoexit", "tts.mp3"])
            if (val == 9):
                speak.tts("your name"+name,lang)
                break
        ret, jpeg = cv2.imencode('.jpg', frame)
        return jpeg.tobytes()
    def __del__(self):
        self.video_capture.release()

最佳答案

最好的方法似乎是在循环外调用 get_frame()。 如果你想在调用gen(camera)函数时只调用一次get_frame(),你不应该把调用放在循环中,因为循环会重复执行它说明。

def gen(camera):
    frame = camera.get_frame()
    while True:
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

关于python - 如何在while循环中只执行一次for循环中的if else语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57193435/

相关文章:

python - print dataframe.head 给出的输出不是一个漂亮的表格

python - 在python中删除字符中串(可迭代)

python - 为什么python在opencv中抛出Assertation错误以进行对象检测?

生成自定义类集合的 Pythonic 多重继承

opencv - 使用 OpenCV 跟踪山脊 - 返回 'ridges' 数组

python - 如何从二进制骨架化图像中找到分支点

python - 我可以在Python中设置Prometheus标签的默认值吗?

python - 查找列表中最常见的元素

python - 对齐子列表中的数字

python - 如何在Python中区分电子邮件html正文和html附件?