-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestrain.py
More file actions
239 lines (182 loc) · 5.46 KB
/
Copy pathtestrain.py
File metadata and controls
239 lines (182 loc) · 5.46 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from ultralytics import YOLO
import cv2
import time
# =========================================================
# CONFIGURATION
# =========================================================
MODEL_PATH = r"D:\Downloads\GUI-ROV-main (1)\GUI-ROV-main\best_new.pt"
CAMERA_INDEX = 0
CONFIDENCE = 0.40
IMG_SIZE = 640
# =========================================================
# LOAD YOLO MODEL
# =========================================================
print("Loading YOLO model...")
model = YOLO(MODEL_PATH)
print("Model berhasil dimuat.")
# =========================================================
# OPEN LAPTOP CAMERA
# =========================================================
cap = cv2.VideoCapture(CAMERA_INDEX)
# Resolusi kamera laptop
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
if not cap.isOpened():
print("❌ Kamera laptop tidak dapat dibuka.")
print("Coba ganti CAMERA_INDEX dari 0 menjadi 1.")
exit()
print("✅ Kamera laptop aktif.")
print("Tekan Q untuk keluar.")
# =========================================================
# FPS
# =========================================================
prev_time = time.time()
# =========================================================
# MAIN LOOP
# =========================================================
while True:
ret, frame = cap.read()
if not ret:
print("❌ Tidak dapat membaca kamera.")
break
# -----------------------------------------------------
# YOLO DETECTION
# -----------------------------------------------------
results = model.predict(
source=frame,
conf=CONFIDENCE,
imgsz=IMG_SIZE,
device="cpu",
verbose=False
)
# Frame hasil deteksi
annotated = results[0].plot()
# -----------------------------------------------------
# CAMERA CENTER
# -----------------------------------------------------
height, width = annotated.shape[:2]
center_x = width // 2
center_y = height // 2
# Crosshair tengah kamera
cv2.line(
annotated,
(center_x - 20, center_y),
(center_x + 20, center_y),
(255, 255, 0),
2
)
cv2.line(
annotated,
(center_x, center_y - 20),
(center_x, center_y + 20),
(255, 255, 0),
2
)
# -----------------------------------------------------
# INFORMATION DETECTION
# -----------------------------------------------------
for result in results:
for box in result.boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
confidence = float(box.conf[0])
class_id = int(box.cls[0])
class_name = model.names[class_id]
# Titik tengah objek
object_x = int((x1 + x2) / 2)
object_y = int((y1 + y2) / 2)
# Error terhadap pusat kamera
error_x = object_x - center_x
error_y = object_y - center_y
# Titik tengah objek
cv2.circle(
annotated,
(object_x, object_y),
5,
(0, 0, 255),
-1
)
# Garis dari pusat kamera ke objek
cv2.line(
annotated,
(center_x, center_y),
(object_x, object_y),
(255, 0, 255),
1
)
# Informasi objek
info = f"{class_name} {confidence:.2f}"
cv2.putText(
annotated,
info,
(x1, max(y1 - 10, 20)),
cv2.FONT_HERSHEY_SIMPLEX,
0.6,
(0, 255, 0),
2
)
# Koordinat
coordinate = f"X:{object_x} Y:{object_y}"
cv2.putText(
annotated,
coordinate,
(x1, y2 + 20),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(0, 255, 255),
2
)
# Error
error_text = f"EX:{error_x} EY:{error_y}"
cv2.putText(
annotated,
error_text,
(x1, y2 + 40),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(255, 255, 255),
2
)
# -----------------------------------------------------
# FPS
# -----------------------------------------------------
current_time = time.time()
fps = 1 / max(current_time - prev_time, 0.001)
prev_time = current_time
cv2.putText(
annotated,
f"FPS: {fps:.1f}",
(20, 35),
cv2.FONT_HERSHEY_SIMPLEX,
0.8,
(0, 255, 255),
2
)
# Status
cv2.putText(
annotated,
"YOLOv8 - LAPTOP CAMERA",
(20, 70),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
(255, 255, 255),
2
)
# -----------------------------------------------------
# SHOW
# -----------------------------------------------------
cv2.imshow(
"HIDRASHIP - YOLOv8 LIVE",
annotated
)
# -----------------------------------------------------
# EXIT
# -----------------------------------------------------
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
# =========================================================
# CLOSE
# =========================================================
cap.release()
cv2.destroyAllWindows()
print("Program selesai.")