-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.py
60 lines (49 loc) · 1.95 KB
/
stream.py
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
from flask import Flask, Response
import cv2
import json
import copy
import sgbm
from detection import YOLO, YoloArgs
yolo = YOLO(YoloArgs())
app = Flask(__name__)
camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
camera.set(cv2.CAP_PROP_FPS, 30)
width = int(camera.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(f"当前分辨率: {width}x{height}")
def get_frames():
with open("parameters.json") as json_file:
params = json.load(json_file)
mappings, sgbm_configure, Q = sgbm.sgbm_setup(params)
i = 0
mean_values = None # initialize
while True:
success, frame = camera.read()
if not success:
break
else:
left_frame = frame[:, :640]
right_frame = frame[:, 640:]
pre_process_frame = copy.deepcopy(right_frame)
# obtain the yolo image
boxes, scores, original_frame = yolo.run(pre_process_frame)
if boxes is not None:
# obtain the distance image
if i & 5 == 0 or mean_values is None: # reduce the FPS of distance measuring
mean_values = sgbm.sgbm_run(mappings, sgbm_configure, (left_frame, right_frame), Q, boxes)
yolo_frame = yolo.draw_detections(boxes, scores, original_frame, mean_values)
else:
yolo_frame = right_frame[:]
_, buffer = cv2.imencode('.jpg', yolo_frame)
frame_bytes = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n')
i += 1
@app.route('/video_feed')
def video_feed():
return Response(get_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000) # Replace with your desired port