Flask实时面部表情分析应用摄像头无画面问题求助
实时面部表情分析Flask应用摄像头画面不显示问题
需求说明
- 启动应用时自动激活设备摄像头
- 通过Py-Feat分析摄像头捕获的面部,实现实时情绪识别
问题现象
运行应用后摄像头画面无法在屏幕上显示,浏览器控制台无特定错误信息,终端输出weights=None提示,怀疑该提示与问题相关。
已排查项
- 曾出现
favicon not found 404错误,已通过创建favicon文件解决 - 运行时出现
Error reading frame from camera提示,已确认内置摄像头可用(通过cap_cam = cv2.VideoCapture(0)验证) - 已关闭所有Chrome扩展排除干扰
- 控制台提示
Can't read 3D image with 2D axis. Install numpy and support 3D!,但已安装numpy仍报错,这是当前最可疑的原因但未找到解决方案
参考代码
flask_AIface.py
from flask import Flask, render_template, Response import numpy as np import cv2 from feat import Detector # rf: https://py-feat.org/content/intro.html#available-models face_model = "retinaface" landmark_model = "mobilenet" au_model = "svm" emotion_model = "resmasknet" detector = Detector( face_model=face_model, landmark_model=landmark_model, au_model=au_model, emotion_model=emotion_model ) app = Flask(__name__) cap_cam = cv2.VideoCapture(0) @app.route('/') def index(): return render_template('index.html') # ここではindex.htmlというテンプレートをレンダリングします。 def gen(): # ここに、カメラからフレームを取得し、それを処理するコードを書く。 cap = cv2.VideoCapture(0) while True: ret, frame = cap_cam.read() if not ret: print('Error reading frame from camera') continue if len(frame.shape) == 2: frame = np.stack((frame,) * 3, axis=-1) else: frame = frame[:, :, ::-1] gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = detector.detect_faces(gray) for face in faces: bbox = face.bbox.astype(int) emotion = detector.detect_emotion(face) cv2.putText(frame, emotion, (bbox[0], bbox[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2) ret, jpeg = cv2.imencode('.jpg', frame) yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n\r\n') @app.route('/video_feed') def video_feed(): return Response(gen(), mimetype='multipart/x-mixed-replace; boundary=frame') # MJPEG形式でストリーミングします。 if __name__ == '__main__': # すべてのインターフェイスからのアクセスを許可し、デバッグモードで実行します。 app.run(host='0.0.0.0', port=5001, debug=True)
index.html
<html> <head> <title>Real-Time Face Emotion App</title> <link href="favicon.ico" rel="icon"> </head> <body> <img src="{{ url_for('video_feed') }}" width="640" height="480"> </body> </html>
开发环境
- VSCode
- MacBook Air M1
- iPad Pro 12.9英寸(用作扩展显示器)
内容的提问来源于stack exchange,提问作者Terada Kousuke
相关产品推荐
相关产品推荐

