Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,49 @@
|
|
| 1 |
from transformers import pipeline
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
od_pipe = pipeline("object-detection", "facebook/detr-resnet-50")
|
| 5 |
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
+
import io
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def render_results_in_image(in_pil_img, in_results):
|
| 8 |
+
plt.figure(figsize=(16, 10))
|
| 9 |
+
plt.imshow(in_pil_img)
|
| 10 |
+
|
| 11 |
+
ax = plt.gca()
|
| 12 |
+
|
| 13 |
+
for prediction in in_results:
|
| 14 |
+
|
| 15 |
+
x, y = prediction['box']['xmin'], prediction['box']['ymin']
|
| 16 |
+
w = prediction['box']['xmax'] - prediction['box']['xmin']
|
| 17 |
+
h = prediction['box']['ymax'] - prediction['box']['ymin']
|
| 18 |
+
|
| 19 |
+
ax.add_patch(plt.Rectangle((x, y),
|
| 20 |
+
w,
|
| 21 |
+
h,
|
| 22 |
+
fill=False,
|
| 23 |
+
color="green",
|
| 24 |
+
linewidth=2))
|
| 25 |
+
ax.text(
|
| 26 |
+
x,
|
| 27 |
+
y,
|
| 28 |
+
f"{prediction['label']}: {round(prediction['score']*100, 1)}%",
|
| 29 |
+
color='red'
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
plt.axis("off")
|
| 33 |
+
|
| 34 |
+
# Save the modified image to a BytesIO object
|
| 35 |
+
img_buf = io.BytesIO()
|
| 36 |
+
plt.savefig(img_buf, format='png',
|
| 37 |
+
bbox_inches='tight',
|
| 38 |
+
pad_inches=0)
|
| 39 |
+
img_buf.seek(0)
|
| 40 |
+
modified_image = Image.open(img_buf)
|
| 41 |
+
|
| 42 |
+
# Close the plot to prevent it from being displayed
|
| 43 |
+
plt.close()
|
| 44 |
+
|
| 45 |
+
return modified_image
|
| 46 |
+
|
| 47 |
|
| 48 |
od_pipe = pipeline("object-detection", "facebook/detr-resnet-50")
|
| 49 |
|