luisarizmendi commited on
Commit
28a72b5
·
verified ·
1 Parent(s): 85b2ce2

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +212 -0
README.md ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ task_categories:
3
+ - object-detection
4
+ tags:
5
+ - yolo
6
+ - yolo11
7
+ - hardhat
8
+ - hat
9
+ datasets:
10
+ - luisarizmendi/safety-equipment
11
+ base_model:
12
+ - Ultralytics/YOLO11
13
+ widget:
14
+ - src: >-
15
+ https://huggingface.co/datasets/mishig/sample_images/resolve/main/football-match.jpg
16
+ example_title: Football Match
17
+ - src: >-
18
+ https://huggingface.co/datasets/mishig/sample_images/resolve/main/airport.jpg
19
+ example_title: Airport
20
+ pipeline_tag: object-detection
21
+ model-index:
22
+ - name: yolo11-safety-equipment
23
+ results:
24
+ - task:
25
+ type: object-detection
26
+ dataset:
27
+ type: safety-equipment
28
+ name: Safety Equipment
29
+ args:
30
+ epochs: 35
31
+ batch: 2
32
+ imgsz: 640
33
+ patience: 5
34
+ optimizer: SGD
35
+ lr0: 0.001
36
+ lrf: 0.01
37
+ momentum: 0.9
38
+ weight_decay: 0.0005
39
+ warmup_epochs: 3
40
+ warmup_bias_lr: 0.01
41
+ warmup_momentum: 0.8
42
+ metrics:
43
+ - type: precision
44
+ name: Precision
45
+ value: 0.9078
46
+ - type: recall
47
+ name: Recall
48
+ value: 0.9064
49
+ - type: mAP50
50
+ name: mAP50
51
+ value: 0.9589
52
+ - type: mAP50-95
53
+ name: mAP50-95
54
+ value: 0.6088
55
+ ---
56
+
57
+ # Model for detecting Hardhats and Hats
58
+
59
+
60
+ <div align="center">
61
+ <img width="640" alt="luisarizmendi/safety-equipment" src="https://huggingface.co/luisarizmendi/hardhat-or-hat/resolve/main/example.png">
62
+ </div>
63
+
64
+ ## Model binary
65
+
66
+ You can [download the model from here](https://github.com/luisarizmendi/ai-apps/raw/refs/heads/main/models/luisarizmendi/object-detector-safety/object-detector-safety-v1.pt)
67
+
68
+
69
+ ## Labels
70
+
71
+ ```
72
+ - hat
73
+ - helmet
74
+ - no_helmet
75
+ ```
76
+
77
+
78
+ ## Model metrics
79
+
80
+
81
+ <div align="center">
82
+ <img width="640" alt="luisarizmendi/safety-equipment" src="https://huggingface.co/luisarizmendi/yolo11-safety-equipment/resolve/main/confusion_matrix_normalized.png"> <img width="640" alt="luisarizmendi/safety-equipment" src="https://huggingface.co/luisarizmendi/yolo11-safety-equipment/resolve/main/results.png">
83
+ </div>
84
+
85
+
86
+ ## Model Dataset
87
+
88
+ [https://universe.roboflow.com/luisarizmendi/hardhat-or-hat](https://universe.roboflow.com/luisarizmendi/hardhat-or-hat)
89
+
90
+
91
+
92
+ ## Model training
93
+
94
+ ### Notebook
95
+
96
+ You can [review the Jupyter notebook here](https://huggingface.co/luisarizmendihardhat-or-hat/blob/main/train.ipynb)
97
+
98
+ ### Hyperparameters
99
+
100
+ ```
101
+ epochs: 35
102
+ batch: 2
103
+ imgsz: 640
104
+ patience: 5
105
+ optimizer: 'SGD'
106
+ lr0: 0.001
107
+ lrf: 0.01
108
+ momentum: 0.9
109
+ weight_decay: 0.0005
110
+ warmup_epochs: 3
111
+ warmup_bias_lr: 0.01
112
+ warmup_momentum: 0.8
113
+ ```
114
+
115
+ ### Augmentation
116
+
117
+ ```
118
+ hsv_h=0.015, # Image HSV-Hue augmentationc
119
+ hsv_s=0.7, # Image HSV-Saturation augmentation
120
+ hsv_v=0.4, # Image HSV-Value augmentation
121
+ degrees=10, # Image rotation (+/- deg)
122
+ translate=0.1, # Image translation (+/- fraction)
123
+ scale=0.3, # Image scale (+/- gain)
124
+ shear=0.0, # Image shear (+/- deg)
125
+ perspective=0.0, # Image perspective
126
+ flipud=0.1, # Image flip up-down
127
+ fliplr=0.1, # Image flip left-right
128
+ mosaic=1.0, # Image mosaic
129
+ mixup=0.0, # Image mixup
130
+ ```
131
+
132
+
133
+ ## Usage
134
+
135
+
136
+ ### Usage with Huggingface spaces
137
+
138
+ If you don't want to run it locally, you can use [this huggingface space](https://huggingface.co/spaces/luisarizmendi/safety-equipment-object-detection) that I've created with this code but be aware that this will be slow since I'm using a free instance, so it's better to run it locally with the python script below.
139
+
140
+
141
+ <div align="center">
142
+ <img width="640" alt="luisarizmendi/safety-equipment" src="https://huggingface.co/luisarizmendi/yolo11-safety-equipment/resolve/main/spaces-example.png">
143
+ </div>
144
+
145
+
146
+ ### Usage with Python script
147
+
148
+ Install the following PIP requirements
149
+
150
+ ```
151
+ gradio
152
+ ultralytics
153
+ Pillow
154
+ opencv-python
155
+ torch
156
+ ```
157
+
158
+ Then [run the python code below](https://huggingface.co/luisarizmendi/yolo11-safety-equipment/blob/main/run_model.py) and then open `http://localhost:7860` in a browser to upload and scan the images.
159
+
160
+ ```
161
+ import gradio as gr
162
+ from ultralytics import YOLO
163
+ from PIL import Image
164
+ import os
165
+ import cv2
166
+ import torch
167
+
168
+ def detect_objects_in_files(files):
169
+ """
170
+ Processes uploaded images for object detection.
171
+ """
172
+ if not files:
173
+ return "No files uploaded.", []
174
+
175
+ device = "cuda" if torch.cuda.is_available() else "cpu"
176
+ model = YOLO("https://github.com/luisarizmendi/ai-apps/raw/refs/heads/main/models/luisarizmendi/object-detector-safety/object-detector-safety-v1.pt")
177
+ model.to(device)
178
+
179
+ results_images = []
180
+ for file in files:
181
+ try:
182
+ image = Image.open(file).convert("RGB")
183
+ results = model(image)
184
+ result_img_bgr = results[0].plot()
185
+ result_img_rgb = cv2.cvtColor(result_img_bgr, cv2.COLOR_BGR2RGB)
186
+ results_images.append(result_img_rgb)
187
+
188
+ # If you want that images appear one by one (slower)
189
+ #yield "Processing image...", results_images
190
+
191
+ except Exception as e:
192
+ return f"Error processing file: {file}. Exception: {str(e)}", []
193
+
194
+ del model
195
+ torch.cuda.empty_cache()
196
+
197
+ return "Processing completed.", results_images
198
+
199
+ interface = gr.Interface(
200
+ fn=detect_objects_in_files,
201
+ inputs=gr.Files(file_types=["image"], label="Select Images"),
202
+ outputs=[
203
+ gr.Textbox(label="Status"),
204
+ gr.Gallery(label="Results")
205
+ ],
206
+ title="Object Detection on Images",
207
+ description="Upload images to perform object detection. The model will process each image and display the results."
208
+ )
209
+
210
+ if __name__ == "__main__":
211
+ interface.launch()
212
+ ```