Delete helper.py
Browse files
helper.py
DELETED
|
@@ -1,103 +0,0 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
"""helper.ipynb
|
| 3 |
-
|
| 4 |
-
Automatically generated by Colaboratory.
|
| 5 |
-
|
| 6 |
-
Original file is located at
|
| 7 |
-
https://colab.research.google.com/drive/1IDhEhDLbnCTaBfIbuMtlNFW3ntQiZBwA
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
-
import io
|
| 11 |
-
import matplotlib.pyplot as plt
|
| 12 |
-
import requests
|
| 13 |
-
import inflect
|
| 14 |
-
from PIL import Image
|
| 15 |
-
|
| 16 |
-
def load_image_from_url(url):
|
| 17 |
-
return Image.open(requests.get(url, stream=True).raw)
|
| 18 |
-
|
| 19 |
-
def render_results_in_image(in_pil_img, in_results):
|
| 20 |
-
plt.figure(figsize=(16, 10))
|
| 21 |
-
plt.imshow(in_pil_img)
|
| 22 |
-
|
| 23 |
-
ax = plt.gca()
|
| 24 |
-
|
| 25 |
-
for prediction in in_results:
|
| 26 |
-
|
| 27 |
-
x, y = prediction['box']['xmin'], prediction['box']['ymin']
|
| 28 |
-
w = prediction['box']['xmax'] - prediction['box']['xmin']
|
| 29 |
-
h = prediction['box']['ymax'] - prediction['box']['ymin']
|
| 30 |
-
|
| 31 |
-
ax.add_patch(plt.Rectangle((x, y),
|
| 32 |
-
w,
|
| 33 |
-
h,
|
| 34 |
-
fill=False,
|
| 35 |
-
color="green",
|
| 36 |
-
linewidth=2))
|
| 37 |
-
ax.text(
|
| 38 |
-
x,
|
| 39 |
-
y,
|
| 40 |
-
f"{prediction['label']}: {round(prediction['score']*100, 1)}%",
|
| 41 |
-
color='red'
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
plt.axis("off")
|
| 45 |
-
|
| 46 |
-
# Save the modified image to a BytesIO object
|
| 47 |
-
img_buf = io.BytesIO()
|
| 48 |
-
plt.savefig(img_buf, format='png',
|
| 49 |
-
bbox_inches='tight',
|
| 50 |
-
pad_inches=0)
|
| 51 |
-
img_buf.seek(0)
|
| 52 |
-
modified_image = Image.open(img_buf)
|
| 53 |
-
|
| 54 |
-
# Close the plot to prevent it from being displayed
|
| 55 |
-
plt.close()
|
| 56 |
-
|
| 57 |
-
return modified_image
|
| 58 |
-
|
| 59 |
-
def summarize_predictions_natural_language(predictions):
|
| 60 |
-
summary = {}
|
| 61 |
-
p = inflect.engine()
|
| 62 |
-
|
| 63 |
-
for prediction in predictions:
|
| 64 |
-
label = prediction['label']
|
| 65 |
-
if label in summary:
|
| 66 |
-
summary[label] += 1
|
| 67 |
-
else:
|
| 68 |
-
summary[label] = 1
|
| 69 |
-
|
| 70 |
-
result_string = "In this image, there are "
|
| 71 |
-
for i, (label, count) in enumerate(summary.items()):
|
| 72 |
-
count_string = p.number_to_words(count)
|
| 73 |
-
result_string += f"{count_string} {label}"
|
| 74 |
-
if count > 1:
|
| 75 |
-
result_string += "s"
|
| 76 |
-
|
| 77 |
-
result_string += " "
|
| 78 |
-
|
| 79 |
-
if i == len(summary) - 2:
|
| 80 |
-
result_string += "and "
|
| 81 |
-
|
| 82 |
-
# Remove the trailing comma and space
|
| 83 |
-
result_string = result_string.rstrip(', ') + "."
|
| 84 |
-
|
| 85 |
-
return result_string
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
##### To ignore warnings #####
|
| 89 |
-
import warnings
|
| 90 |
-
import logging
|
| 91 |
-
from transformers import logging as hf_logging
|
| 92 |
-
|
| 93 |
-
def ignore_warnings():
|
| 94 |
-
# Ignore specific Python warnings
|
| 95 |
-
warnings.filterwarnings("ignore", message="Some weights of the model checkpoint")
|
| 96 |
-
warnings.filterwarnings("ignore", message="Could not find image processor class")
|
| 97 |
-
warnings.filterwarnings("ignore", message="The `max_size` parameter is deprecated")
|
| 98 |
-
|
| 99 |
-
# Adjust logging for libraries using the logging module
|
| 100 |
-
logging.basicConfig(level=logging.ERROR)
|
| 101 |
-
hf_logging.set_verbosity_error()
|
| 102 |
-
|
| 103 |
-
########
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|