Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import torch
|
| 2 |
from PIL import Image
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
-
import matplotlib.patches as patches
|
| 5 |
import io
|
| 6 |
from random import choice
|
| 7 |
import gradio as gr
|
|
@@ -9,27 +8,22 @@ from yolo import YOLO
|
|
| 9 |
|
| 10 |
yolo = YOLO()
|
| 11 |
|
| 12 |
-
|
| 13 |
-
try:
|
| 14 |
-
r_image = yolo.detect_image(image, crop=crop, count=count)
|
| 15 |
-
return r_image
|
| 16 |
-
except Exception as e:
|
| 17 |
-
return str(e)
|
| 18 |
-
|
| 19 |
COLORS = ["#ff7f7f", "#ff7fbf", "#ff7fff", "#bf7fff",
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
|
|
|
| 23 |
fdic = {
|
| 24 |
-
"family"
|
| 25 |
-
"style"
|
| 26 |
-
"size"
|
| 27 |
-
"color"
|
| 28 |
-
"weight"
|
| 29 |
}
|
| 30 |
|
| 31 |
-
|
| 32 |
def get_figure(in_pil_img, in_results):
|
|
|
|
| 33 |
plt.figure(figsize=(16, 10))
|
| 34 |
plt.imshow(in_pil_img)
|
| 35 |
ax = plt.gca()
|
|
@@ -37,32 +31,29 @@ def get_figure(in_pil_img, in_results):
|
|
| 37 |
for score, label, box in zip(in_results["scores"], in_results["labels"], in_results["boxes"]):
|
| 38 |
selected_color = choice(COLORS)
|
| 39 |
|
| 40 |
-
box_int = [i.item() for i in torch.round(box)
|
| 41 |
-
x, y, w, h = box_int[0], box_int[1], box_int[2]-box_int[0], box_int[3]-box_int[1]
|
| 42 |
-
#x, y, w, h = torch.round(box[0]).item(), torch.round(box[1]).item(), torch.round(box[2]-box[0]).item(), torch.round(box[3]-box[1]).item()
|
| 43 |
|
| 44 |
ax.add_patch(plt.Rectangle((x, y), w, h, fill=False, color=selected_color, linewidth=3, alpha=0.8))
|
| 45 |
-
ax.text(x, y
|
| 46 |
|
| 47 |
plt.axis("off")
|
| 48 |
-
|
| 49 |
return plt.gcf()
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
gr.Examples(['img/1.png', 'img/2.png'], inputs=input_image)
|
| 64 |
-
|
| 65 |
-
send_btn = gr.Button("Predict")
|
| 66 |
-
|
| 67 |
-
#demo.queue()
|
| 68 |
-
demo.launch(debug=True)
|
|
|
|
| 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
|
|
|
|
| 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()
|
|
|
|
| 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 |
+
try:
|
| 45 |
+
r_image = yolo.detect_image(image)
|
| 46 |
+
return r_image
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return str(e)
|
| 49 |
|
| 50 |
+
# Define the Gradio interface
|
| 51 |
+
gr.Interface(
|
| 52 |
+
fn=predict,
|
| 53 |
+
inputs=gr.inputs.Image(label="Input image", type="pil"),
|
| 54 |
+
outputs=gr.outputs.Image(label="Output image with predicted instances", type="pil"),
|
| 55 |
+
examples=['img/1.png', 'img/2.png'],
|
| 56 |
+
title="MASFNet Object Detection",
|
| 57 |
+
theme="default",
|
| 58 |
+
debug=True
|
| 59 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|