-
Notifications
You must be signed in to change notification settings - Fork 1
/
depthAI_streaming.py
98 lines (78 loc) · 2.55 KB
/
depthAI_streaming.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
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
98
import cv2
import depthai
import uvicorn
import numpy as np
from fastapi import FastAPI, Response, Request
from fastapi.responses import StreamingResponse
global streaming
streaming = False
pipeline = depthai.Pipeline()
cam_rgb = pipeline.create(depthai.node.ColorCamera)
cam_rgb.setPreviewSize(640, 640)
cam_rgb.setInterleaved(False)
#create output
xout_rgb = pipeline.create(depthai.node.XLinkOut)
xout_rgb.setStreamName("rgb")
cam_rgb.preview.link(xout_rgb.input)
app = FastAPI()
streaming = False
def generate_frames():
# if pipeline:
streaming = True
global device
with depthai.Device(pipeline) as device:
q_rgb = device.getOutputQueue("rgb")
frame = None
while streaming:
in_rgb = q_rgb.tryGet()
if in_rgb is not None:
frame = in_rgb.getCvFrame()
if frame is not None:
global img
img = frame.copy()
ret, buffer = cv2.imencode('.jpg', frame)
frame_encoded = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame_encoded + b'\r\n')
# yield img
@app.get('/')
async def index():
return {"message": "Welcome to the webcam streaming application!"}
@app.get('/video_feed')
async def video_feed():
global streaming
streaming = True
return StreamingResponse(generate_frames(),
media_type='multipart/x-mixed-replace; boundary=frame')
@app.get('/stop_stream')
async def stop_stream():
global streaming, device
streaming = False
if device:
device.close()
device = None
return {"message": "Streaming stopped successfully."}
@app.get('/restart_stream')
async def restart_stream():
global streaming
streaming = True
return {"message": "Streaming restarted successfully."}
import os
@app.get('/save_frame')
async def save_frame(filename: str):
# Specify the file path to save the image
file_path = os.path.join("your_path", filename)
# Save the image to the specified file path
cv2.imwrite(file_path, img)
return {"message": f"Image saved to {file_path}"}
####demo using @app.post to receive request from client(web browser)
@app.post('/demo')
async def demo_getvalue(value:str):
global demo_value
demo_value = value
return demo_value
@app.get('/rundemo')
async def rundemo():
return {"message": "the value was received as value = {}".format(demo_value)}
if __name__ == '__main__':
uvicorn.run(app, host='localhost', port=8000)