dlaima commited on
Commit
aed2186
·
verified ·
1 Parent(s): aee21f5

Update helper.py

Browse files
Files changed (1) hide show
  1. helper.py +37 -55
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
- 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
- ########
 
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()