Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import matplotlib.patches as patches
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from random import choice
|
| 10 |
+
import io
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
model = pipeline(model="jaimin/ObjectDetect")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
COLORS = ["#ff7f7f", "#ff7fbf", "#ff7fff", "#bf7fff",
|
| 18 |
+
"#7f7fff", "#7fbfff", "#7fffff", "#7fffbf",
|
| 19 |
+
"#7fff7f", "#bfff7f", "#ffff7f", "#ffbf7f"]
|
| 20 |
+
|
| 21 |
+
fdic = {
|
| 22 |
+
"family" : "Impact",
|
| 23 |
+
"style" : "italic",
|
| 24 |
+
"size" : 15,
|
| 25 |
+
"color" : "yellow",
|
| 26 |
+
"weight" : "bold"
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def get_figure(in_pil_img, in_results):
|
| 31 |
+
plt.figure(figsize=(16, 10))
|
| 32 |
+
plt.imshow(in_pil_img)
|
| 33 |
+
#pyplot.gcf()
|
| 34 |
+
ax = plt.gca()
|
| 35 |
+
|
| 36 |
+
for prediction in in_results:
|
| 37 |
+
selected_color = choice(COLORS)
|
| 38 |
+
|
| 39 |
+
x, y = prediction['box']['xmin'], prediction['box']['ymin'],
|
| 40 |
+
w, h = prediction['box']['xmax'] - prediction['box']['xmin'], prediction['box']['ymax'] - prediction['box']['ymin']
|
| 41 |
+
|
| 42 |
+
ax.add_patch(plt.Rectangle((x, y), w, h, fill=False, color=selected_color, linewidth=3))
|
| 43 |
+
ax.text(x, y, f"{prediction['label']}: {round(prediction['score']*100, 1)}%", fontdict=fdic)
|
| 44 |
+
|
| 45 |
+
plt.axis("off")
|
| 46 |
+
|
| 47 |
+
return plt.gcf()
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def infer(model, in_pil_img):
|
| 51 |
+
|
| 52 |
+
results = model(in_pil_img)
|
| 53 |
+
|
| 54 |
+
figure = get_figure(in_pil_img, results)
|
| 55 |
+
|
| 56 |
+
buf = io.BytesIO()
|
| 57 |
+
figure.savefig(buf, bbox_inches='tight')
|
| 58 |
+
buf.seek(0)
|
| 59 |
+
output_pil_img = Image.open(buf)
|
| 60 |
+
|
| 61 |
+
return output_pil_img
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
input = gr.inputs.Image(label="Upload your Image", type = 'pil', optional=True)
|
| 65 |
+
output = gr.outputs.Textbox(label="Captions")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
interface = gr.Interface(
|
| 69 |
+
fn=predict,
|
| 70 |
+
inputs = input,
|
| 71 |
+
outputs=output,
|
| 72 |
+
)
|
| 73 |
+
interface.launch(debug=True)
|