如何禁用DeepFace库中的情绪检测进度条?
如何禁用DeepFace情绪检测的进度条?
我正在用DeepFace分析视频中的情绪,代码如下:
import cv2 from deepface import DeepFace cap = cv2.VideoCapture("video.mp4") prev_emotion = None prev_time = 0 while True: ret, frame = cap.read() if not ret: break # 分析帧中的情绪 result = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False) # 获取主导情绪 dominant_emotion = result[0]['dominant_emotion'] # 获取当前时间 current_time = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000 # 检查情绪是否变化 if dominant_emotion != prev_emotion: # 检查是否为首次检测到情绪 if prev_emotion is not None: print(f"{current_time:.2f} seconds: Emotion changed from {prev_emotion} to {dominant_emotion}") # 更新之前的情绪和时间 prev_emotion = dominant_emotion prev_time = current_time cap.release()
运行时每帧都会输出类似这样的进度条:
Action: emotion: 100%|██████████| 1/1 [00:00<00:00, 18.19it/s]
这个进度条对我的使用场景没用,想知道能不能禁用,以及具体操作方法。
解决方案
可以直接在DeepFace.analyze()方法中添加progress_bar=False参数来禁用进度条。
修改后的核心代码行如下:
result = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False, progress_bar=False)
完整的修改后代码:
import cv2 from deepface import DeepFace cap = cv2.VideoCapture("video.mp4") prev_emotion = None prev_time = 0 while True: ret, frame = cap.read() if not ret: break # 分析帧中的情绪,禁用进度条 result = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False, progress_bar=False) # 获取主导情绪 dominant_emotion = result[0]['dominant_emotion'] # 获取当前时间 current_time = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000 # 检查情绪是否变化 if dominant_emotion != prev_emotion: # 检查是否为首次检测到情绪 if prev_emotion is not None: print(f"{current_time:.2f} seconds: Emotion changed from {prev_emotion} to {dominant_emotion}") # 更新之前的情绪和时间 prev_emotion = dominant_emotion prev_time = current_time cap.release()
添加该参数后,运行代码就不会再输出进度条,只会保留你需要的情绪变化日志。
内容的提问来源于stack exchange,提问作者obk
相关产品推荐
相关产品推荐

