flask_video_stream

发布时间 2023-05-05 23:55:00作者: 虎虎生威啊

/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() #从第一个摄像头捕获图像

        # 将 OpenCV 图像转换为 JPEG 图像
        ret, jpeg = cv2.imencode('.jpg', img)

        # 返回 JPEG 图像数据
        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() #释放第二个相机捕获对象

    # 将 OpenCV 图像转换为 PIL 图像
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    pil_img = Image.fromarray(img)

    # 将 PIL 图像保存到内存中
    img_io = io.BytesIO()
    pil_img.save(img_io, 'JPEG')
    img_io.seek(0)

    # 使用 send_file 函数返回图像
    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:
            # 如果 close_video 为 True,则释放摄像头并等待
            if close_video:
                cap.release()
                cond.wait()
                cap.open(0)

        ret, img = cap.read() #从第一个摄像头捕获图像

        # 将 OpenCV 图像转换为 JPEG 图像
        ret, jpeg = cv2.imencode('.jpg', img)

        # 返回 JPEG 图像数据
        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,使视频流关闭
        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() #释放第二个相机捕获对象

    # 将 OpenCV 图像转换为 PIL 图像
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    pil_img = Image.fromarray(img)

    # 将 PIL 图像保存到内存中
    img_io = io.BytesIO()
    pil_img.save(img_io, 'JPEG')
    img_io.seek(0)

    with cond:
        # 设置 close_video 为 False,使视频流重新打开
        close_video = False
        # 唤醒等待的线程
        cond.notify_all()

    # 使用 send_file 函数返回图像
    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() #释放相机捕获对象

    # 将 OpenCV 图像转换为 PIL 图像
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    pil_img = Image.fromarray(img)

    # 将 PIL 图像保存到内存中
    img_io = io.BytesIO()
    pil_img.save(img_io, 'JPEG')
    img_io.seek(0)

    # 使用 send_file 函数返回图像
    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:
            # 如果 pause_video 为 True,则等待
            while pause_video:
                cond.wait()

        ret, img = cap.read() #从第一个摄像头捕获图像

        # 将 OpenCV 图像转换为 JPEG 图像
        ret, jpeg = cv2.imencode('.jpg', img)

        # 返回 JPEG 图像数据
        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,使视频流暂停
        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() #释放第二个相机捕获对象

    # 将 OpenCV 图像转换为 PIL 图像
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    pil_img = Image.fromarray(img)

    # 将 PIL 图像保存到内存中
    img_io = io.BytesIO()
    pil_img.save(img_io, 'JPEG')
    img_io.seek(0)

    with cond:
        # 设置 pause_video 为 False,使视频流恢复
        pause_video = False
        # 唤醒等待的线程
        cond.notify_all()

    # 使用 send_file 函数返回图像
    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

    # cap = cv2.VideoCapture(0) #打开第一个摄像头

    # if not cap.isOpened():
    #     print("ERROR!!Unable to open camera 1")
    #     exit()

    while True:
        if show_local_image:
            # 读取本地图片
            img = cv2.imread('photo.jpg')
        else:
            # 从第一个摄像头捕获图像
            img = cv2.imread('photo2.jpg')
            # ret, img = cap.read()

        # 将 OpenCV 图像转换为 JPEG 图像
        ret, jpeg = cv2.imencode('.jpg', img)

        # 返回 JPEG 图像数据
        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,使视频流返回本地图片
    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() #释放第二个相机捕获对象

    # 将 OpenCV 图像转换为 PIL 图像
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    pil_img = Image.fromarray(img)

    # 将 PIL 图像保存到内存中
    img_io = io.BytesIO()
    pil_img.save(img_io, 'JPEG')
    img_io.seek(0)

    # 设置 show_local_image 为 False,使视频流恢复原来的效果
    show_local_image = False

    # 使用 send_file 函数返回图像
    return send_file(img_io, mimetype='image/jpeg')

if __name__ == '__main__':
    app.run()