Spaces:
Runtime error
Runtime error
jawadrashid
commited on
Commit
•
4cde2be
1
Parent(s):
7e27a4c
Create helper.py
Browse files
helper.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import requests
|
4 |
+
import inflect
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
def load_image_from_url(url):
|
8 |
+
return Image.open(requests.get(url, stream=True).raw)
|
9 |
+
|
10 |
+
def render_results_in_image(in_pil_img, in_results):
|
11 |
+
plt.figure(figsize=(16, 10))
|
12 |
+
plt.imshow(in_pil_img)
|
13 |
+
|
14 |
+
ax = plt.gca()
|
15 |
+
|
16 |
+
for prediction in in_results:
|
17 |
+
|
18 |
+
x, y = prediction['box']['xmin'], prediction['box']['ymin']
|
19 |
+
w = prediction['box']['xmax'] - prediction['box']['xmin']
|
20 |
+
h = prediction['box']['ymax'] - prediction['box']['ymin']
|
21 |
+
|
22 |
+
ax.add_patch(plt.Rectangle((x, y),
|
23 |
+
w,
|
24 |
+
h,
|
25 |
+
fill=False,
|
26 |
+
color="green",
|
27 |
+
linewidth=2))
|
28 |
+
ax.text(
|
29 |
+
x,
|
30 |
+
y,
|
31 |
+
f"{prediction['label']}: {round(prediction['score']*100, 1)}%",
|
32 |
+
color='red'
|
33 |
+
)
|
34 |
+
|
35 |
+
plt.axis("off")
|
36 |
+
|
37 |
+
# Save the modified image to a BytesIO object
|
38 |
+
img_buf = io.BytesIO()
|
39 |
+
plt.savefig(img_buf, format='png',
|
40 |
+
bbox_inches='tight',
|
41 |
+
pad_inches=0)
|
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 prediction in predictions:
|
55 |
+
label = prediction['label']
|
56 |
+
if label in summary:
|
57 |
+
summary[label] += 1
|
58 |
+
else:
|
59 |
+
summary[label] = 1
|
60 |
+
|
61 |
+
result_string = "In this image, there are "
|
62 |
+
for i, (label, count) in enumerate(summary.items()):
|
63 |
+
count_string = p.number_to_words(count)
|
64 |
+
result_string += f"{count_string} {label}"
|
65 |
+
if count > 1:
|
66 |
+
result_string += "s"
|
67 |
+
|
68 |
+
result_string += " "
|
69 |
+
|
70 |
+
if i == len(summary) - 2:
|
71 |
+
result_string += "and "
|
72 |
+
|
73 |
+
# Remove the trailing comma and space
|
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 |
+
########
|