Spaces:
Sleeping
Sleeping
Update helper.py
Browse files
helper.py
CHANGED
|
@@ -1,94 +1,76 @@
|
|
| 1 |
import io
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
-
import requests
|
| 4 |
import inflect
|
| 5 |
from PIL import Image
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
def render_results_in_image(in_pil_img, in_results):
|
| 11 |
-
plt.figure(figsize=(
|
| 12 |
plt.imshow(in_pil_img)
|
| 13 |
-
|
| 14 |
ax = plt.gca()
|
| 15 |
|
| 16 |
for prediction in in_results:
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
x, y =
|
| 19 |
-
w =
|
| 20 |
-
h =
|
| 21 |
|
| 22 |
-
ax.add_patch(plt.Rectangle((x, y),
|
| 23 |
-
w,
|
| 24 |
-
h,
|
| 25 |
fill=False,
|
| 26 |
-
color="
|
| 27 |
linewidth=2))
|
| 28 |
ax.text(
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
plt.axis("off")
|
| 36 |
|
| 37 |
-
# Save
|
| 38 |
-
|
| 39 |
-
plt.savefig(
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
img_buf.seek(0)
|
| 43 |
-
modified_image = Image.open(img_buf)
|
| 44 |
|
| 45 |
-
# Close the plot to prevent it from being displayed
|
| 46 |
plt.close()
|
|
|
|
| 47 |
|
| 48 |
-
return modified_image
|
| 49 |
|
| 50 |
def summarize_predictions_natural_language(predictions):
|
|
|
|
|
|
|
|
|
|
| 51 |
summary = {}
|
| 52 |
p = inflect.engine()
|
| 53 |
|
| 54 |
-
for
|
| 55 |
-
label =
|
| 56 |
-
|
| 57 |
-
summary[label] += 1
|
| 58 |
-
else:
|
| 59 |
-
summary[label] = 1
|
| 60 |
|
| 61 |
-
|
| 62 |
for i, (label, count) in enumerate(summary.items()):
|
| 63 |
-
|
| 64 |
-
|
| 65 |
if count > 1:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
if i == len(summary) - 2:
|
| 71 |
-
result_string += "and "
|
| 72 |
|
| 73 |
-
|
| 74 |
-
result_string = result_string.rstrip(', ') + "."
|
| 75 |
|
| 76 |
-
return result_string
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
##### To ignore warnings #####
|
| 80 |
-
import warnings
|
| 81 |
-
import logging
|
| 82 |
-
from transformers import logging as hf_logging
|
| 83 |
|
| 84 |
def ignore_warnings():
|
| 85 |
-
# Ignore specific Python warnings
|
| 86 |
warnings.filterwarnings("ignore", message="Some weights of the model checkpoint")
|
| 87 |
warnings.filterwarnings("ignore", message="Could not find image processor class")
|
| 88 |
warnings.filterwarnings("ignore", message="The `max_size` parameter is deprecated")
|
| 89 |
-
|
| 90 |
-
# Adjust logging for libraries using the logging module
|
| 91 |
logging.basicConfig(level=logging.ERROR)
|
| 92 |
hf_logging.set_verbosity_error()
|
| 93 |
-
|
| 94 |
-
########
|
|
|
|
| 1 |
import io
|
| 2 |
import matplotlib.pyplot as plt
|
|
|
|
| 3 |
import inflect
|
| 4 |
from PIL import Image
|
| 5 |
+
import warnings
|
| 6 |
+
import logging
|
| 7 |
+
from transformers import logging as hf_logging
|
| 8 |
|
| 9 |
def render_results_in_image(in_pil_img, in_results):
|
| 10 |
+
plt.figure(figsize=(12, 8))
|
| 11 |
plt.imshow(in_pil_img)
|
|
|
|
| 12 |
ax = plt.gca()
|
| 13 |
|
| 14 |
for prediction in in_results:
|
| 15 |
+
box = prediction["box"]
|
| 16 |
+
score = prediction["score"]
|
| 17 |
+
label = prediction["label"]
|
| 18 |
|
| 19 |
+
x, y = box['xmin'], box['ymin']
|
| 20 |
+
w = box['xmax'] - box['xmin']
|
| 21 |
+
h = box['ymax'] - box['ymin']
|
| 22 |
|
| 23 |
+
ax.add_patch(plt.Rectangle((x, y), w, h,
|
|
|
|
|
|
|
| 24 |
fill=False,
|
| 25 |
+
color="lime",
|
| 26 |
linewidth=2))
|
| 27 |
ax.text(
|
| 28 |
+
x, y - 5,
|
| 29 |
+
f"{label}: {score:.2f}",
|
| 30 |
+
color="yellow",
|
| 31 |
+
fontsize=10,
|
| 32 |
+
backgroundcolor="black"
|
| 33 |
)
|
| 34 |
|
| 35 |
plt.axis("off")
|
| 36 |
|
| 37 |
+
# Save to buffer
|
| 38 |
+
buf = io.BytesIO()
|
| 39 |
+
plt.savefig(buf, format="png", bbox_inches="tight", pad_inches=0)
|
| 40 |
+
buf.seek(0)
|
| 41 |
+
modified_img = Image.open(buf)
|
|
|
|
|
|
|
| 42 |
|
|
|
|
| 43 |
plt.close()
|
| 44 |
+
return modified_img
|
| 45 |
|
|
|
|
| 46 |
|
| 47 |
def summarize_predictions_natural_language(predictions):
|
| 48 |
+
if not predictions:
|
| 49 |
+
return "No objects detected."
|
| 50 |
+
|
| 51 |
summary = {}
|
| 52 |
p = inflect.engine()
|
| 53 |
|
| 54 |
+
for pred in predictions:
|
| 55 |
+
label = pred["label"]
|
| 56 |
+
summary[label] = summary.get(label, 0) + 1
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
result = "In this image, there are "
|
| 59 |
for i, (label, count) in enumerate(summary.items()):
|
| 60 |
+
count_str = p.number_to_words(count)
|
| 61 |
+
result += f"{count_str} {label}"
|
| 62 |
if count > 1:
|
| 63 |
+
result += "s"
|
| 64 |
+
if i < len(summary) - 1:
|
| 65 |
+
result += ", "
|
| 66 |
+
result += "."
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
return result
|
|
|
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
def ignore_warnings():
|
|
|
|
| 72 |
warnings.filterwarnings("ignore", message="Some weights of the model checkpoint")
|
| 73 |
warnings.filterwarnings("ignore", message="Could not find image processor class")
|
| 74 |
warnings.filterwarnings("ignore", message="The `max_size` parameter is deprecated")
|
|
|
|
|
|
|
| 75 |
logging.basicConfig(level=logging.ERROR)
|
| 76 |
hf_logging.set_verbosity_error()
|
|
|
|
|
|