/Users/song/Code/flask_video_stream/main3.py
| from flask import Flask, Response, send_file |
| import cv2 |
| from PIL import Image |
| import io |
| |
| app = Flask(__name__) |
| |
| def generate_frames(): |
| cap = cv2.VideoCapture(0) |
| |
| if not cap.isOpened(): |
| print("ERROR!!Unable to open camera 1") |
| exit() |
| |
| while True: |
| ret, img = cap.read() |
| |
| |
| ret, jpeg = cv2.imencode('.jpg', img) |
| |
| |
| yield (b'--frame\r\n' |
| b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n\r\n') |
| |
| @app.route('/video_feed') |
| def video_feed(): |
| return Response(generate_frames(), |
| mimetype='multipart/x-mixed-replace; boundary=frame') |
| |
| @app.route('/capture') |
| def capture(): |
| cap = cv2.VideoCapture(1) |
| |
| if not cap.isOpened(): |
| return "ERROR!!Unable to open camera 2" |
| |
| ret, img = cap.read() |
| |
| cap.release() |
| |
| |
| img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| pil_img = Image.fromarray(img) |
| |
| |
| img_io = io.BytesIO() |
| pil_img.save(img_io, 'JPEG') |
| img_io.seek(0) |
| |
| |
| return send_file(img_io, mimetype='image/jpeg') |
| |
| if __name__ == '__main__': |
| app.run() |
| |
/Users/song/Code/flask_video_stream/main6.py
| import time |
| from flask import Flask, Response, send_file |
| import cv2 |
| from PIL import Image |
| import io |
| import threading |
| |
| app = Flask(__name__) |
| |
| |
| close_video = False |
| |
| |
| cond = threading.Condition() |
| |
| def generate_frames(): |
| global close_video, cond |
| |
| cap = cv2.VideoCapture(0) |
| |
| if not cap.isOpened(): |
| print("ERROR!!Unable to open camera 1") |
| exit() |
| |
| while True: |
| with cond: |
| |
| if close_video: |
| cap.release() |
| cond.wait() |
| cap.open(0) |
| |
| ret, img = cap.read() |
| |
| |
| ret, jpeg = cv2.imencode('.jpg', img) |
| |
| |
| yield (b'--frame\r\n' |
| b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n\r\n') |
| |
| @app.route('/video_feed') |
| def video_feed(): |
| return Response(generate_frames(), |
| mimetype='multipart/x-mixed-replace; boundary=frame') |
| |
| @app.route('/capture') |
| def capture(): |
| global close_video, cond |
| |
| with cond: |
| |
| close_video = True |
| |
| cap = cv2.VideoCapture(0) |
| time.sleep(3) |
| |
| if not cap.isOpened(): |
| return "ERROR!!Unable to open camera 2" |
| |
| ret, img = cap.read() |
| |
| cap.release() |
| |
| |
| img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| pil_img = Image.fromarray(img) |
| |
| |
| img_io = io.BytesIO() |
| pil_img.save(img_io, 'JPEG') |
| img_io.seek(0) |
| |
| with cond: |
| |
| close_video = False |
| |
| cond.notify_all() |
| |
| |
| return send_file(img_io, mimetype='image/jpeg') |
| |
| if __name__ == '__main__': |
| app.run() |
| |
/Users/song/Code/flask_video_stream/main2.py
| from flask import Flask, send_file |
| import cv2 |
| import numpy as np |
| from PIL import Image |
| import io |
| import time |
| |
| app = Flask(__name__) |
| |
| @app.route('/capture') |
| def capture(): |
| cap = cv2.VideoCapture(0) |
| time.sleep(3) |
| |
| if not cap.isOpened(): |
| return "ERROR!!Unable to open camera" |
| |
| ret, img = cap.read() |
| |
| cap.release() |
| |
| |
| img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| pil_img = Image.fromarray(img) |
| |
| |
| img_io = io.BytesIO() |
| pil_img.save(img_io, 'JPEG') |
| img_io.seek(0) |
| |
| |
| return send_file(img_io, mimetype='image/jpeg') |
| |
| if __name__ == '__main__': |
| app.run(debug=True) |
| |
/Users/song/Code/flask_video_stream/main5.py
| import time |
| from flask import Flask, Response, send_file |
| import cv2 |
| from PIL import Image |
| import io |
| import threading |
| |
| app = Flask(__name__) |
| |
| |
| pause_video = False |
| |
| |
| cond = threading.Condition() |
| |
| def generate_frames(): |
| global pause_video, cond |
| |
| cap = cv2.VideoCapture(0) |
| |
| if not cap.isOpened(): |
| print("ERROR!!Unable to open camera 1") |
| exit() |
| |
| while True: |
| with cond: |
| |
| while pause_video: |
| cond.wait() |
| |
| ret, img = cap.read() |
| |
| |
| ret, jpeg = cv2.imencode('.jpg', img) |
| |
| |
| yield (b'--frame\r\n' |
| b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n\r\n') |
| |
| @app.route('/video_feed') |
| def video_feed(): |
| return Response(generate_frames(), |
| mimetype='multipart/x-mixed-replace; boundary=frame') |
| |
| @app.route('/capture') |
| def capture(): |
| global pause_video, cond |
| |
| with cond: |
| |
| pause_video = True |
| |
| cap = cv2.VideoCapture(0) |
| time.sleep(3) |
| |
| if not cap.isOpened(): |
| return "ERROR!!Unable to open camera 2" |
| |
| ret, img = cap.read() |
| |
| |
| cap.release() |
| |
| |
| img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| pil_img = Image.fromarray(img) |
| |
| |
| img_io = io.BytesIO() |
| pil_img.save(img_io, 'JPEG') |
| img_io.seek(0) |
| |
| with cond: |
| |
| pause_video = False |
| |
| cond.notify_all() |
| |
| |
| return send_file(img_io, mimetype='image/jpeg') |
| |
| if __name__ == '__main__': |
| app.run(debug=True) |
| |
/Users/song/Code/flask_video_stream/main.py
| from flask import Flask |
| import cv2 |
| |
| app = Flask(__name__) |
| |
| @app.route('/capture') |
| def capture(): |
| cap = cv2.VideoCapture(0) |
| |
| if not cap.isOpened(): |
| return "ERROR!!Unable to open camera" |
| |
| ret, img = cap.read() |
| |
| cv2.imwrite("photo.jpg", img) |
| |
| cap.release() |
| |
| return "Capture success!" |
| |
| if __name__ == '__main__': |
| app.run() |
| |
/Users/song/Code/flask_video_stream/main4.py
| from flask import Flask, Response, send_file |
| import cv2 |
| from PIL import Image |
| import io |
| |
| app = Flask(__name__) |
| |
| |
| show_local_image = False |
| |
| def generate_frames(): |
| global show_local_image |
| |
| |
| |
| |
| |
| |
| |
| while True: |
| if show_local_image: |
| |
| img = cv2.imread('photo.jpg') |
| else: |
| |
| img = cv2.imread('photo2.jpg') |
| |
| |
| |
| ret, jpeg = cv2.imencode('.jpg', img) |
| |
| |
| yield (b'--frame\r\n' |
| b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n\r\n') |
| |
| @app.route('/video_feed') |
| def video_feed(): |
| return Response(generate_frames(), |
| mimetype='multipart/x-mixed-replace; boundary=frame') |
| |
| @app.route('/capture') |
| def capture(): |
| global show_local_image |
| |
| |
| show_local_image = True |
| |
| cap = cv2.VideoCapture(0) |
| |
| if not cap.isOpened(): |
| return "ERROR!!Unable to open camera 2" |
| |
| ret, img = cap.read() |
| |
| cap.release() |
| |
| |
| img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| pil_img = Image.fromarray(img) |
| |
| |
| img_io = io.BytesIO() |
| pil_img.save(img_io, 'JPEG') |
| img_io.seek(0) |
| |
| |
| show_local_image = False |
| |
| |
| return send_file(img_io, mimetype='image/jpeg') |
| |
| if __name__ == '__main__': |
| app.run() |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战