charanhu kadirnar commited on
Commit
c638640
0 Parent(s):

Duplicate from kadirnar/yolov8

Browse files

Co-authored-by: Kadir Nar <kadirnar@users.noreply.huggingface.co>

Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +16 -0
  3. app.py +84 -0
  4. requirements.txt +5 -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,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ tags:
12
+ - making-demos
13
+ duplicated_from: kadirnar/yolov8
14
+ ---
15
+
16
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from sahi.prediction import ObjectPrediction
4
+ from sahi.utils.cv import visualize_object_predictions, read_image
5
+ from ultralyticsplus import YOLO
6
+
7
+ # Images
8
+ torch.hub.download_url_to_file('https://raw.githubusercontent.com/kadirnar/dethub/main/data/images/highway.jpg', 'highway.jpg')
9
+ torch.hub.download_url_to_file('https://user-images.githubusercontent.com/34196005/142742872-1fefcc4d-d7e6-4c43-bbb7-6b5982f7e4ba.jpg', 'highway1.jpg')
10
+ torch.hub.download_url_to_file('https://raw.githubusercontent.com/obss/sahi/main/tests/data/small-vehicles1.jpeg', 'small-vehicles1.jpeg')
11
+
12
+ def yolov8_inference(
13
+ image: gr.inputs.Image = None,
14
+ model_path: gr.inputs.Dropdown = None,
15
+ image_size: gr.inputs.Slider = 640,
16
+ conf_threshold: gr.inputs.Slider = 0.25,
17
+ iou_threshold: gr.inputs.Slider = 0.45,
18
+ ):
19
+ """
20
+ YOLOv8 inference function
21
+ Args:
22
+ image: Input image
23
+ model_path: Path to the model
24
+ image_size: Image size
25
+ conf_threshold: Confidence threshold
26
+ iou_threshold: IOU threshold
27
+ Returns:
28
+ Rendered image
29
+ """
30
+ model = YOLO(model_path)
31
+ model.conf = conf_threshold
32
+ model.iou = iou_threshold
33
+ results = model.predict(image, imgsz=image_size, return_outputs=True)
34
+ object_prediction_list = []
35
+ for _, image_results in enumerate(results):
36
+ if len(image_results)!=0:
37
+ image_predictions_in_xyxy_format = image_results['det']
38
+ for pred in image_predictions_in_xyxy_format:
39
+ x1, y1, x2, y2 = (
40
+ int(pred[0]),
41
+ int(pred[1]),
42
+ int(pred[2]),
43
+ int(pred[3]),
44
+ )
45
+ bbox = [x1, y1, x2, y2]
46
+ score = pred[4]
47
+ category_name = model.model.names[int(pred[5])]
48
+ category_id = pred[5]
49
+ object_prediction = ObjectPrediction(
50
+ bbox=bbox,
51
+ category_id=int(category_id),
52
+ score=score,
53
+ category_name=category_name,
54
+ )
55
+ object_prediction_list.append(object_prediction)
56
+
57
+ image = read_image(image)
58
+ output_image = visualize_object_predictions(image=image, object_prediction_list=object_prediction_list)
59
+ return output_image['image']
60
+
61
+
62
+ inputs = [
63
+ gr.inputs.Image(type="filepath", label="Input Image"),
64
+ gr.inputs.Dropdown(["kadirnar/yolov8n-v8.0", "kadirnar/yolov8m-v8.0", "kadirnar/yolov8l-v8.0", "kadirnar/yolov8x-v8.0", "kadirnar/yolov8x6-v8.0"],
65
+ default="kadirnar/yolov8m-v8.0", label="Model"),
66
+ gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
67
+ gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
68
+ gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
69
+ ]
70
+
71
+ outputs = gr.outputs.Image(type="filepath", label="Output Image")
72
+ title = "Ultralytics YOLOv8: State-of-the-Art YOLO Models"
73
+
74
+ 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]]
75
+ demo_app = gr.Interface(
76
+ fn=yolov8_inference,
77
+ inputs=inputs,
78
+ outputs=outputs,
79
+ title=title,
80
+ examples=examples,
81
+ cache_examples=True,
82
+ theme='huggingface',
83
+ )
84
+ demo_app.launch(debug=True, enable_queue=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ opencv_python
2
+ sahi
3
+ torch
4
+ ultralytics==8.0.4
5
+ ultralyticsplus==0.0.3