JUNGU kadirnar commited on
Commit
a30f983
0 Parent(s):

Duplicate from kadirnar/yolov8

Browse files

Co-authored-by: Kadir Nar <[email protected]>

Files changed (5) hide show
  1. .gitattributes +34 -0
  2. README.md +14 -0
  3. app.py +87 -0
  4. requirements.txt +4 -0
  5. utils.py +17 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Yolov8
3
+ emoji: 💩
4
+ colorFrom: blue
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 3.16.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: gpl-3.0
11
+ duplicated_from: kadirnar/yolov8
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from ultralytics import YOLO
4
+ from sahi.prediction import ObjectPrediction
5
+ from sahi.utils.cv import visualize_object_predictions
6
+ import cv2
7
+ from utils import attempt_download_from_hub
8
+
9
+ # Images
10
+ torch.hub.download_url_to_file('https://raw.githubusercontent.com/kadirnar/dethub/main/data/images/highway.jpg', 'highway.jpg')
11
+ torch.hub.download_url_to_file('https://user-images.githubusercontent.com/34196005/142742872-1fefcc4d-d7e6-4c43-bbb7-6b5982f7e4ba.jpg', 'highway1.jpg')
12
+ torch.hub.download_url_to_file('https://raw.githubusercontent.com/obss/sahi/main/tests/data/small-vehicles1.jpeg', 'small-vehicles1.jpeg')
13
+
14
+ def yolov8_inference(
15
+ image: gr.inputs.Image = None,
16
+ model_path: gr.inputs.Dropdown = None,
17
+ image_size: gr.inputs.Slider = 640,
18
+ conf_threshold: gr.inputs.Slider = 0.25,
19
+ iou_threshold: gr.inputs.Slider = 0.45,
20
+ ):
21
+ """
22
+ YOLOv8 inference function
23
+ Args:
24
+ image: Input image
25
+ model_path: Path to the model
26
+ image_size: Image size
27
+ conf_threshold: Confidence threshold
28
+ iou_threshold: IOU threshold
29
+ Returns:
30
+ Rendered image
31
+ """
32
+ hf_model_path = attempt_download_from_hub(model_path)
33
+ model = YOLO(hf_model_path)
34
+ model.conf = conf_threshold
35
+ model.iou = iou_threshold
36
+ prediction = model.predict(image, imgsz=image_size)
37
+ object_prediction_list = []
38
+ for _, image_predictions_in_xyxy_format in enumerate(prediction):
39
+ for pred in image_predictions_in_xyxy_format.cpu().detach().numpy():
40
+ x1, y1, x2, y2 = (
41
+ int(pred[0]),
42
+ int(pred[1]),
43
+ int(pred[2]),
44
+ int(pred[3]),
45
+ )
46
+ bbox = [x1, y1, x2, y2]
47
+ score = pred[4]
48
+ category_name = model.model.names[int(pred[5])]
49
+ category_id = pred[5]
50
+ object_prediction = ObjectPrediction(
51
+ bbox=bbox,
52
+ category_id=int(category_id),
53
+ score=score,
54
+ category_name=category_name,
55
+ )
56
+ object_prediction_list.append(object_prediction)
57
+
58
+ image = cv2.imread(image)
59
+ save_path = 'output.jpg'
60
+ output_image = visualize_object_predictions(image=image, object_prediction_list=object_prediction_list)
61
+ output_image = cv2.imwrite(save_path, output_image["image"])
62
+ return save_path
63
+
64
+
65
+ inputs = [
66
+ gr.inputs.Image(type="filepath", label="Input Image"),
67
+ gr.inputs.Dropdown(["kadirnar/yolov8n-v8.0", "kadirnar/yolov8m-v8.0", "kadirnar/yolov8l-v8.0", "kadirnar/yolov8x-v8.0", "kadirnar/yolov8x6-v8.0"], label="Model"),
68
+ gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
69
+ gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
70
+ gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
71
+ ]
72
+
73
+ outputs = gr.outputs.Image(type="filepath", label="Output Image")
74
+ title = "Ultralytics YOLOv8: State-of-the-Art YOLO Models"
75
+
76
+ examples = [['highway.jpg', 'kadirnar/yolov8m-v8.0', 640, 0.25, 0.45], ['highway1.jpg', 'kadirnar/yolov8l-v8.0', 640, 0.25, 0.45], ['small-vehicles1.jpeg', 'kadirnar/yolov8x-v8.0', 1280, 0.25, 0.45]]
77
+ demo_app = gr.Interface(
78
+ fn=yolov8_inference,
79
+ inputs=inputs,
80
+ outputs=outputs,
81
+ title=title,
82
+ examples=examples,
83
+ cache_examples=True,
84
+ live=True,
85
+ theme='huggingface',
86
+ )
87
+ demo_app.launch(debug=True, enable_queue=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ opencv_python
2
+ sahi
3
+ torch
4
+ ultralytics
utils.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def attempt_download_from_hub(repo_id, hf_token=None):
2
+ # https://github.com/fcakyon/yolov5-pip/blob/main/yolov5/utils/downloads.py
3
+ from huggingface_hub import hf_hub_download, list_repo_files
4
+ from huggingface_hub.utils._errors import RepositoryNotFoundError
5
+ from huggingface_hub.utils._validators import HFValidationError
6
+ try:
7
+ repo_files = list_repo_files(repo_id=repo_id, repo_type='model', token=hf_token)
8
+ model_file = [f for f in repo_files if f.endswith('.pt')][0]
9
+ file = hf_hub_download(
10
+ repo_id=repo_id,
11
+ filename=model_file,
12
+ repo_type='model',
13
+ token=hf_token,
14
+ )
15
+ return file
16
+ except (RepositoryNotFoundError, HFValidationError):
17
+ return None