-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
50 lines (39 loc) · 1.15 KB
/
app.py
File metadata and controls
50 lines (39 loc) · 1.15 KB
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
from asyncio import sleep
from fastapi import FastAPI, WebSocket
from fastapi.responses import HTMLResponse
from video_processor import VideoProcessor
app = FastAPI()
FRAMES_DELAY = 0.04
html = """
<!DOCTYPE html>
<html>
<head>
<title>Chat</title>
</head>
<body>
<img id="videoElement" src=""></video>
<script>
const videoElement = document.getElementById('videoElement');
const socket = new WebSocket('ws://192.168.1.3/video');
socket.onmessage = function(event) {
videoElement.src = 'data:image/jpeg;base64,' + event.data;
};
</script>
</body>
</html>
"""
@app.websocket("/video")
async def video_stream(websocket: WebSocket):
video = VideoProcessor()
await websocket.accept()
for status, frame in video.get_frame():
frame_base64 = video.process_frame(frame)
if not status:
await websocket.send_text("No video")
await websocket.send_text(frame_base64)
await sleep(
FRAMES_DELAY
) # Optimize streaming and needed for accepting new clients
@app.get("/")
def index():
return HTMLResponse(html)