-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
329 lines (257 loc) · 9.3 KB
/
Copy pathmain.py
File metadata and controls
329 lines (257 loc) · 9.3 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from flask import Flask, request, abort
from linebot import (LineBotApi, WebhookHandler)
from linebot.exceptions import (InvalidSignatureError)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
SourceUser, SourceGroup, SourceRoom,
TemplateSendMessage, ConfirmTemplate, MessageAction,
ButtonsTemplate, ImageCarouselTemplate, ImageCarouselColumn, URIAction,
PostbackAction, DatetimePickerAction,
CameraAction, CameraRollAction, LocationAction,
CarouselTemplate, CarouselColumn, PostbackEvent,
StickerMessage, StickerSendMessage, LocationMessage, LocationSendMessage,
ImageMessage, VideoMessage, AudioMessage, FileMessage,
UnfollowEvent, FollowEvent, JoinEvent, LeaveEvent, BeaconEvent,
FlexSendMessage, BubbleContainer, ImageComponent, BoxComponent,
TextComponent, SpacerComponent, IconComponent, ButtonComponent,
SeparatorComponent, QuickReply, QuickReplyButton
)
import os
from io import BytesIO
from tinydb import TinyDB, Query
from PIL import Image
import numpy as np
import base64
app = Flask(__name__)
# 環境変数からLINEのトークンを読み込み
YOUR_CHANNEL_ACCESS_TOKEN = os.environ["CHANNEL_ACCESS_TOKEN"]
YOUR_CHANNEL_SECRET = os.environ["CHANNEL_SECRET"]
line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(YOUR_CHANNEL_SECRET)
#データベースの作成
db = TinyDB('userData.json')
_diff = 20
class ColorDot:
color = []
count = 0
name = ""
def __init__(self, col, name):
self.color = np.array(col)
self.name = name
class DotsColorList:
ColorList = []
def addColor(self, col, name):
item = ColorDot(col, name)
self.ColorList.append(item)
def removeColor(self):
self.ColorList.clear()
def searchDot(self, col):
for c in self.ColorList:
if (col == c.color).all():
c.count += 1
def searchDotNear(self, col):
for c in self.ColorList:
ival = True
for (coli, ci) in zip(col, c.color):
if (not (int(ci) <= int(coli) + _diff and int(ci) >= int(coli) - _diff)):
ival = False
if (ival):
c.count += 1
def outputPrint(self):
for c in self.ColorList:
print(c.name + " " + str(c.count))
def outputPrintRatio(self):
default = float(self.ColorList[0].count)+0.000001
for c in self.ColorList:
print(c.name + " " + '{:.4f}'.format(float(c.count) / default))
def outputStringRatio(self):
mes = ""
default = float(self.ColorList[0].count)
for c in self.ColorList:
mes += c.name + " " + \
'{:.4f}'.format(float(c.count) / default) + "\n"
return mes
def push_textMessage(user_id,texts):
line_bot_api.push_message(to=user_id,
messages=TextSendMessage(text=texts)
)
bubble = BubbleContainer(
direction='ltr',
body=BoxComponent(
layout='vertical',
contents=[
TextComponent(text='ルートで使う色を登録してね', weight='bold', size='xl')
]
),
footer=BoxComponent(
layout='vertical',
spacing='sm',
contents=[
SpacerComponent(size='sm'),
ButtonComponent(
style='primary', height='sm',
action=PostbackAction(label='赤 ff0000', data='c_red'),
color='#ff4444'
),
SeparatorComponent(),
ButtonComponent(
style='primary', height='sm',
action=PostbackAction(label='青 0000ff', data='c_blue'),
color='#4444ff'
),
SeparatorComponent(),
ButtonComponent(
style='secondary', height='sm',
action=PostbackAction(label='黄 ffff00', data='c_yellow'),
color='#ffff44'
),
SeparatorComponent(),
ButtonComponent(
style='secondary', height='sm',
action=PostbackAction(label='マゼンタ ff00ff', data='c_mazenta'),
color='#ff44ff'
),
SeparatorComponent(),
ButtonComponent(
style='secondary', height='sm',
action=PostbackAction(label='シアン 00ffff', data='c_cian'),
color='#44ffff'
),
SeparatorComponent(),
ButtonComponent(
style='primary',margin='xxl',
action=PostbackAction(label='比較実行', data='run')
)
]
)
)
# flex message送るだけ
def flexMessage(event):
print("sendFlex")
reply_txt = ""
Flexmessage = FlexSendMessage(alt_text="hello", contents=bubble)
line_bot_api.reply_message(
event.reply_token,
Flexmessage
)
@app.route("/")
def hello():
return "Hello World!"
# LINE APIにアプリがあることを知らせるためのもの
@app.route("/callback", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
# メッセージが来た時の反応
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
message_txt = event.message.text
user_id = event.source.user_id
print("user_id : " + user_id)
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text="ルート画像を送ってね"))
# ポストバックイベントでカラーを登録する
@handler.add(PostbackEvent)
def handle_postback(event):
reply_token = event.reply_token
user_id = event.source.user_id
postback_msg = event.postback.data
print("user_id : " + user_id)
que = Query()
if not (db.search(que.id == user_id) ):
push_textMessage(user_id, "先に画像を上げてね!")
return
reply_txt = "エラー 色が存在しません。"
if postback_msg == 'run':
userdat = db.search(que.id == user_id)
colors = userdat[0]['color']
img = userdat[0]['imgbin']
if len(colors) > 0:
reply_txt = RunCompareLines(user_id,img,colors)
db.remove(que.id == user_id)
else:
reply_txt = "エラー ルートの色が登録されてないよ"
else:
userdat = db.search(que.id == user_id)
colors = userdat[0]['color']
if postback_msg == 'c_red':
colors.append([[255, 0, 0], "red"])
reply_txt = "赤色を登録したよ"
elif postback_msg == 'c_blue':
reply_txt = "青色を登録したよ"
colors.append([[0, 0, 255], "blue"])
elif postback_msg == 'c_yellow':
reply_txt = "黄色を登録したよ"
colors.append([[255, 255, 0], "Yellow"])
elif postback_msg == 'c_cian':
reply_txt = "シアンを登録したよ"
colors.append([[0, 255, 255], "Cian"])
elif postback_msg == 'c_mazenta':
reply_txt = "マゼンタを登録したよ"
colors.append([[255, 0, 255], "Mazenta"])
db.update({'color':colors}, que.id == user_id)
push_textMessage(user_id, reply_txt)
# 画像が来たときの反応
@handler.add(MessageEvent, message=ImageMessage)
def handle_image(event):
message_id = event.message.id
user_id = event.source.user_id
img = line_bot_api.get_message_content(message_id)
print("user_id : " + user_id)
que = Query()
if db.search(que.id == user_id):
db.remove(que.id == user_id)
db.insert({
'id':user_id,
'color':[],
'imgbin':base64.b64encode(img.content).decode('utf-8')
})
# flex messageを送信
flexMessage(event)
def RunCompareLines(userid,img,colors):
# # Pillowで開く
img_binary = base64.b64decode(img)
img_binary = BytesIO(img_binary)
img = Image.open(img_binary)
# # こっから処理
width, height = img.size
if (width * height > 700000):
reply_txt = "画像サイズが大きすぎるよ..!!!"
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=reply_txt))
img_pixels = []
for i in range(height * width):
# getpixel((x,y))で左からx番目,上からy番目のピクセルの色を取得し、img_pixelsに追加する
pixcel = img.getpixel((i % width, i / width))
pix = []
pix.append(pixcel[0])
pix.append(pixcel[1])
pix.append(pixcel[2])
pix = np.array(pix)
img_pixels.append(pix)
# あとで計算しやすいようにnumpyのarrayに変換しておく
img_pixels = np.array(img_pixels)
print(" ---------- ")
dotsColorList = DotsColorList()
for c in colors:
dotsColorList.addColor(c[0], c[1])
cnt = 0
method = dotsColorList.searchDotNear
# 各色の面積をカウント
# ボトルネック
for i in img_pixels:
method(i)
reply_txt = dotsColorList.outputStringRatio()
dotsColorList.removeColor()
return reply_txt
if __name__ == "__main__":
port = int(os.getenv("PORT", 5000))
app.run(host="0.0.0.0", port=port)