-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraLomba_Angelo_MATE.py
More file actions
168 lines (117 loc) · 4.93 KB
/
Copy pathCameraLomba_Angelo_MATE.py
File metadata and controls
168 lines (117 loc) · 4.93 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
import subprocess as sp
import numpy as np
import cv2
import os
import time
from ultralytics import YOLO
# ================= CONFIG =================
FFMPEG_BIN = "ffmpeg"
SDP_FILE = "HDExplorer.sdp"
WIDTH = 640
HEIGHT = 360
YOLO_MODEL = "KepitingAlaska.pt"
YOLO_CONF = 0.35
DETECT_EVERY_N_FRAMES = 3
# =========================================
frame_size = WIDTH * HEIGHT * 3
def start_ffmpeg_proc(sdp_file, width, height):
cmd = [
FFMPEG_BIN,
"-protocol_whitelist", "file,udp,rtp",
"-i", sdp_file,
"-fflags", "nobuffer",
"-flags", "low_delay",
"-an", "-sn",
"-vf", f"scale={width}:{height}",
"-f", "rawvideo",
"-pix_fmt", "bgr24",
"pipe:1"
]
return sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.DEVNULL, bufsize=frame_size)
def draw_hud(frame, mode_idx, fps, current_count, max_count, detected_names):
h, w = frame.shape[:2]
modes = ["OFF", "YOLO 1", "YOLO 2"]
current_mode_name = modes[mode_idx]
overlay = frame.copy()
cv2.rectangle(overlay, (0, 0), (320, 110), (0, 0, 0), -1)
cv2.addWeighted(overlay, 0.5, frame, 0.5, 0, frame)
cv2.putText(frame, f"MODE: {current_mode_name}", (15, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2)
cv2.putText(frame, f"FPS: {fps:.1f}", (15, 55), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
if mode_idx > 0: # Jika mode bukan OFF
nama_objek = "kepiting" if detected_names else "-"
cv2.putText(frame, f"Terdeteksi: {nama_objek}", (15, 80),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
limit_val = 2 if mode_idx == 1 else 10
# Warna berubah hijau jika target tercapai
color = (0, 200, 0) if max_count >= limit_val else (0, 165, 255)
cv2.rectangle(frame, (w-230, 10), (w-10, 75), color, -1)
cv2.putText(frame, "TARGET", (w-200, 35),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)
cv2.putText(frame, f"{max_count}", (w-135, 68),
cv2.FONT_HERSHEY_SIMPLEX, 1.1, (255, 255, 255), 3)
cv2.putText(frame, "R: Reset | Y: Switch Mode", (w-210, 95),
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1)
def main():
if not os.path.exists(SDP_FILE):
print(f"Error: {SDP_FILE} tidak ditemukan.")
return
print("[INFO] Loading YOLO model...")
model = YOLO(YOLO_MODEL)
print("[INFO] Starting FFmpeg Stream (SDP)...")
proc = start_ffmpeg_proc(SDP_FILE, WIDTH, HEIGHT)
mode_idx = 1 # Default ke YOLO 1
frame_count = 0
last_boxes = []
last_detect_names = []
current_crab_count = 0
max_crab_count = 0
fps_last_time = time.time()
fps_counter = 0
fps = 0.0
while True:
raw = proc.stdout.read(frame_size)
if not raw or len(raw) != frame_size:
print("[WARN] Stream terputus atau frame tidak lengkap.")
break
frame = np.frombuffer(raw, dtype=np.uint8).reshape((HEIGHT, WIDTH, 3)).copy()
frame_count += 1
if mode_idx > 0:
if frame_count % DETECT_EVERY_N_FRAMES == 0:
results = model.predict(frame, imgsz=320, conf=YOLO_CONF, verbose=False)[0]
new_boxes = []
for box in results.boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
new_boxes.append((x1, y1, x2, y2))
last_boxes = new_boxes
last_detect_names = ["kepiting"] if new_boxes else []
live_actual = len(new_boxes)
current_crab_count = live_actual
limit = 2 if mode_idx == 1 else 10
if live_actual > max_crab_count:
max_crab_count = min(live_actual, limit)
for (x1, y1, x2, y2) in last_boxes:
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
fps_counter += 1
if time.time() - fps_last_time >= 1.0:
fps = fps_counter / (time.time() - fps_last_time)
fps_last_time = time.time()
fps_counter = 0
draw_hud(frame, mode_idx, fps, current_crab_count, max_crab_count, last_detect_names)
cv2.imshow("ROV MONITOR - YOLO MODE", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
elif key == ord("y"):
mode_idx = (mode_idx + 1) % 3
max_crab_count = 0
print(f"[MODE] Ganti ke: {mode_idx}")
elif key == ord("r"):
max_crab_count = 0
print("[INFO] Counter Reset.")
elif key == ord("s"):
cv2.imwrite(f"rov_capture_{int(time.time())}.png", frame)
proc.terminate()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()