-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrowd_server.py
More file actions
97 lines (72 loc) · 2.68 KB
/
crowd_server.py
File metadata and controls
97 lines (72 loc) · 2.68 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
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 cv2
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
from typing import List
import ultralytics
import asyncio
import uvicorn
import websockets.exceptions
FAST_API_PORT = 5005
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
ultralytics.checks()
model = ultralytics.YOLO('yolov8x.pt')
print(f"YOLO model loaded !")
# Store connected WebSocket clients
connected_clients = []
class WebSocketConnection:
def __init__(self, websocket: WebSocket):
self.websocket = websocket
# async def send_people_count(websocket: WebSocket):
# await websocket.accept()
# connection = WebSocketConnection(websocket)
# connected_clients.append(connection)
# print("Connected")
# try:
# results = model("rtmp://127.0.0.1/mystream", stream=True, save=False)
# for result in results:
# people_in_frame = list(result.boxes.cls).count(0)
# print("People in frame", people_in_frame)
# await websocket.send_text(str(people_in_frame))
# print("Sent to frontend !")
# except websockets.exceptions.ConnectionClosedError:
# connected_clients.remove(connection)
# async def websocket_endpoint(websocket: WebSocket):
# print("Here")
# await send_people_count(websocket)
@app.websocket("/ws")
async def process_stream(websocket: WebSocket):
await websocket.accept()
await asyncio.sleep(0.1)
# Start the video stream
cap = cv2.VideoCapture('rtmp://127.0.0.1/mystream')
try:
while True:
# Capture a frame from the video stream
ret, frame = cap.read()
if not ret:
break
# Perform object detection on the frame
predictions = model(frame)
people_in_frame = list(predictions[0].boxes.cls).count(0)
# people_in_frame = sum(p['class'] == 'person' for p in predictions)
print("People in frame", people_in_frame)
# Send the predictions back to the client
# await websocket.send_text(str(people_in_frame))
# Send the number of people in the frame back to the client
await websocket.send_text(str(people_in_frame))
# Sleep briefly to allow other asyncio tasks to run
await asyncio.sleep(0.1)
except (websockets.exceptions.ConnectionClosedError, websockets.exceptions.ConnectionClosedOK):
print("Client disconnected")
cap.release()
# Close the video stream
cap.release()
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=FAST_API_PORT)