视频流在实现人脸检测时滞后



我正在使用Rapberry Pi在web服务器上视频流。

视频流工作得很好,但是一旦我实现了人脸识别,它将是如此的滞后。

这是我的代码:

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'--framern'b'Content-Type: image/jpegrnrn' + frame + b'rn')  # 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()

是否有一种方法可以使视频流更流畅与面部检测功能?

似乎视频的宽度是导致我的视频流出现问题的原因。只是必须将其减少到500以使其更平滑。

来自:

frame = imutils.resize(frame, width=800)

:

frame = imutils.resize(frame, width=500)

最新更新