-
Notifications
You must be signed in to change notification settings - Fork 1
/
label_detection.py
84 lines (56 loc) · 2.22 KB
/
label_detection.py
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
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 8 15:36:45 2020
@author: SJVD
"""
import boto3
import io
from PIL import Image, ImageDraw
import pandas as pd
import matplotlib.pyplot as plt
def detect_labels(photo, r_aws_access_key_id, r_aws_secret_access_key ):
client=boto3.client('rekognition', aws_access_key_id= r_aws_access_key_id , aws_secret_access_key=r_aws_secret_access_key, region_name='us-east-1')
imageFile= photo
image=Image.open(imageFile)
stream = io.BytesIO()
image.save(stream,format=image.format ,dpi=(30, 30))
image_binary = stream.getvalue()
#Call DetectLabels
response = client.detect_labels(Image={'Bytes':image_binary},MinConfidence=65 ,
MaxLabels=20 )
imgWidth, imgHeight = image.size
draw = ImageDraw.Draw(image)
print('Detected labels for ' + photo)
print()
objects = list()
for label in response['Labels']:
count=0
for instance in label['Instances']:
count+=1
box = instance['BoundingBox']
left = imgWidth * box['Left']
top = imgHeight * box['Top']
width = imgWidth * box['Width']
height = imgHeight * box['Height']
points = (
(left,top),
(left + width, top),
(left + width, top + height),
(left , top + height),
(left, top)
)
points_header = (
(left + width*.4,top-10 )
)
draw.line(points, fill='#1335f2', width=2, joint='curve')
draw.text(points_header, label['Name'], fill='blue' )
x = lambda x: count if x>0 else 1
objects.append((label['Name'], x(count) , label['Confidence'] ))
fig=plt.imshow(image)
plt.axis('off')
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
output_image ='tmp_'+ imageFile
plt.savefig('./www/images/' + output_image , bbox_inches='tight', transparent=True, pad_inches=0, dpi=200)
results = [output_image,len(response['Labels']), pd.DataFrame(objects) ]
return results