Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
from random import choice
|
|
|
|
| 8 |
|
| 9 |
-
# โหลดโมเดล
|
| 10 |
detector50 = pipeline(model="facebook/detr-resnet-50")
|
| 11 |
-
detector101 = pipeline(model="facebook/detr-resnet-50") # แก้ให้ใช้ตัวเดียวกัน
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
COLORS = ["#ff7f7f", "#ff7fbf", "#ff7fff", "#bf7fff",
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
-
# font style
|
| 19 |
fdic = {
|
| 20 |
-
"family": "Impact",
|
| 21 |
-
"style": "italic",
|
| 22 |
-
"size": 15,
|
| 23 |
-
"color": "yellow",
|
| 24 |
-
"weight": "bold"
|
| 25 |
}
|
| 26 |
|
| 27 |
-
|
| 28 |
def get_figure(in_pil_img, in_results):
|
| 29 |
plt.figure(figsize=(16, 10))
|
| 30 |
plt.imshow(in_pil_img)
|
|
|
|
| 31 |
ax = plt.gca()
|
| 32 |
|
| 33 |
for prediction in in_results:
|
| 34 |
selected_color = choice(COLORS)
|
| 35 |
-
|
| 36 |
-
y = prediction['box']['ymin']
|
| 37 |
-
w = prediction['box']['xmax'] - prediction['box']['xmin']
|
| 38 |
-
h = prediction['box']['ymax'] - prediction['box']['ymin']
|
| 39 |
|
| 40 |
ax.add_patch(plt.Rectangle((x, y), w, h, fill=False, color=selected_color, linewidth=3))
|
| 41 |
ax.text(x, y, f"{prediction['label']}: {round(prediction['score']*100, 1)}%", fontdict=fdic)
|
| 42 |
|
| 43 |
plt.axis("off")
|
|
|
|
| 44 |
return plt.gcf()
|
| 45 |
|
| 46 |
-
|
| 47 |
def infer(model, in_pil_img):
|
|
|
|
|
|
|
| 48 |
if model == "detr-resnet-101":
|
| 49 |
results = detector101(in_pil_img)
|
| 50 |
else:
|
| 51 |
results = detector50(in_pil_img)
|
| 52 |
|
| 53 |
figure = get_figure(in_pil_img, results)
|
|
|
|
| 54 |
buf = io.BytesIO()
|
| 55 |
figure.savefig(buf, bbox_inches='tight')
|
| 56 |
buf.seek(0)
|
| 57 |
output_pil_img = Image.open(buf)
|
| 58 |
-
|
| 59 |
return output_pil_img
|
| 60 |
|
| 61 |
-
|
| 62 |
with gr.Blocks(title="DETR Object Detection - ClassCat",
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
gr.HTML("<div style=
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
gr.HTML("<h4 style='color:navy;'>1. Select a model.</h4>")
|
| 68 |
model = gr.Radio(["detr-resnet-50", "detr-resnet-101"], value="detr-resnet-50", label="Model name")
|
| 69 |
|
| 70 |
-
gr.HTML("<br
|
| 71 |
-
gr.HTML("<h4 style=
|
|
|
|
| 72 |
|
| 73 |
with gr.Row():
|
| 74 |
input_image = gr.Image(label="Input image", type="pil")
|
|
@@ -76,10 +129,22 @@ with gr.Blocks(title="DETR Object Detection - ClassCat",
|
|
| 76 |
|
| 77 |
gr.Examples(['samples/cats.jpg', 'samples/detectron2.png', 'samples/cat.jpg', 'samples/hotdog.jpg'], inputs=input_image)
|
| 78 |
|
| 79 |
-
gr.HTML("<br
|
|
|
|
|
|
|
| 80 |
send_btn = gr.Button("Infer")
|
| 81 |
send_btn.click(fn=infer, inputs=[model, input_image], outputs=[output_image])
|
| 82 |
|
| 83 |
-
gr.HTML("<br
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
|
|
|
| 85 |
demo.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Hugging Face's logo
|
| 2 |
+
Hugging Face
|
| 3 |
+
Models
|
| 4 |
+
Datasets
|
| 5 |
+
Spaces
|
| 6 |
+
Community
|
| 7 |
+
Docs
|
| 8 |
+
Pricing
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
Hugging Face is way more fun with friends and colleagues! 🤗 Join an organization
|
| 12 |
+
Spaces:
|
| 13 |
+
ClassCat
|
| 14 |
+
/
|
| 15 |
+
DETR-Object-Detection
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
like
|
| 19 |
+
21
|
| 20 |
+
App
|
| 21 |
+
Files
|
| 22 |
+
Community
|
| 23 |
+
DETR-Object-Detection
|
| 24 |
+
/
|
| 25 |
+
app.py
|
| 26 |
+
|
| 27 |
+
ClassCat's picture
|
| 28 |
+
ClassCat
|
| 29 |
+
update app.py
|
| 30 |
+
4f82037
|
| 31 |
+
over 2 years ago
|
| 32 |
+
raw
|
| 33 |
+
|
| 34 |
+
Copy download link
|
| 35 |
+
history
|
| 36 |
+
blame
|
| 37 |
+
contribute
|
| 38 |
+
delete
|
| 39 |
+
|
| 40 |
+
3.35 kB
|
| 41 |
+
|
| 42 |
import torch
|
| 43 |
from transformers import pipeline
|
| 44 |
+
|
| 45 |
from PIL import Image
|
| 46 |
+
|
| 47 |
import matplotlib.pyplot as plt
|
| 48 |
+
import matplotlib.patches as patches
|
| 49 |
+
|
| 50 |
from random import choice
|
| 51 |
+
import io
|
| 52 |
|
|
|
|
| 53 |
detector50 = pipeline(model="facebook/detr-resnet-50")
|
|
|
|
| 54 |
|
| 55 |
+
detector101 = pipeline(model="facebook/detr-resnet-101")
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
import gradio as gr
|
| 59 |
+
|
| 60 |
COLORS = ["#ff7f7f", "#ff7fbf", "#ff7fff", "#bf7fff",
|
| 61 |
+
"#7f7fff", "#7fbfff", "#7fffff", "#7fffbf",
|
| 62 |
+
"#7fff7f", "#bfff7f", "#ffff7f", "#ffbf7f"]
|
| 63 |
|
|
|
|
| 64 |
fdic = {
|
| 65 |
+
"family" : "Impact",
|
| 66 |
+
"style" : "italic",
|
| 67 |
+
"size" : 15,
|
| 68 |
+
"color" : "yellow",
|
| 69 |
+
"weight" : "bold"
|
| 70 |
}
|
| 71 |
|
| 72 |
+
|
| 73 |
def get_figure(in_pil_img, in_results):
|
| 74 |
plt.figure(figsize=(16, 10))
|
| 75 |
plt.imshow(in_pil_img)
|
| 76 |
+
#pyplot.gcf()
|
| 77 |
ax = plt.gca()
|
| 78 |
|
| 79 |
for prediction in in_results:
|
| 80 |
selected_color = choice(COLORS)
|
| 81 |
+
|
| 82 |
+
x, y = prediction['box']['xmin'], prediction['box']['ymin'],
|
| 83 |
+
w, h = prediction['box']['xmax'] - prediction['box']['xmin'], prediction['box']['ymax'] - prediction['box']['ymin']
|
|
|
|
| 84 |
|
| 85 |
ax.add_patch(plt.Rectangle((x, y), w, h, fill=False, color=selected_color, linewidth=3))
|
| 86 |
ax.text(x, y, f"{prediction['label']}: {round(prediction['score']*100, 1)}%", fontdict=fdic)
|
| 87 |
|
| 88 |
plt.axis("off")
|
| 89 |
+
|
| 90 |
return plt.gcf()
|
| 91 |
|
| 92 |
+
|
| 93 |
def infer(model, in_pil_img):
|
| 94 |
+
|
| 95 |
+
results = None
|
| 96 |
if model == "detr-resnet-101":
|
| 97 |
results = detector101(in_pil_img)
|
| 98 |
else:
|
| 99 |
results = detector50(in_pil_img)
|
| 100 |
|
| 101 |
figure = get_figure(in_pil_img, results)
|
| 102 |
+
|
| 103 |
buf = io.BytesIO()
|
| 104 |
figure.savefig(buf, bbox_inches='tight')
|
| 105 |
buf.seek(0)
|
| 106 |
output_pil_img = Image.open(buf)
|
| 107 |
+
|
| 108 |
return output_pil_img
|
| 109 |
|
| 110 |
+
|
| 111 |
with gr.Blocks(title="DETR Object Detection - ClassCat",
|
| 112 |
+
css=".gradio-container {background:lightyellow;}"
|
| 113 |
+
) as demo:
|
| 114 |
+
#sample_index = gr.State([])
|
| 115 |
|
| 116 |
+
gr.HTML("""<div style="font-family:'Times New Roman', 'Serif'; font-size:16pt; font-weight:bold; text-align:center; color:royalblue;">DETR Object Detection</div>""")
|
| 117 |
+
|
| 118 |
+
gr.HTML("""<h4 style="color:navy;">1. Select a model.</h4>""")
|
| 119 |
|
|
|
|
| 120 |
model = gr.Radio(["detr-resnet-50", "detr-resnet-101"], value="detr-resnet-50", label="Model name")
|
| 121 |
|
| 122 |
+
gr.HTML("""<br/>""")
|
| 123 |
+
gr.HTML("""<h4 style="color:navy;">2-a. Select an example by clicking a thumbnail below.</h4>""")
|
| 124 |
+
gr.HTML("""<h4 style="color:navy;">2-b. Or upload an image by clicking on the canvas.</h4>""")
|
| 125 |
|
| 126 |
with gr.Row():
|
| 127 |
input_image = gr.Image(label="Input image", type="pil")
|
|
|
|
| 129 |
|
| 130 |
gr.Examples(['samples/cats.jpg', 'samples/detectron2.png', 'samples/cat.jpg', 'samples/hotdog.jpg'], inputs=input_image)
|
| 131 |
|
| 132 |
+
gr.HTML("""<br/>""")
|
| 133 |
+
gr.HTML("""<h4 style="color:navy;">3. Then, click "Infer" button to predict object instances. It will take about 10 seconds (on cpu)</h4>""")
|
| 134 |
+
|
| 135 |
send_btn = gr.Button("Infer")
|
| 136 |
send_btn.click(fn=infer, inputs=[model, input_image], outputs=[output_image])
|
| 137 |
|
| 138 |
+
gr.HTML("""<br/>""")
|
| 139 |
+
gr.HTML("""<h4 style="color:navy;">Reference</h4>""")
|
| 140 |
+
gr.HTML("""<ul>""")
|
| 141 |
+
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</a>""")
|
| 142 |
+
gr.HTML("""</ul>""")
|
| 143 |
+
|
| 144 |
|
| 145 |
+
#demo.queue()
|
| 146 |
demo.launch(debug=True)
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
### EOF ###
|
| 150 |
+
|