Good Morning, I am beginner in python language, I would to ask a question regarding on python code. Fyi, currently I am working on face recognition with voice. Currently my problem when i call get_frame() function. The speak.tts("your name"+name,lang) code executed repeatedly and non-stop. My question how i going to execute this only once when i call this function in my app.py and it's will not produce the voice repeatedly. Below I share my code, if you dont understand the code let's me know i will try the best to explain and maybe can add more details code. Hope some one can helps thanks.
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()
The best way seems to call get_frame()
outside the loop.
If you want to call get_frame()
only once when calling the gen(camera)
function, you should not put the call in the loop, as the loop will repeatedly execute its instructions.
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')