如何在while循环中只执行一次for循环中的if else语句
How to execute if else statement in for loop only once in while loop
早上好,我是 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')
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()
最好的方法似乎是在循环外调用 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 语言的初学者,我想问一个关于 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')
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()
最好的方法似乎是在循环外调用 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')