mahmudunnabi commited on
Commit
0db89ac
·
verified ·
1 Parent(s): 4716bb5

Update detection_utils.py

Browse files
Files changed (1) hide show
  1. detection_utils.py +65 -68
detection_utils.py CHANGED
@@ -1,69 +1,66 @@
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
- x, y = prediction['box']['xmin'], prediction['box']['ymin']
18
- w = prediction['box']['xmax'] - prediction['box']['xmin']
19
- h = prediction['box']['ymax'] - prediction['box']['ymin']
20
-
21
- ax.add_patch(plt.Rectangle(
22
- (x,y),
23
- w,
24
- h,
25
- fill = False,
26
- color = 'green',
27
- linewidth = 2
28
- ))
29
-
30
- ax.text(
31
- x,
32
- y,
33
- f"{prediction['label']}: {round(prediction['score']*100,1)}%",
34
- color = 'red'
35
- )
36
- plt.axis('off')
37
-
38
- img_buf = io.BytesIO()
39
- plt.savefig(img_buf, format = 'png', bbox_inches = 'tight', pad_inches = 0)
40
- img_buf.seek(0)
41
- modified_image = Image.open(img_buf)
42
- plt.close()
43
- return modified_image
44
-
45
-
46
- def summarize_predictions_natural_language(predictions):
47
- summary = {}
48
- p = inflect.engine()
49
-
50
- for prediction in predictions:
51
- label = prediction['label']
52
- if label in summary:
53
- summary[label] += 1
54
- else:
55
- summary[label] = 1
56
-
57
- result_string = "In this image, there are "
58
- for i, (label, count) in enumerate(summary.items()):
59
- count_string = p.number_to_words(count)
60
- result_string += f"{count_string} {label}"
61
-
62
- if count > 1:
63
- result_string += 's'
64
- result_string += " "
65
-
66
- if i == len(summary) - 2:
67
- result_string += "and "
68
- result_string = result_string.rstrip(', ') + "."
69
  return result_string
 
1
+ import io
2
+ import matplotlib.pyplot as plt
3
+ import inflect
4
+ from PIL import Image
5
+
6
+
7
+ def render_results_in_image(in_pil_img, in_results):
8
+ plt.figure(figsize=(16,10))
9
+ plt.imshow(in_pil_img)
10
+
11
+ ax = plt.gca()
12
+
13
+ for prediction in in_results:
14
+ x, y = prediction['box']['xmin'], prediction['box']['ymin']
15
+ w = prediction['box']['xmax'] - prediction['box']['xmin']
16
+ h = prediction['box']['ymax'] - prediction['box']['ymin']
17
+
18
+ ax.add_patch(plt.Rectangle(
19
+ (x,y),
20
+ w,
21
+ h,
22
+ fill = False,
23
+ color = 'green',
24
+ linewidth = 2
25
+ ))
26
+
27
+ ax.text(
28
+ x,
29
+ y,
30
+ f"{prediction['label']}: {round(prediction['score']*100,1)}%",
31
+ color = 'red'
32
+ )
33
+ plt.axis('off')
34
+
35
+ img_buf = io.BytesIO()
36
+ plt.savefig(img_buf, format = 'png', bbox_inches = 'tight', pad_inches = 0)
37
+ img_buf.seek(0)
38
+ modified_image = Image.open(img_buf)
39
+ plt.close()
40
+ return modified_image
41
+
42
+
43
+ def summarize_predictions_natural_language(predictions):
44
+ summary = {}
45
+ p = inflect.engine()
46
+
47
+ for prediction in predictions:
48
+ label = prediction['label']
49
+ if label in summary:
50
+ summary[label] += 1
51
+ else:
52
+ summary[label] = 1
53
+
54
+ result_string = "In this image, there are "
55
+ for i, (label, count) in enumerate(summary.items()):
56
+ count_string = p.number_to_words(count)
57
+ result_string += f"{count_string} {label}"
58
+
59
+ if count > 1:
60
+ result_string += 's'
61
+ result_string += " "
62
+
63
+ if i == len(summary) - 2:
64
+ result_string += "and "
65
+ result_string = result_string.rstrip(', ') + "."
 
 
 
66
  return result_string