1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| import _thread import queue import time
import cv2 from flask import Flask, Response
app = Flask(__name__)
max_size = 3 q1 = queue.Queue(maxsize=max_size) q2 = queue.Queue(maxsize=max_size)
open_flag = 1
def open_and_show(ip_camera_url, title): global open_flag
cap = cv2.VideoCapture(ip_camera_url)
while open_flag == 1: ret, frame = cap.read() if title == "1": if q1.full(): q1.get() q1.put(frame) elif title == "2": if q2.full(): q2.get() q2.put(frame)
cap.release()
def gen_frames():
fourcc = cv2.VideoWriter_fourcc(*"mp4v") out = cv2.VideoWriter(f'{int(time.time())}.mp4', fourcc, 20, (1080*2, 720))
while True: if q1.empty() or q2.empty(): continue frame1 = q1.get() frame2 = q2.get()
frame1 = cv2.resize(frame1, (1080, 720)) frame2 = cv2.resize(frame2, (1080, 720))
frames = [frame1, frame2] combined_frame = cv2.hconcat(frames)
out.write(combined_frame)
ret, buffer = cv2.imencode('.jpg', combined_frame) frame = buffer.tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
if cv2.waitKey(1) == ord('q'): break
@app.route("/") def index(): global open_flag
open_flag = 0 time.sleep(1) open_flag = 1 try: _thread.start_new_thread(open_and_show, ("rtsp://admin:qwer1234!.@192.168.1.65:554", "1",)) _thread.start_new_thread(open_and_show, ("rtsp://admin:qwer1234!.@192.168.1.66:554", "2",))
except: print("Error: 无法启动线程")
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == "__main__": app.run( port=5000, host="0.0.0.0" )
|