-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLINE.py
More file actions
265 lines (250 loc) · 10.4 KB
/
LINE.py
File metadata and controls
265 lines (250 loc) · 10.4 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import *
from bs4 import BeautifulSoup
from urllib.parse import parse_qs
import requests
import json
import uuid
import boto3
import os
import re
s3 = boto3.client(
's3',
aws_access_key_id=os.environ['aws_access_key_id'],
aws_secret_access_key=os.environ['aws_secret_access_key'],
region_name=os.environ['region_name']
)
class FaceFinder:
def __init__(self, img) :
self.s3_client = boto3.client(
's3',
aws_access_key_id=os.environ['aws_access_key_id'],
aws_secret_access_key=os.environ['aws_secret_access_key'],
region_name=os.environ['region_name']
)
self.rek = boto3.client(
'rekognition',
aws_access_key_id=os.environ['aws_access_key_id'],
aws_secret_access_key=os.environ['aws_secret_access_key'],
region_name=os.environ['region_name']
)
self.img = img
def index_from_collection(self, MaxFaces=3, FaceMatchThreshold=30):
collection = []
with open(self.img, 'rb') as f :
response = self.rek.search_faces_by_image(
CollectionId=os.environ['CollectionId'],
Image={
'Bytes':f.read()
},
MaxFaces=MaxFaces,
FaceMatchThreshold=FaceMatchThreshold
)
if response['ResponseMetadata']['HTTPStatusCode'] == 200 :
for face in response['FaceMatches'] :
collection.append(
{
'ImgURL': 'https://%s.s3-ap-northeast-1.amazonaws.com/NSFW/%s.jpeg'%(os.environ['Bucket'], face['Face']['ExternalImageId']),
'Similarity': face['Similarity'],
'TagSet': self.s3_client.get_object_tagging(Bucket=os.environ['Bucket'],Key='NSFW/%s.jpeg'%face['Face']['ExternalImageId'])['TagSet']
}
)
return collection
def index_from_celebrities(self, threshold=30):
collection = []
with open(self.img, 'rb') as f :
response = self.rek.recognize_celebrities(
Image={
'Bytes':f.read()
}
)
if response['ResponseMetadata']['HTTPStatusCode'] == 200 :
print(response['CelebrityFaces'])
for face in response['CelebrityFaces'] :
# Get matched celebrity image.
r = requests.get('https://' + face['Urls'][0])
soup = BeautifulSoup(r.content, 'html.parser')
ele = soup.find_all(id='name-poster')
if ele :
ImgURL = ele[0]['src']
collection.append(
{
'ImgURL': ImgURL,
'Similarity': face['MatchConfidence'],
'TagSet': [{'Value' : face['Name']}]
}
)
return collection
class FileIO :
@classmethod
def write_image_from_message(self, message_content) :
with open('/tmp/user_upload.jpeg', 'wb') as f :
for chunk in message_content.iter_content() :
f.write(chunk)
return '/tmp/user_upload.jpeg'
@classmethod
def write_token_json(self, token, result) :
with open('/tmp/%s.json'%token, 'w') as f :
json.dump(result, f)
return '/tmp/%s.json'%token
def lambda_handler(even, context) :
for e in json.loads(even['body'])['events'] :
if e['type'] == 'message' :
if e['message']['type'] == 'image':
# LINE authentication
line_bot_api = LineBotApi(os.environ['channel_access_token'])
handler = WebhookHandler(os.environ['channel_secret'])
# extract image from message
message_id = e['message']['id']
message_content = line_bot_api.get_message_content(message_id)
image_path = FileIO.write_image_from_message(message_content)
# image processing
FF = FaceFinder(image_path)
other = FF.index_from_collection()
celebrity = FF.index_from_celebrities()
print(other, celebrity)
# generate token and write static to s3
token = uuid.uuid4().hex
line_bot_api.reply_message(e['replyToken'], generateOption(token))
file_path = FileIO.write_token_json(token, {
'other': other,
'celebrity': celebrity
})
FF.s3_client.upload_file(
Filename=file_path,
Bucket=os.environ['Bucket'],
Key='to-push/' + token + '.json',
ExtraArgs={'ACL': 'public-read'}
)
elif e['type'] == 'postback' :
# LINE authentication
line_bot_api = LineBotApi(os.environ['channel_access_token'])
handler = WebhookHandler(os.environ['channel_secret'])
params = {k : v for k, v in parse_qs(e['postback']['data']).items()}
if params :
data = requests.get('https://%s.s3-ap-northeast-1.amazonaws.com/to-push/%s.json'%(os.environ['Bucket'], params['token'][0])).json()
result = data[params['type'][0]]
if result :
contents = FlexSendMessage(alt_text='result', contents=generateReply(result))
else :
contents = TextSendMessage(text='Sorry, No similar face has been found in %s'%params['type'][0])
line_bot_api.reply_message(e['replyToken'], contents)
def generateOption(token) :
return TemplateSendMessage(
alt_text="Select your checking reference.",
template=ConfirmTemplate(
text="Select your checking reference.",
actions=[
PostbackAction(
label="Celebrities",
data="token=%s&type=celebrity"%token,
text="Celebrities"
),
PostbackAction(
label="Other",
data="token=%s&type=other"%token,
text="Other"
)
]
)
)
def generateReply(faces) :
carousel = {
"type": "carousel",
"contents": []
}
for face in faces :
carousel['contents'].append(
{
"type": "bubble",
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "image",
"url": "%s"%face["ImgURL"],
"size": "full",
"aspectMode": "cover",
"aspectRatio": "1:1",
"gravity": "center"
},
{
"type": "image",
"url": "https://scdn.line-apps.com/n/channel_devcenter/img/flexsnapshot/clip/clip15.png",
"position": "absolute",
"aspectMode": "fit",
"aspectRatio": "1:1",
"offsetTop": "0px",
"offsetBottom": "0px",
"offsetStart": "0px",
"offsetEnd": "0px",
"size": "full"
},
{
"type": "box",
"layout": "horizontal",
"contents": [
{
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "box",
"layout": "horizontal",
"contents": [
{
"type": "text",
"text": x['Value'],
"size": "md",
"color": "#ffffff"
} for x in face["TagSet"]
]
},
{
"type": "text",
"text": "{}%".format(int(face['Similarity'])),
"color": "#ffffff",
"align": "start",
"size": "xs",
"gravity": "center",
"margin": "lg"
},
{
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "filler"
}
],
"width": "{}%".format(int(face['Similarity'])),
"backgroundColor": "#808080",
"height": "6px"
}
],
"backgroundColor": "#DCDCDC",
"height": "6px",
"margin": "sm"
}
],
"spacing": "xs"
}
],
"position": "absolute",
"offsetBottom": "0px",
"offsetStart": "0px",
"offsetEnd": "0px",
"paddingAll": "20px"
}
],
"paddingAll": "0px"
}
}
)
return carousel