Spaces:
Sleeping
Sleeping
Delete helper.py
Browse files
helper.py
DELETED
|
@@ -1,76 +0,0 @@
|
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|