带有与每个帧相关联的数据结构的Flask Video Streaming

时间:2018-06-13 16:35:37

标签: python reactjs flask web-applications computer-vision

非常新的网络开发和使用Flask构建面部识别的Web应用程序,希望React作为前端。我使用了一个教程来设置烧录视频流,并集成了openCV,它运行良好,但我想将一些数据与每个帧一起传递到我的前端,并且我很难理解如何这样做。

from flask import Flask, render_template, Response
from camera import VideoCamera
import cv2
import face_recognition
import os
import glob
os.chdir('C:/Users/fk143vt/Desktop/Flask')
app = Flask(__name__)

known_face_encodings = []
known_face_names = []
for file in glob.glob("images/*"):
    identity = os.path.splitext(os.path.basename(file))[0]
    im = cv2.imread(file, 1)
    known_face_encodings.append(face_recognition.face_encodings(im)[0])
    known_face_names.append(identity)


#os.chdir('C:/Users/yx559hx/Desktop/Face Recog Demo/Flask')

@app.route('/')
def index():
    return render_template('hello.html')

def gen(camera):
    face_locations = []
    face_encodings = []
    face_names = []
    process_this_frame = True
    while True:
        frame_jpg, frame = camera.get_frame()
        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
        rgb_small_frame = small_frame[:, :, ::-1]
        if process_this_frame:
            face_locations = face_recognition.face_locations(rgb_small_frame)
            face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

            face_names = []    
            for face_encoding in face_encodings:
                matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
                name = "Unknown"

                if True in matches:
                    first_match_index = matches.index(True)
                    name = known_face_names[first_match_index]
                face_names.append(name)
        process_this_frame = not process_this_frame

        names_locations = zip(face_locations, face_names)

        for (top, right, bottom, left), name in names_locations:
            top *= 4
            right *= 4
            bottom *= 4
            left *= 4
            cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
            cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 255, 0), cv2.FILLED)
            font = cv2.FONT_HERSHEY_DUPLEX
            cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (0, 0, 0), 1)

        ret, frame_jpg = cv2.imencode('.jpg', frame)    
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame_jpg.tobytes() + b'\r\n\r\n')



@app.route('/video_feed')
def video_feed():
    return Response(gen(VideoCamera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

我的相机档案:

import cv2

class VideoCamera(object):
    def __init__(self):
        self.video = cv2.VideoCapture(0)        
    def __del__(self):
        self.video.release()

    def get_frame(self):
        success, image = self.video.read()
        # We are using Motion JPEG, but OpenCV defaults to capture raw images,
        # so we must encode it into JPEG in order to correctly display the
        # video stream.
        ret, jpeg = cv2.imencode('.jpg', image)
        return jpeg.tobytes(), image
        #return image

我的问题是我需要将面部识别软件识别的名称与视频一起流式传输,以便可以在浏览器中捕获名称。我不知道如何构建我的响应对象以将图像和配对字符串传递给前端,因为正在使用生成器来传输视频。

非常感谢任何建议!

0 个答案:

没有答案