-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
98 lines (77 loc) · 2.89 KB
/
main.py
File metadata and controls
98 lines (77 loc) · 2.89 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
import cv2
import time
from camera.camera import Camera
from camera.tracking import Tracker
# Constants
HFOV, VFOV = 60.0, 45.0
CROSSHAIR_OFFSET_X, CROSSHAIR_OFFSET_Y = 0, 0
# Use a dict for global settings to ensure real-time access in callbacks
settings = {"size": 100, "last_center": None} # Stores (x, y) of the target
camera = Camera(src=0)
tracker = Tracker(tracker_type="CSRT")
camera.start()
time.sleep(2)
def click_event(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
current_frame = param["frame"]
# Define ROI centered on click
s = settings["size"]
roi = (int(x - s // 2), int(y - s // 2), s, s)
tracker.init(current_frame, roi)
cv2.namedWindow("Camera Frame")
while True:
frame = camera.read()
if frame is None:
continue
h, w = frame.shape[:2]
marker_x, marker_y = (w // 2) + CROSSHAIR_OFFSET_X, (h // 2) - CROSSHAIR_OFFSET_Y
# Update mouse callback with latest frame
cv2.setMouseCallback("Camera Frame", click_event, param={"frame": frame})
if tracker.initialized:
success, bbox = tracker.track(frame)
if success:
tx, ty, tw, th = [int(v) for v in bbox]
settings["last_center"] = (tx + tw // 2, ty + th // 2)
# Display target info
azimuth = ((settings["last_center"][0] - marker_x) / w) * HFOV
elevation = ((settings["last_center"][1] - marker_y) / h) * VFOV
cv2.rectangle(frame, (tx, ty), (tx + tw, ty + th), (255, 0, 0), 2)
cv2.line(
frame,
(marker_x, marker_y),
(settings["last_center"][0], settings["last_center"][1]),
(0, 255, 0),
1,
)
cv2.putText(
frame,
f"Az: {azimuth:.1f} El: {elevation:.1f}",
(10, 60),
0,
0.6,
(255, 255, 255),
2,
)
# UI Elements
cv2.drawMarker(frame, (marker_x, marker_y), (0, 255, 255), cv2.MARKER_CROSS, 20, 2)
cv2.putText(
frame, f"Tracker Size: {settings['size']}px", (10, 30), 0, 0.6, (0, 255, 255), 2
)
cv2.imshow("Camera Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
# Handle Resize (Up: 82, Down: 84 on many systems, or use 'w'/'s')
if key in [82, ord("w"), 84, ord("s")]:
if key in [82, ord("w")]:
settings["size"] += 10
else:
settings["size"] = max(20, settings["size"] - 10)
# REAL-TIME UPDATE: If currently tracking, re-init with new size immediately
if tracker.initialized and settings["last_center"]:
cx, cy = settings["last_center"]
s = settings["size"]
new_roi = (int(cx - s // 2), int(cy - s // 2), s, s)
tracker.init(frame, new_roi)
camera.stop()
cv2.destroyAllWindows()