File size: 2,115 Bytes
0a3bbe4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
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
37
38
39
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
67
68
69
70
71
72
73
74
75
76
77
import io
import matplotlib.pyplot as plt
import inflect
from PIL import Image
import warnings
import logging
from transformers import logging as hf_logging

def render_results_in_image(in_pil_img, in_results):
    plt.figure(figsize=(12, 8))
    plt.imshow(in_pil_img)
    ax = plt.gca()

    for prediction in in_results:
        box = prediction["box"]
        score = prediction["score"]
        label = prediction["label"]

        x, y = box['xmin'], box['ymin']
        w = box['xmax'] - box['xmin']
        h = box['ymax'] - box['ymin']

        ax.add_patch(plt.Rectangle((x, y), w, h,
                                   fill=False,
                                   color="lime",
                                   linewidth=2))
        ax.text(
            x, y - 5,
            f"{label}: {score:.2f}",
            color="yellow",
            fontsize=10,
            backgroundcolor="black"
        )

    plt.axis("off")

    # Save to buffer
    buf = io.BytesIO()
    plt.savefig(buf, format="png", bbox_inches="tight", pad_inches=0)
    buf.seek(0)
    modified_img = Image.open(buf)

    plt.close()
    return modified_img


def summarize_predictions_natural_language(predictions):
    if not predictions:
        return "No objects detected."

    summary = {}
    p = inflect.engine()

    for pred in predictions:
        label = pred["label"]
        summary[label] = summary.get(label, 0) + 1

    result = "In this image, there are "
    for i, (label, count) in enumerate(summary.items()):
        count_str = p.number_to_words(count)
        result += f"{count_str} {label}"
        if count > 1:
            result += "s"
        if i < len(summary) - 1:
            result += ", "
    result += "."

    return result


def ignore_warnings():
    warnings.filterwarnings("ignore", message="Some weights of the model checkpoint")
    warnings.filterwarnings("ignore", message="Could not find image processor class")
    warnings.filterwarnings("ignore", message="The `max_size` parameter is deprecated")
    logging.basicConfig(level=logging.ERROR)
    hf_logging.set_verbosity_error()