-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsamplewebclient.py
More file actions
34 lines (31 loc) · 1.09 KB
/
samplewebclient.py
File metadata and controls
34 lines (31 loc) · 1.09 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
import cv2
import websockets
import asyncio
import base64
import ssl
async def send_video(uri):
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("[Client] Error: Could not open camera.")
return
try:
# Use SSL context for wss
ssl_context = ssl._create_unverified_context()
async with websockets.connect(uri, ssl=ssl_context) as websocket:
print("[Client] Connected to server")
while True:
ret, frame = cap.read()
if not ret:
print("[Client] Error: Failed to capture frame.")
break
_, buffer = cv2.imencode('.jpg', frame)
jpg_as_text = base64.b64encode(buffer).decode('utf-8')
await websocket.send(jpg_as_text)
await asyncio.sleep(0.1) # Control frame rate
except Exception as e:
print(f"[Client] Error: {e}")
finally:
cap.release()
cameraId = 1 # Change this for different cameras
uri = f"wss://088d-27-60-165-45.ngrok-free.app/camera/{cameraId}"
asyncio.run(send_video(uri))