python - 使用Flask的浏览器html中的OpenCV非常慢且延迟
问题描述
当我尝试使用模型媒体管道将模型 .pkl 上传到带有各种细节点的烧瓶中时,会导致大量视频延迟和延迟,有什么办法可以解决这些问题吗?(但是当我尝试在 Jupyter notebook 中运行 open CV 时,没有这些问题。)
解决方案
这是我的代码
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)
推荐阅读
- arangodb - Arangodb 插入多行并使用 UPSERT
- javascript - Wordpress 错误,javascript 未加载到网站页面 jQuery 未声明
- excel - excel vba - sumif 和 id 字段的平均值
- atlassian-sourcetree - 如何发现丢失的 SourceTree 存储库的原始路径
- javascript - Node.js 如何使用 this 和调用函数
- r - 在没有ggplot2的R上绘制世界地图
- javascript - 如何将外部库 RainyDay.js 添加到我的项目中?
- c# - 如何在 Visual Studio 2017 解决方案中设置文件夹以在那里搜索 DLL?
- java - iPad 上的 Codenameone 应用程序在每隔一段时间从睡眠状态开始时使用错误的纵横比
- javascript - 为什么 JQuery 点击事件只工作一次?