Maulidaaa commited on
Commit
fc0c11a
·
verified ·
1 Parent(s): b9449ed

Upload 2 files

Browse files
detector/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from .gesture_detector import detect_gesture
detector/gesture_detector.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ultralytics import YOLO
2
+ from PIL import Image
3
+ import cv2
4
+ import os
5
+ from datetime import datetime
6
+ import base64
7
+
8
+ # Load model YOLO hanya sekali
9
+ model = YOLO("best (2).pt")
10
+
11
+ # Buat folder hasil
12
+ os.makedirs("detected_images", exist_ok=True)
13
+
14
+ def detect_gesture(image_file):
15
+ image = Image.open(image_file.stream).convert("RGB")
16
+ results = model.predict(image, conf=0.3)
17
+
18
+ detections = []
19
+ if results[0].boxes is not None:
20
+ for box in results[0].boxes:
21
+ class_id = int(box.cls[0])
22
+ confidence = float(box.conf[0])
23
+ label = model.names[class_id]
24
+ x1, y1, x2, y2 = box.xyxy[0].tolist()
25
+
26
+ detections.append({
27
+ "label": label,
28
+ "confidence": round(confidence, 2),
29
+ "bbox": {
30
+ "x1": round(x1, 2),
31
+ "y1": round(y1, 2),
32
+ "x2": round(x2, 2),
33
+ "y2": round(y2, 2)
34
+ }
35
+ })
36
+
37
+ img_with_boxes = results[0].plot()
38
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
39
+ saved_filename = f"detected_images/detection_{timestamp}.jpg"
40
+ cv2.imwrite(saved_filename, img_with_boxes)
41
+
42
+ _, buffer = cv2.imencode(".jpg", img_with_boxes)
43
+ img_base64 = base64.b64encode(buffer).decode("utf-8")
44
+
45
+ return {
46
+ "saved_to": saved_filename,
47
+ "image_base64": img_base64,
48
+ "detections": detections,
49
+ "total_detections": len(detections)
50
+ }