Views
No views yet
pip install supervision git+https://github.com/THU-MIG/yolov10.git1from ultralytics import YOLOv10
2import supervision as sv
3import cv2
4
5def attempt_download_from_hub(repo_id, hf_token=None):
6 # https://github.com/fcakyon/yolov5-pip/blob/main/yolov5/utils/downloads.py
7 from huggingface_hub import hf_hub_download, list_repo_files
8 from huggingface_hub.utils._errors import RepositoryNotFoundError
9 from huggingface_hub.utils._validators import HFValidationError
10 try:
11 repo_files = list_repo_files(repo_id=repo_id, repo_type='model', token=hf_token)
12 model_file = [f for f in repo_files if f.endswith('.pt')][0]
13 file = hf_hub_download(
14 repo_id=repo_id,
15 filename=model_file,
16 repo_type='model',
17 token=hf_token,
18 )
19 return file
20 except (RepositoryNotFoundError, HFValidationError):
21 return None
22
23
24MODEL_PATH = attempt_download_from_hub("kadirnar/yolov10x", hf_token="hf_token")
25IMAGE_PATH = 'dog.jpeg'
26
27model = YOLOv10(MODEL_PATH)
28image = cv2.imread(IMAGE_PATH)
29results = model(source=image, conf=0.25, verbose=False)[0]
30detections = sv.Detections.from_ultralytics(results)
31box_annotator = sv.BoxAnnotator()
32
33category_dict = {
34 0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus',
35 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant',
36 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat',
37 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear',
38 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag',
39 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard',
40 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove',
41 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle',
42 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl',
43 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli',
44 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake',
45 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table',
46 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard',
47 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink',
48 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors',
49 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'
50}
51
52labels = [
53 f"{category_dict[class_id]} {confidence:.2f}"
54 for class_id, confidence in zip(detections.class_id, detections.confidence)
55]
56annotated_image = box_annotator.annotate(
57 image.copy(), detections=detections, labels=labels
58)
59
60cv2.imwrite('annotated_dog.jpeg', annotated_image)@misc{wang2024yolov10,
title={YOLOv10: Real-Time End-to-End Object Detection},
author={Ao Wang and Hui Chen and Lihao Liu and Kai Chen and Zijia Lin and Jungong Han and Guiguang Ding},
year={2024},
eprint={2405.14458},
archivePrefix={arXiv},
primaryClass={cs.CV}
}