Spaces:
Runtime error
Runtime error
Commit
·
fee6d38
1
Parent(s):
f23cbc0
Upload 8 files
Browse files- .gitattributes +2 -0
- app.py +106 -0
- requirements.txt +5 -0
- samples/cats.jpg +0 -0
- samples/detectron.png +0 -0
- samples/dogandcat.jpg +0 -0
- samples/god.jpg +3 -0
- samples/hotdog.jpg +0 -0
- samples/road.jpg +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
samples/god.jpg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
samples/road.jpg filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
import cv2
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
from random import choice
|
| 11 |
+
import io
|
| 12 |
+
|
| 13 |
+
detector50 = pipeline(model="facebook/detr-resnet-50")
|
| 14 |
+
|
| 15 |
+
detector101 = pipeline(model="facebook/detr-resnet-101")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import gradio as gr
|
| 19 |
+
|
| 20 |
+
COLORS = ["#ff7f7f", "#ff7fbf", "#ff7fff", "#bf7fff",
|
| 21 |
+
"#7f7fff", "#7fbfff", "#7fffff", "#7fffbf",
|
| 22 |
+
"#7fff7f", "#bfff7f", "#ffff7f", "#ffbf7f"]
|
| 23 |
+
|
| 24 |
+
fdic = {
|
| 25 |
+
"family" : "Impact",
|
| 26 |
+
"style" : "italic",
|
| 27 |
+
"size" : 15,
|
| 28 |
+
"color" : "yellow",
|
| 29 |
+
"weight" : "bold"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def get_figure(in_pil_img, in_results):
|
| 34 |
+
# Convert PIL image to OpenCV format
|
| 35 |
+
img_cv2 = np.array(in_pil_img)
|
| 36 |
+
img_cv2 = cv2.cvtColor(img_cv2, cv2.COLOR_RGB2BGR)
|
| 37 |
+
|
| 38 |
+
for prediction in in_results:
|
| 39 |
+
selected_color = choice(COLORS)
|
| 40 |
+
color = tuple(int(selected_color[i:i+2], 16) for i in (1, 3, 5)) # Convert hex color to RGB tuple
|
| 41 |
+
|
| 42 |
+
x, y = prediction['box']['xmin'], prediction['box']['ymin']
|
| 43 |
+
w, h = prediction['box']['xmax'] - prediction['box']['xmin'], prediction['box']['ymax'] - prediction['box']['ymin']
|
| 44 |
+
|
| 45 |
+
# Draw bounding box using OpenCV
|
| 46 |
+
img_cv2 = cv2.rectangle(img_cv2, (x, y), (x+w, y+h), color, 2)
|
| 47 |
+
text = f"{prediction['label']}: {round(prediction['score']*100, 1)}%"
|
| 48 |
+
img_cv2 = cv2.putText(img_cv2, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
|
| 49 |
+
|
| 50 |
+
# Convert back to PIL format
|
| 51 |
+
img_pil = Image.fromarray(cv2.cvtColor(img_cv2, cv2.COLOR_BGR2RGB))
|
| 52 |
+
return img_pil
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def infer(model, in_pil_img):
|
| 56 |
+
|
| 57 |
+
results = None
|
| 58 |
+
if model == "detr-resnet-101":
|
| 59 |
+
results = detector101(in_pil_img)
|
| 60 |
+
else:
|
| 61 |
+
results = detector50(in_pil_img)
|
| 62 |
+
|
| 63 |
+
output_pil_img = get_figure(in_pil_img, results)
|
| 64 |
+
|
| 65 |
+
output_pil_img.save("output.jpg")
|
| 66 |
+
|
| 67 |
+
return output_pil_img
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
with gr.Blocks(title="DETR Object Detection using openCV",
|
| 71 |
+
css=".gradio-container {background:lightyellow;}"
|
| 72 |
+
) as demo:
|
| 73 |
+
#sample_index = gr.State([])
|
| 74 |
+
|
| 75 |
+
gr.HTML("""<div style="font-family:'Times New Roman', 'Serif'; font-size:16pt; font-weight:bold; text-align:center; color:royalblue;">ObjecTron🪄</div>""")
|
| 76 |
+
gr.HTML("""<div style="font-family:'Times New Roman', 'Serif'; font-size:16pt; font-weight:bold; text-align:center; color:royalblue;">
|
| 77 |
+
A object detection app using OpenCV, Huggingface-transformers, detr-resnet and Gradio </div>""")
|
| 78 |
+
gr.HTML("""<h4 style="color:navy;">1. Select a model.</h4>""")
|
| 79 |
+
|
| 80 |
+
model = gr.Radio(["detr-resnet-50", "detr-resnet-101"], value="detr-resnet-50", label="Model name")
|
| 81 |
+
|
| 82 |
+
gr.HTML("""<br/>""")
|
| 83 |
+
gr.HTML("""<h4 style="color:navy;">2-a. Select an example below</h4>""")
|
| 84 |
+
gr.HTML("""<h4 style="color:navy;">2-b. Or upload an image by clicking on the canvas.</h4>""")
|
| 85 |
+
|
| 86 |
+
with gr.Row():
|
| 87 |
+
input_image = gr.Image(label="Input image", type="pil")
|
| 88 |
+
output_image = gr.Image(label="Output image with predicted instances", type="pil")
|
| 89 |
+
|
| 90 |
+
gr.Examples(['samples/god.jpg','samples/road.jpg','samples/cats.jpg','samples/detectron.png','samples/dogandcat.jpg'], inputs=input_image)
|
| 91 |
+
|
| 92 |
+
gr.HTML("""<br/>""")
|
| 93 |
+
gr.HTML("""<h4 style="color:navy;">3. Then, click the button below to predict and see the magic!!!</h4>""")
|
| 94 |
+
|
| 95 |
+
send_btn = gr.Button("Expecto Patronum 🪄")
|
| 96 |
+
send_btn.click(fn=infer, inputs=[model, input_image], outputs=[output_image])
|
| 97 |
+
|
| 98 |
+
gr.HTML("""<br/>""")
|
| 99 |
+
gr.HTML("""<h4 style="color:navy;">Reference</h4>""")
|
| 100 |
+
gr.HTML("""<ul>""")
|
| 101 |
+
gr.HTML("""<li><a href="https://colab.research.google.com/github/facebookresearch/detr/blob/colab/notebooks/detr_attention.ipynb" target="_blank">Hands-on tutorial for DETR by facebookresearch</a>""")
|
| 102 |
+
gr.HTML("""</ul>""")
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
#demo.queue()
|
| 106 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
timm
|
| 4 |
+
opencv-python
|
| 5 |
+
pillow
|
samples/cats.jpg
ADDED
|
samples/detectron.png
ADDED
|
samples/dogandcat.jpg
ADDED
|
samples/god.jpg
ADDED
|
Git LFS Details
|
samples/hotdog.jpg
ADDED
|
samples/road.jpg
ADDED
|
Git LFS Details
|