Spaces:
Runtime error
Runtime error
keremberke
commited on
Commit
·
ce8aed7
1
Parent(s):
bcc855e
upload space files
Browse files- README.md +3 -3
- app.py +53 -0
- requirements.txt +5 -0
- test_images/-1079-_png_jpg.rf.eae5c731d79f3b240ce6b5ae84589e49.jpg +0 -0
- test_images/Mask-detector1_mov-46_jpg.rf.2122d830c41384952c89ef8cd23734ca.jpg +0 -0
- test_images/construction-1-_mp4-147_jpg.rf.6593d553fd4c445c810aedcc8f9bf5b0.jpg +0 -0
- test_images/construction-1023-_jpg.rf.10ea2a0d607573c1c90d7c38bacf2f04.jpg +0 -0
- test_images/construction-3-_mp4-21_jpg.rf.f90d04a7fe8ee4d1d3331050b4e64e1b.jpg +0 -0
- test_images/image_140_jpg.rf.e7727a5a4bd52d812adbd6f5d2fea6d9.jpg +0 -0
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
title: Construction Safety Object Detection
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.15.0
|
8 |
app_file: app.py
|
|
|
1 |
---
|
2 |
title: Construction Safety Object Detection
|
3 |
+
emoji: 🎮
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: gray
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.15.0
|
8 |
app_file: app.py
|
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import json
|
3 |
+
import gradio as gr
|
4 |
+
import yolov5
|
5 |
+
from PIL import Image
|
6 |
+
from huggingface_hub import hf_hub_download
|
7 |
+
|
8 |
+
app_title = "Construction Safety Object Detection"
|
9 |
+
models_ids = ['keremberke/yolov5n-construction-safety', 'keremberke/yolov5s-construction-safety', 'keremberke/yolov5m-construction-safety']
|
10 |
+
article = f"<p style='text-align: center'> <a href='https://huggingface.co/{models_ids[-1]}'>huggingface.co/{models_ids[-1]}</a> | <a href='https://huggingface.co/keremberke/construction-safety-object-detection'>huggingface.co/keremberke/construction-safety-object-detection</a> | <a href='https://github.com/keremberke/awesome-yolov5-models'>awesome-yolov5-models</a> </p>"
|
11 |
+
|
12 |
+
current_model_id = models_ids[-1]
|
13 |
+
model = yolov5.load(current_model_id)
|
14 |
+
|
15 |
+
examples = [['test_images/-1079-_png_jpg.rf.eae5c731d79f3b240ce6b5ae84589e49.jpg', 0.25, 'keremberke/yolov5m-construction-safety'], ['test_images/construction-1-_mp4-147_jpg.rf.6593d553fd4c445c810aedcc8f9bf5b0.jpg', 0.25, 'keremberke/yolov5m-construction-safety'], ['test_images/construction-1023-_jpg.rf.10ea2a0d607573c1c90d7c38bacf2f04.jpg', 0.25, 'keremberke/yolov5m-construction-safety'], ['test_images/construction-3-_mp4-21_jpg.rf.f90d04a7fe8ee4d1d3331050b4e64e1b.jpg', 0.25, 'keremberke/yolov5m-construction-safety'], ['test_images/image_140_jpg.rf.e7727a5a4bd52d812adbd6f5d2fea6d9.jpg', 0.25, 'keremberke/yolov5m-construction-safety'], ['test_images/Mask-detector1_mov-46_jpg.rf.2122d830c41384952c89ef8cd23734ca.jpg', 0.25, 'keremberke/yolov5m-construction-safety']]
|
16 |
+
|
17 |
+
|
18 |
+
def predict(image, threshold=0.25, model_id=None):
|
19 |
+
# update model if required
|
20 |
+
global current_model_id
|
21 |
+
global model
|
22 |
+
if model_id != current_model_id:
|
23 |
+
model = yolov5.load(model_id)
|
24 |
+
current_model_id = model_id
|
25 |
+
|
26 |
+
# get model input size
|
27 |
+
config_path = hf_hub_download(repo_id=model_id, filename="config.json")
|
28 |
+
with open(config_path, "r") as f:
|
29 |
+
config = json.load(f)
|
30 |
+
input_size = config["input_size"]
|
31 |
+
|
32 |
+
# perform inference
|
33 |
+
model.conf = threshold
|
34 |
+
results = model(image, size=input_size)
|
35 |
+
numpy_image = results.render()[0]
|
36 |
+
output_image = Image.fromarray(numpy_image)
|
37 |
+
return output_image
|
38 |
+
|
39 |
+
|
40 |
+
gr.Interface(
|
41 |
+
title=app_title,
|
42 |
+
description="Created by 'keremberke'",
|
43 |
+
article=article,
|
44 |
+
fn=predict,
|
45 |
+
inputs=[
|
46 |
+
gr.Image(type="pil"),
|
47 |
+
gr.Slider(maximum=1, step=0.01, value=0.25),
|
48 |
+
gr.Dropdown(models_ids, value=models_ids[-1]),
|
49 |
+
],
|
50 |
+
outputs=gr.Image(type="pil"),
|
51 |
+
examples=examples,
|
52 |
+
cache_examples=True if examples else False,
|
53 |
+
).launch(enable_queue=True)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
yolov5==7.0.5
|
3 |
+
gradio==3.15.0
|
4 |
+
torch
|
5 |
+
huggingface-hub
|
test_images/-1079-_png_jpg.rf.eae5c731d79f3b240ce6b5ae84589e49.jpg
ADDED
test_images/Mask-detector1_mov-46_jpg.rf.2122d830c41384952c89ef8cd23734ca.jpg
ADDED
test_images/construction-1-_mp4-147_jpg.rf.6593d553fd4c445c810aedcc8f9bf5b0.jpg
ADDED
test_images/construction-1023-_jpg.rf.10ea2a0d607573c1c90d7c38bacf2f04.jpg
ADDED
test_images/construction-3-_mp4-21_jpg.rf.f90d04a7fe8ee4d1d3331050b4e64e1b.jpg
ADDED
test_images/image_140_jpg.rf.e7727a5a4bd52d812adbd6f5d2fea6d9.jpg
ADDED