-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
186 lines (156 loc) · 6.09 KB
/
main.py
File metadata and controls
186 lines (156 loc) · 6.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import base64
import json
import os
import time
import torch
from ultralytics import YOLO
import cv2
from dotenv import load_dotenv, find_dotenv
import threading
from Camera.CameraStream import CameraStream
from Logger import Logger
from MQTTClient import mqtt_client
MAX_FRAME_COUNT = 1000000000000000000
# --- Load environment variables ---
load_dotenv(dotenv_path=find_dotenv(".env"), override=False)
local_env = find_dotenv(".env.local")
if local_env:
load_dotenv(dotenv_path=local_env, override=True)
# --- Initialize YOLO model ---
model = YOLO("./models/yolov11_cozmo.pt")
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model.to(device)
Logger.info(f"[YOLO] Model loaded on {device}")
# Start threaded camera stream
stream = CameraStream(0)
fps_limit = 12
frame_time = 1.0 / fps_limit
def init_broker():
mqtt_client.set_config(
broker=os.environ.get("MQTT_BROKER", "localhost"),
port=int(os.environ.get("MQTT_PORT", 1884)),
username=os.environ.get("MQTT_USERNAME", 'username'),
password=os.environ.get("MQTT_PASSWORD", 'password'),
)
mqtt_client.start()
mqtt_client.wait_for_broker_ready(timeout=10)
def publish_annotated_image(img, topic="cozmo/camera_top/annotated"):
img_bytes = cv2.imencode('.jpg', img)[1].tobytes()
now = str(time.time())
object_to_publish = {
"stamp": now,
"image": base64.b64encode(img_bytes).decode('utf-8')
}
mqtt_client.publish(topic, json.dumps(object_to_publish))
def publish_motion_event(is_moving, topic="cozmo/camera_top/motion"):
event = {
"state": is_moving,
"stamp": str(time.time()),
}
mqtt_client.publish(topic, json.dumps(event))
def publish_robot_event(event_type, topic="cozmo/camera_top/events"):
event = {
"type": event_type,
"stamp": str(time.time()),
}
mqtt_client.publish(topic, json.dumps(event))
previous_position = None
previous_state = None
state_history = []
cozmo_found = False
missing_counter = 0
def visual_cue_handler():
global previous_position, previous_state, state_history
global cozmo_found, missing_counter
while True:
start_time = time.time()
ret, frame = stream.read()
if not ret or frame is None:
Logger.error("[YOLO] Camera disconnected or no frame.")
break
results = model.track(frame, conf=0.65, persist=True,
verbose=False,
tracker="bytetrack.yaml")
main_cozmo_detected = False
for r in results:
detected_objects = []
for box in r.boxes:
cls_id = int(box.cls)
conf = float(box.conf)
label = model.names[cls_id]
x1, y1, x2, y2 = box.xyxy[0].tolist()
detected_objects.append({
"label": label,
"confidence": round(conf, 3),
"bbox": {
"x1": int(x1),
"y1": int(y1),
"x2": int(x2),
"y2": int(y2)
}
})
Logger.debug(f"[YOLO] Detections: {json.dumps(detected_objects)}")
if detected_objects:
# --- COZMO DETECTION FOUND ---
main_cozmo = max(detected_objects, key=lambda x: x['confidence'])
main_cozmo_detected = True
x1 = main_cozmo["bbox"]["x1"]
y1 = main_cozmo["bbox"]["y1"]
x2 = main_cozmo["bbox"]["x2"]
y2 = main_cozmo["bbox"]["y2"]
center_x = (x1 + x2) // 2
center_y = (y1 + y2) // 2
current_coordinates = {"x": center_x, "y": center_y}
# Movement check (threshold=2)
move_threshold = 2
if previous_position is not None:
dx = current_coordinates["x"] - previous_position["x"]
dy = current_coordinates["y"] - previous_position["y"]
dist = (dx ** 2 + dy ** 2) ** 0.5
is_moving = dist > move_threshold
else:
is_moving = True # First frame, assume moving
previous_position = current_coordinates
# State smoothing: require 2 consecutive frames to trigger state change
state_history.append(is_moving)
if len(state_history) > 2:
state_history.pop(0)
# Only trigger state change if last 2 frames agree and state is different
if previous_state is not is_moving:
Logger.info(f"[YOLO] Coordinates: {json.dumps(current_coordinates)}, Moving: {is_moving}")
publish_motion_event(is_moving)
previous_state = is_moving
# Publish the annotated image using helper
publish_annotated_image(r.plot())
# --- COZMO FOUND/LOST LOGIC ---
if main_cozmo_detected:
if not cozmo_found:
cozmo_found = True
missing_counter = 0
publish_robot_event("robot_found")
Logger.info("[YOLO] Robot found, published robot_found event")
else:
missing_counter = 0 # reset
else:
if cozmo_found:
missing_counter += 1
if missing_counter >= fps_limit:
cozmo_found = False
publish_robot_event("robot_lost")
Logger.info("[YOLO] Robot lost, published robot_lost event")
else:
missing_counter = 0 # stays zero
elapsed = time.time() - start_time
if elapsed < frame_time:
time.sleep(frame_time - elapsed)
if __name__ == '__main__':
init_broker()
Logger.info("[Main] Starting visual cue service...")
threading.Thread(target=visual_cue_handler, daemon=True).start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
Logger.info("[Main] Shutting down")
mqtt_client.stop()
stream.stop()