我正在使用一个Rapberry Pi在网络服务器上进行视频流传输。
视频流工作正常,但一旦我实现了人脸识别,它将是如此滞后。
这是我的代码:
from flask import Flask, render_template, Response
import cv2
import argparse
from imutils.video import VideoStream
import imutils
import subprocess
app = Flask(__name__)
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
vs = VideoStream(usePiCamera=1).start()
def gen_frames(): # generate frame by frame from camera
while True:
# Capture frame-by-frame
global vs
frame = vs.read() # read the camera frame
frame = imutils.resize(frame, width=800)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
#Draw the rectangle around each face
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # concat frame one by one and show result
@app.route('/video_feed')
def video_feed():
#Video streaming route. Put this in the src attribute of an img tag
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/')
def index():
"""Video streaming home page."""
return render_template('index.html')
def videostreaming():
# start the flask app
app.run(host="192.168.0.33",port=8000, debug=True,use_reloader=False)
videostreaming()
有没有办法让视频流更流畅的人脸检测功能?
1条答案
按热度按时间wfveoks01#
看起来视频的宽度是导致我的视频流出现问题的原因。只是不得不把它减少到500,使它更流畅。
发件人:
收件人: