Spaces:
Runtime error
Runtime error
Update detection_utils.py
Browse files- detection_utils.py +65 -68
detection_utils.py
CHANGED
|
@@ -1,69 +1,66 @@
|
|
| 1 |
-
import io
|
| 2 |
-
import matplotlib.pyplot as plt
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
plt.
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
plt.
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
plt.
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
if i == len(summary) - 2:
|
| 67 |
-
result_string += "and "
|
| 68 |
-
result_string = result_string.rstrip(', ') + "."
|
| 69 |
return result_string
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import inflect
|
| 4 |
+
from PIL import Image
|
| 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 |
+
x, y = prediction['box']['xmin'], prediction['box']['ymin']
|
| 15 |
+
w = prediction['box']['xmax'] - prediction['box']['xmin']
|
| 16 |
+
h = prediction['box']['ymax'] - prediction['box']['ymin']
|
| 17 |
+
|
| 18 |
+
ax.add_patch(plt.Rectangle(
|
| 19 |
+
(x,y),
|
| 20 |
+
w,
|
| 21 |
+
h,
|
| 22 |
+
fill = False,
|
| 23 |
+
color = 'green',
|
| 24 |
+
linewidth = 2
|
| 25 |
+
))
|
| 26 |
+
|
| 27 |
+
ax.text(
|
| 28 |
+
x,
|
| 29 |
+
y,
|
| 30 |
+
f"{prediction['label']}: {round(prediction['score']*100,1)}%",
|
| 31 |
+
color = 'red'
|
| 32 |
+
)
|
| 33 |
+
plt.axis('off')
|
| 34 |
+
|
| 35 |
+
img_buf = io.BytesIO()
|
| 36 |
+
plt.savefig(img_buf, format = 'png', bbox_inches = 'tight', pad_inches = 0)
|
| 37 |
+
img_buf.seek(0)
|
| 38 |
+
modified_image = Image.open(img_buf)
|
| 39 |
+
plt.close()
|
| 40 |
+
return modified_image
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def summarize_predictions_natural_language(predictions):
|
| 44 |
+
summary = {}
|
| 45 |
+
p = inflect.engine()
|
| 46 |
+
|
| 47 |
+
for prediction in predictions:
|
| 48 |
+
label = prediction['label']
|
| 49 |
+
if label in summary:
|
| 50 |
+
summary[label] += 1
|
| 51 |
+
else:
|
| 52 |
+
summary[label] = 1
|
| 53 |
+
|
| 54 |
+
result_string = "In this image, there are "
|
| 55 |
+
for i, (label, count) in enumerate(summary.items()):
|
| 56 |
+
count_string = p.number_to_words(count)
|
| 57 |
+
result_string += f"{count_string} {label}"
|
| 58 |
+
|
| 59 |
+
if count > 1:
|
| 60 |
+
result_string += 's'
|
| 61 |
+
result_string += " "
|
| 62 |
+
|
| 63 |
+
if i == len(summary) - 2:
|
| 64 |
+
result_string += "and "
|
| 65 |
+
result_string = result_string.rstrip(', ') + "."
|
|
|
|
|
|
|
|
|
|
| 66 |
return result_string
|