如何在for循环中仅在一次while循环中执行if else语句

时间:2019-07-25 02:22:37

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

早上好,我是python语言的初学者,我想问一个关于python代码的问题。 Fyi,目前我正在研究带语音的人脸识别。目前,我在调用 get_frame()函数时遇到了问题。 speak.tts(“您的名字” + 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')

camera.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()

1 个答案:

答案 0 :(得分:0)

最好的方法似乎是在循环外调用get_frame()。 如果要在调用get_frame()函数时仅调用一次gen(camera),则不应将调用置于循环中,因为循环将重复执行其指令。

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')
相关问题