# 网络图像推流图传模板(OpenCV_Flask_高刷FPS方法) **Repository Path**: Mike_Zhou_Admin/CV_Web ## Basic Information - **Project Name**: 网络图像推流图传模板(OpenCV_Flask_高刷FPS方法) - **Description**: 网络图像推流项目模板(采用OpenCV和Flask来进行网络图像推流的低延迟高刷FPS方法) - **Primary Language**: Python - **License**: AGPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 2 - **Created**: 2024-01-25 - **Last Updated**: 2025-07-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: OpenCV, Flask, 网络图像推流 ## README 【Python】采用OpenCV和Flask来进行网络图像推流的低延迟高刷FPS方法(项目模板) 更新了在Linux图传文件夹,主要用于Linux设备上的摄像头优先检测方法,以及时间戳图传 原文: [【Python】采用OpenCV和Flask来进行网络图像推流的低延迟高刷FPS方法(项目模板)](https://blog.csdn.net/weixin_53403301/article/details/135856115) 前文: [【最简改进】基于OpenCV-Python+Flask的人脸检测网络摄像头图像输出(将本地图像转为网络URL地址,可输出带识别框的图像)](https://blog.csdn.net/weixin_53403301/article/details/124030457) @[toc] # 高刷方式 首先 在前文中 我们用OpenCV获取的图像转为bytes类型 然后发送给flask端网页进行图像推流 但由于OpenCV和网络部分都会占用系统资源 所以FPS不高 亦或是延迟较高 尤其是在树莓派等系统资源不够多的系统上运行时 延时非常明显 另外 还可能进行人脸识别、手势识别等功能 所以延迟进一步提高 但采用多线程的方式 可以在以上满足所有条件的情况下 将延时控制在150ms内 切FPS能达到30 ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/2d898110e40542b89cb6773f543ea808.png) 以上还是手机端浏览效果 服务端是由树莓派本身建立的 手机用的自带的浏览器 本身就有一定延迟 # 网络线程 网络线程其实就一个 就是flask的app.run函数 ```python def start_server(): web_app.run(host='0.0.0.0', port=local_post) thread_send = threading.Thread(target=start_server) thread_send.setDaemon(True) thread_send.start() ``` 而图像推流依靠以下函数: ```python def send_img(): global cam_img global pause_all_flag while pause_all_flag == 0: image = cv2.imencode('.jpg', cam_img)[1].tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + image + b'\r\n') if pause_all_flag == 1: break return @web_app.route('/video_feed') def video_feed(): return Response(send_img(), mimetype='multipart/x-mixed-replace; boundary=frame') ``` 另外 表单提取和推送也很简单 ```python @web_app.route('/', methods=['GET', 'POST']) def index(): global kill_all_flag global pause_all_flag global command_str if request.method == 'POST': command_str = str(request.form.get('WEB_COMMAND')) if kill_all_flag == 0 and pause_all_flag == 0: command_jugg() elif kill_all_flag == 0 and pause_all_flag == 1: print(command_str) if command_str == "继续程序": pause_all_flag = 0 print("Continue") else: print("程序已暂停,继续请按键") else: print("程序已终止,请重启程序") now_today = time.time() time_hour = time.localtime(now_today).tm_hour time_min = time.localtime(now_today).tm_min if time_hour < 10: time_hour = "0"+str(time_hour) if time_min < 10: time_min = "0"+str(time_min) local_time_str = str(time.localtime(now_today).tm_year)+"-"+str(time.localtime(now_today).tm_mon)+"-"+str(time.localtime(now_today).tm_mday)+" "+str(time_hour)+":"+str(time_min) data = { '当前时间:': [ local_time_str] } return render_template('index.html',data_dict=data) ``` 这三个部分其实是共用一个线程的 # 视频线程 视频线程的话 如果有检测、识别等部分 可以单独列一个线程 这里推荐使用最纯粹的视频获取线程作为一个单独的线程: 这里我是放在主线程里 ```python def img_main(): global pause_all_flag global kill_all_flag global cam_img while True: time.sleep(0.1) while pause_all_flag==0: cam_img = cv2.flip(cv2_cap.read()[1],1) cv2.imshow("http://"+local_ip+":"+str(local_post)+"/ (img: video_feed)",cam_img) # 展示图像 if pause_all_flag == 1: pause_all_flag = 1 print("暂停程序") cv2.destroyAllWindows() break if cv2.waitKey(10) == 27: # 通过esc键退出摄像 kill_all_flag = 1 pause_all_flag = 1 print("结束程序") cv2.destroyAllWindows() break if kill_all_flag == 1: break cv2_cap.release() print("全部退出") return ``` 几乎是感觉不到延迟的: ![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/160937e2423c4033b99ffb4d1054d8d8.png)