首页 > 解决方案 > 使用Flask的浏览器html中的OpenCV非常慢且延迟

问题描述

当我尝试使用模型媒体管道将模型 .pkl 上传到带有各种细节点的烧瓶中时,会导致大量视频延迟和延迟,有什么办法可以解决这些问题吗?(但是当我尝试在 Jupyter notebook 中运行 open CV 时,没有这些问题。)

标签: pythonhtmlflask

解决方案


这是我的代码

import numpy as np
from flask import Flask, request, render_template, Response
import pickle
import cv2
import mediapipe as mp
import pandas as pd
import csv
import cv2
import time

app = Flask(__name__)

model = pickle.load(open('normal.pkl','rb'))

mp_drawing = mp.solutions.drawing_utils
mp_holistic = mp.solutions.holistic

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

def gen():
    """Video streaming generator function."""
    cap = cv2.VideoCapture(0)
    count = 0
    direc = 0
    fix = 0


    while(cap.isOpened()):
      # Capture frame-by-frame
        ret, img = cap.read()
        if ret == True:
            img = cv2.resize(img, (0,0), fx=1.0, fy=1.0) 

            image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            image.flags.writeable = False

            results = mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5).process(image)

            image.flags.writeable = True
            image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

            # mp_drawing.draw_landmarks(image, results.face_landmarks, mp_holistic.FACE_CONNECTIONS, 
            #                      mp_drawing.DrawingSpec(color=(80,110,10), thickness=1, circle_radius=1),
            #                      mp_drawing.DrawingSpec(color=(80,256,121), thickness=1, circle_radius=1)
            #                      )

            mp_drawing.draw_landmarks(image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS, 
                                 mp_drawing.DrawingSpec(color=(80,22,10), thickness=2, circle_radius=4),
                                 mp_drawing.DrawingSpec(color=(80,44,121), thickness=2, circle_radius=2)
                                 )


            mp_drawing.draw_landmarks(image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS, 
                                 mp_drawing.DrawingSpec(color=(121,22,76), thickness=2, circle_radius=4),
                                 mp_drawing.DrawingSpec(color=(121,44,250), thickness=2, circle_radius=2)
                                 )
            mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS, 
                                 mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=4),
                                 mp_drawing.DrawingSpec(color=(245,66,230), thickness=2, circle_radius=2)
                                 )

            frame = cv2.imencode('.jpg', image)[1].tobytes()
            yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
            time.sleep(0)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        else: 
            break

@app.route('/video_feed')
def video_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(gen(),
                    mimetype='multipart/x-mixed-replace; boundary=frame')




if __name__=="__main__":
    app.run(debug=True)

推荐阅读