Update myapp/detector.py
Browse files- myapp/detector.py +131 -90
myapp/detector.py
CHANGED
|
@@ -1,90 +1,131 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import os
|
| 3 |
-
import time
|
| 4 |
-
import numpy as np
|
| 5 |
-
from ultralytics import YOLO
|
| 6 |
-
from django.conf import settings
|
| 7 |
-
|
| 8 |
-
class TrafficDetector:
|
| 9 |
-
def __init__(self, model_rel_path):
|
| 10 |
-
self.model_path = os.path.join(settings.MEDIA_ROOT, model_rel_path)
|
| 11 |
-
self.model = YOLO(self.model_path)
|
| 12 |
-
self.objects_count = 0
|
| 13 |
-
self.
|
| 14 |
-
self.
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import os
|
| 3 |
+
import time
|
| 4 |
+
import numpy as np
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
from django.conf import settings
|
| 7 |
+
|
| 8 |
+
class TrafficDetector:
|
| 9 |
+
def __init__(self, model_rel_path):
|
| 10 |
+
self.model_path = os.path.join(settings.MEDIA_ROOT, model_rel_path)
|
| 11 |
+
self.model = YOLO(self.model_path)
|
| 12 |
+
self.objects_count = 0
|
| 13 |
+
self.traffic_weight = 0
|
| 14 |
+
self.current_file_id = None
|
| 15 |
+
self.stop_processing = False
|
| 16 |
+
|
| 17 |
+
def process_file(self, file_path):
|
| 18 |
+
ext = os.path.splitext(file_path)[1].lower()
|
| 19 |
+
|
| 20 |
+
if ext in ['.jpg', '.jpeg', '.png', '.bmp']:
|
| 21 |
+
return self.process_image(file_path)
|
| 22 |
+
elif ext in ['.mp4', '.avi', '.mov', '.mkv']:
|
| 23 |
+
return self.get_frames(file_path)
|
| 24 |
+
else:
|
| 25 |
+
return None
|
| 26 |
+
|
| 27 |
+
def process_image(self, image_path):
|
| 28 |
+
frame = cv2.imread(image_path)
|
| 29 |
+
if frame is None:
|
| 30 |
+
return
|
| 31 |
+
|
| 32 |
+
objects_count = 0
|
| 33 |
+
traffic_weight = 0
|
| 34 |
+
|
| 35 |
+
results = self.model.predict(frame, conf=0.5, verbose=False)
|
| 36 |
+
frame = results[0].plot()
|
| 37 |
+
|
| 38 |
+
for r in results:
|
| 39 |
+
boxes = r.boxes
|
| 40 |
+
objects_count = len(boxes)
|
| 41 |
+
for box in boxes:
|
| 42 |
+
cls_id = int(box.cls[0])
|
| 43 |
+
label = r.names[cls_id]
|
| 44 |
+
|
| 45 |
+
if label.lower() == 'mobil':
|
| 46 |
+
traffic_weight += 4
|
| 47 |
+
elif label.lower() == 'kendaraan besar':
|
| 48 |
+
traffic_weight += 6
|
| 49 |
+
else:
|
| 50 |
+
traffic_weight += 1
|
| 51 |
+
|
| 52 |
+
if traffic_weight > 100:
|
| 53 |
+
traffic_weight = 100
|
| 54 |
+
|
| 55 |
+
self.traffic_weight = traffic_weight
|
| 56 |
+
self.objects_count = objects_count
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
_, buffer = cv2.imencode('.jpg', frame)
|
| 60 |
+
yield (b'--frame\r\n'
|
| 61 |
+
b'Content-Type: image/jpeg\r\n\r\n' + buffer.tobytes() + b'\r\n')
|
| 62 |
+
|
| 63 |
+
def get_frames(self, video_path):
|
| 64 |
+
current_timer = 10
|
| 65 |
+
last_time_update = time.time()
|
| 66 |
+
lampu_state = "GREEN"
|
| 67 |
+
objects_count = 0
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
cap = cv2.VideoCapture(video_path)
|
| 71 |
+
while cap.isOpened():
|
| 72 |
+
if self.stop_processing:
|
| 73 |
+
break
|
| 74 |
+
|
| 75 |
+
ret, frame = cap.read()
|
| 76 |
+
if not ret: break
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
results = self.model.predict(frame, conf=0.5, verbose=False)
|
| 80 |
+
frame = results[0].plot()
|
| 81 |
+
|
| 82 |
+
traffic_weight = 0
|
| 83 |
+
objects_count = 0
|
| 84 |
+
for r in results:
|
| 85 |
+
boxes = r.boxes
|
| 86 |
+
objects_count += len(boxes)
|
| 87 |
+
for box in boxes:
|
| 88 |
+
cls_id = int(box.cls[0])
|
| 89 |
+
label = r.names[cls_id]
|
| 90 |
+
|
| 91 |
+
if label.lower() == 'mobil':
|
| 92 |
+
traffic_weight += 4
|
| 93 |
+
elif label.lower() == 'kendaraan besar':
|
| 94 |
+
traffic_weight += 6
|
| 95 |
+
else:
|
| 96 |
+
traffic_weight += 1
|
| 97 |
+
|
| 98 |
+
if traffic_weight > 100:
|
| 99 |
+
traffic_weight = 100
|
| 100 |
+
|
| 101 |
+
self.traffic_weight = traffic_weight
|
| 102 |
+
self.objects_count = objects_count
|
| 103 |
+
|
| 104 |
+
if time.time() - last_time_update >= 1:
|
| 105 |
+
if current_timer > 0:
|
| 106 |
+
current_timer -= 1
|
| 107 |
+
last_time_update = time.time()
|
| 108 |
+
|
| 109 |
+
if current_timer <= 0:
|
| 110 |
+
if lampu_state == "RED":
|
| 111 |
+
lampu_state = "GREEN"
|
| 112 |
+
current_timer = 20 if self.traffic_weight > 80 else 10
|
| 113 |
+
else:
|
| 114 |
+
lampu_state = "RED"
|
| 115 |
+
current_timer = 30 if self.traffic_weight > 80 else 15
|
| 116 |
+
|
| 117 |
+
color_map = {"RED": (0, 0, 255), "GREEN": (0, 255, 0)}
|
| 118 |
+
current_color = color_map[lampu_state]
|
| 119 |
+
|
| 120 |
+
cv2.putText(frame, f"STATUS: {lampu_state}", (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, current_color, 3)
|
| 121 |
+
cv2.putText(frame, f"TIMER: {current_timer}s", (20, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
|
| 122 |
+
|
| 123 |
+
_, buffer = cv2.imencode('.jpg', frame)
|
| 124 |
+
yield (b'--frame\r\n'
|
| 125 |
+
b'Content-Type: image/jpeg\r\n\r\n' + buffer.tobytes() + b'\r\n')
|
| 126 |
+
|
| 127 |
+
except Exception as e:
|
| 128 |
+
print(f"Error processing frame: {e}")
|
| 129 |
+
continue
|
| 130 |
+
|
| 131 |
+
cap.release()
|