vpratama

detection-distance.py

Oct 8th, 2025
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. import cv2
  2. from ultralytics import YOLO
  3. from scipy.spatial import distance as dist
  4. import datetime
  5. import json
  6.  
  7. focal_length = 258
  8.  
  9. real_sizes = {
  10.     'person': 1.65,
  11.     'car': 1.5,
  12.     'bicycle': 1.0,
  13.     'dog': 0.5,
  14.     'cat': 0.3,
  15.     'truck': 2.0,
  16.     'bus': 3.5,
  17.     'motorcycle': 1.2,
  18.     'sheep': 1.0,
  19.     'horse': 1.5,
  20.     'elephant': 3.0,
  21.     'giraffe': 5.5,
  22.     'table': 0.75,
  23.     'chair': 0.45,
  24.     'sofa': 0.85,
  25.     'tv': 1.0,
  26.     'refrigerator': 1.8,
  27.     'door': 2.0,
  28.     'window': 1.5,
  29.     'tree': 5.0,
  30.     'flower': 0.3,
  31.     'basketball': 0.24,
  32.     'soccer_ball': 0.22,
  33.     'apple': 0.1,
  34.     'banana': 0.2,
  35.     'bottle': 0.25,
  36.     'cup': 0.1,
  37.     'pencil': 0.19,
  38.     'book': 0.25,
  39.     'laptop': 0.02,
  40.     'backpack': 0.5,
  41.     'stool': 0.75,
  42.     'bench': 0.9,
  43.     'kitchen_island': 0.9,
  44.     'fire_hydrant': 1.0,
  45.     'traffic_light': 2.5,
  46.     'sign': 1.5,
  47.     'cactus': 1.0,
  48.     'palm_tree': 6.0,
  49.     'fence': 1.5,
  50.     'swing': 1.5,
  51.     'ladder': 2.0,
  52.     'scooter': 0.9,
  53.     'skateboard': 0.1,
  54. }
  55.  
  56. model = YOLO("yolo11n.pt")
  57. cap = cv2.VideoCapture(0)
  58.  
  59. def get_label(class_id, yolo_classes):
  60.     return yolo_classes[class_id]
  61.  
  62. while cap.isOpened():
  63.     success, frame = cap.read()
  64.     if success:
  65.         results = model(frame)
  66.         annotated_frame = results[0].plot()
  67.         detection_data = []
  68.         for result in results:
  69.             for box in result.boxes:
  70.                 x1, y1, x2, y2 = box.xyxy[0]
  71.                 confidence = box.conf[0]
  72.                 label = model.names[int(box.cls[0])]  
  73.                 class_id = result.boxes.cls[0]
  74.                 name = get_label(int(class_id), model.names)
  75.                 box_size = dist.euclidean((x1, y1), (x2, y2))
  76.  
  77.                 real_size = real_sizes.get(label, 1.0)
  78.                 distance = (real_size * focal_length) / box_size
  79.  
  80.                 detection_data.append({
  81.                     "time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
  82.                     "class_id": int(class_id),
  83.                     'label': label,
  84.                     "name": name,
  85.                     'confidence': confidence.item(),
  86.                     'box': [int(x1), int(y1), int(x2), int(y2)],
  87.                     "x1": int(x1),
  88.                     "y1": int(y1),
  89.                     "x2": int(x2),
  90.                     "y2": int(y2),
  91.                     "distance": distance
  92.                 })
  93.  
  94.         print(json.dumps(detection_data))
  95.  
  96.         for detection in detection_data:
  97.             x1, y1, x2, y2 = detection['box']
  98.             cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
  99.             text = f"{detection['label']}: {detection['confidence']:.2f}, Distance: {detection['distance']:.2f}"
  100.             cv2.putText(frame, text, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
  101.            
  102.         cv2.imshow("Prediksi Kamera", frame)
  103.         if cv2.waitKey(1) & 0xFF == ord("q"):
  104.             break
  105.     else:
  106.         break
  107.  
  108. cap.release()
  109. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment