Maulidaaa commited on
Commit
ae173e0
·
verified ·
1 Parent(s): b9e788f

Update detector/gesture_detector.py

Browse files
Files changed (1) hide show
  1. detector/gesture_detector.py +12 -12
detector/gesture_detector.py CHANGED
@@ -3,13 +3,16 @@ 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.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")
@@ -34,17 +37,14 @@ def detect_gesture(image_file):
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
  }
 
3
  import cv2
4
  import os
5
  from datetime import datetime
 
6
 
7
+ # Load YOLO model
8
  model = YOLO("best.pt")
9
 
10
+ # Folder aman untuk Hugging Face
11
+ SAVE_DIR = "/tmp/detected_images"
12
+ os.makedirs(SAVE_DIR, exist_ok=True)
13
+
14
+ # Base URL Hugging Face kamu (ganti sesuai nama Space kamu)
15
+ BASE_URL = "https://maulidaaa-hellome.hf.space/detected_images"
16
 
17
  def detect_gesture(image_file):
18
  image = Image.open(image_file.stream).convert("RGB")
 
37
  }
38
  })
39
 
40
+ # Simpan gambar hasil
41
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
42
+ filename = f"detection_{timestamp}.jpg"
43
+ saved_path = os.path.join(SAVE_DIR, filename)
44
+ cv2.imwrite(saved_path, results[0].plot())
 
 
45
 
46
  return {
47
+ "saved_to": f"{BASE_URL}/{filename}",
 
48
  "detections": detections,
49
  "total_detections": len(detections)
50
  }