forked from ByteBangshu/HackAI2026
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
64 lines (51 loc) · 2.05 KB
/
Copy pathscript.py
File metadata and controls
64 lines (51 loc) · 2.05 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
# laptop_sender.py
import asyncio
import cv2
import websockets
ESP32_IP = "192.168.X.X" # ← Replace with your ESP32's IP (printed in Serial Monitor)
ESP32_PORT = 8765
TARGET_FPS = 10 # Lower = less load on ESP32
JPEG_QUALITY = 50 # 0-100, lower = smaller frames
async def stream_webcam():
uri = f"ws://{ESP32_IP}:{ESP32_PORT}"
cap = cv2.VideoCapture(0) # 0 = default webcam
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
if not cap.isOpened():
print("ERROR: Could not open webcam.")
return
print(f"Connecting to ESP32 at {uri} ...")
async with websockets.connect(uri) as ws:
print("Connected! Streaming frames. Press Ctrl+C to stop.")
frame_interval = 1.0 / TARGET_FPS
while True:
loop_start = asyncio.get_event_loop().time()
ret, frame = cap.read()
if not ret:
print("ERROR: Failed to capture frame.")
break
# Encode frame as JPEG
ok, buf = cv2.imencode(".jpg", frame,
[cv2.IMWRITE_JPEG_QUALITY, JPEG_QUALITY])
if not ok:
continue
# Send binary JPEG to ESP32
await ws.send(buf.tobytes())
# Show local preview (optional, close window to stop)
cv2.imshow("Sending to ESP32", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# Wait for ACK (optional — comment out if you want max speed)
try:
ack = await asyncio.wait_for(ws.recv(), timeout=1.0)
except asyncio.TimeoutError:
pass # Continue even if no ACK
# Pace to target FPS
elapsed = asyncio.get_event_loop().time() - loop_start
sleep_time = frame_interval - elapsed
if sleep_time > 0:
await asyncio.sleep(sleep_time)
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
asyncio.run(stream_webcam())