Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,86 +1,30 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from PIL import Image
|
| 3 |
-
import matplotlib.pyplot as plt
|
| 4 |
-
import io
|
| 5 |
-
from random import choice
|
| 6 |
import gradio as gr
|
| 7 |
from yolo import YOLO
|
| 8 |
|
| 9 |
yolo = YOLO()
|
| 10 |
|
| 11 |
-
# Colors for bounding boxes
|
| 12 |
-
COLORS = ["#ff7f7f", "#ff7fbf", "#ff7fff", "#bf7fff",
|
| 13 |
-
"#7f7fff", "#7fbfff", "#7fffff", "#7fffbf",
|
| 14 |
-
"#7fff7f", "#bfff7f", "#ffff7f", "#ffbf7f"]
|
| 15 |
-
|
| 16 |
-
# Font dictionary for text annotations
|
| 17 |
-
fdic = {
|
| 18 |
-
"family": "DejaVu Serif",
|
| 19 |
-
"style": "normal",
|
| 20 |
-
"size": 18,
|
| 21 |
-
"color": "yellow",
|
| 22 |
-
"weight": "bold"
|
| 23 |
-
}
|
| 24 |
-
|
| 25 |
-
def get_figure(in_pil_img, in_results):
|
| 26 |
-
""" Function to generate figure with bounding boxes and labels """
|
| 27 |
-
plt.figure(figsize=(16, 10))
|
| 28 |
-
plt.imshow(in_pil_img)
|
| 29 |
-
ax = plt.gca()
|
| 30 |
-
|
| 31 |
-
for score, label, box in zip(in_results["scores"], in_results["labels"], in_results["boxes"]):
|
| 32 |
-
selected_color = choice(COLORS)
|
| 33 |
-
|
| 34 |
-
box_int = [int(i.item()) for i in torch.round(box)]
|
| 35 |
-
x, y, w, h = box_int[0], box_int[1], box_int[2] - box_int[0], box_int[3] - box_int[1]
|
| 36 |
-
|
| 37 |
-
ax.add_patch(plt.Rectangle((x, y), w, h, fill=False, color=selected_color, linewidth=3, alpha=0.8))
|
| 38 |
-
ax.text(x, y)
|
| 39 |
-
|
| 40 |
-
plt.axis("off")
|
| 41 |
-
return plt.gcf()
|
| 42 |
-
|
| 43 |
def predict(image):
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
gr.
|
| 59 |
-
gr.
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
gr.HTML("""<h4 style="color:navy;">3. Set a threshold value (default to 0.9)</h4>""")
|
| 69 |
-
|
| 70 |
-
# threshold = gr.Slider(0, 1.0, value=0.9, label='threshold')
|
| 71 |
-
|
| 72 |
-
gr.HTML("""<br/>""")
|
| 73 |
-
gr.HTML("""<h4 style="color:navy;">4. Then, click "Infer" button to predict object instances. It will take about 10 seconds (yolos-tiny) or 20 seconds (yolos-small).</h4>""")
|
| 74 |
-
|
| 75 |
-
send_btn = gr.Button("Infer")
|
| 76 |
-
send_btn.click(fn=predict, inputs=[input_image], outputs=[output_image])
|
| 77 |
-
|
| 78 |
-
gr.HTML("""<br/>""")
|
| 79 |
-
gr.HTML("""<h4 style="color:navy;">Reference</h4>""")
|
| 80 |
-
gr.HTML("""<ul>""")
|
| 81 |
-
gr.HTML("""<li><a href="https://huggingface.co/docs/transformers/model_doc/yolos" target="_blank">Hugging Face Transformers - YOLOS</a>""")
|
| 82 |
-
gr.HTML("""</ul>""")
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
#demo.queue()
|
| 86 |
-
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from yolo import YOLO
|
| 3 |
|
| 4 |
yolo = YOLO()
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def predict(image):
|
| 7 |
+
r_image = yolo.detect_image(image)
|
| 8 |
+
return r_image
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
title = "MASFNet: Multi-scale Adaptive Sampling Fusion Network for Object Detection in Adverse Weather"
|
| 12 |
+
description = """
|
| 13 |
+
The bot was trained to answer questions based on Rick and Morty dialogues. Ask Rick anything!
|
| 14 |
+
<img src="https://huggingface.co/spaces/course-demos/Rick_and_Morty_QA/resolve/main/rick.png" width=200px>
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
article = "Check out [the original Rick and Morty Bot](https://huggingface.co/spaces/kingabzpro/Rick_and_Morty_Bot) that this demo is based off of."
|
| 18 |
+
|
| 19 |
+
gr.Interface(
|
| 20 |
+
fn=predict,
|
| 21 |
+
inputs=gr.inputs.Image(type="file", label="Upload an Image"),
|
| 22 |
+
outputs=gr.outputs.Image("Prediction Result"),
|
| 23 |
+
title=title,
|
| 24 |
+
description=description,
|
| 25 |
+
article=article,
|
| 26 |
+
examples=[
|
| 27 |
+
["img/1.png"],
|
| 28 |
+
["img/2.png"]
|
| 29 |
+
]
|
| 30 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|