-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_yolo.py
209 lines (176 loc) · 6.75 KB
/
app_yolo.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
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
## https://ithelp.ithome.com.tw/articles/10279933
from flask import Flask, request, abort
#https://pypi.org/project/line-bot-sdk/
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import *
import re
from dataframe import *
import pandas as pd
import urllib.request
import json
from web_crawler.kitchen_story import *
from googletrans import Translator
import cv2 as cv
import torch
torch.hub.set_dir('yolov5')
#from linebot.models.send_messages import ImageSendMessage
#from moon import *
app = Flask(__name__)
# 必須放上自己的Channel Access Token(在line那邊按下issue)
line_bot_api = LineBotApi('Channel Access Token')
# 必須放上自己的Channel Secret
handler = WebhookHandler('Channel Secret')
# 監聽所有來自 /callback 的 Post Request
@app.route("/callback", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
keyword = []
@handler.add(MessageEvent) #, message=TextMessage
def handle_message(event):
global keyword, keyword_copy
if (event.message.type == "image"):
#event.contentProvider.originalContentUrl
model = torch.hub.load('ultralytics/yolov5', 'yolov5l') # yolov5n - yolov5x6 or custom
image_content = line_bot_api.get_message_content(event.message.id)
path='./img/test.jpg'
with open(path, 'wb') as fd:
for chunk in image_content.iter_content():
fd.write(chunk)
results = model(cv.imread('./img/test.jpg',1))
img_corp = results.crop()
keywordtmep = list(set([img_corp[i]['label'].split(" ")[0] for i in range(len(img_corp)) if img_corp[i]['label'].split(" ")[0] not in (['bowl','cup','dining','spoon','sandwich'])]))
translator = Translator()
keywordtmep = [translator.translate(i, dest='zh-TW').text for i in keywordtmep]
#print(keywordtmep)
keyword.extend(keywordtmep)
print(keyword)
else :
# keywordtmep=[event.message.text]
# keyword.extend(keywordtmep)
while "開始搜尋" not in keyword:
temp = [event.message.text]
print(temp)
keyword.extend(temp)
print(keyword)
if "開始搜尋" in keyword:
keyword_copy = keyword[:-1].copy()
keyword = []
break
return keyword_copy
# keyword=event.message.text
#line_bot_api.reply_message(
# event.reply_token,
# TextSendMessage(text="等我一下 我找一下"+event.message.text+"食譜"))
print(keyword_copy)
condition = [['<15', '<30', '>=30'], ['有教學影片', '無教學影片'],['<=3', '<=5'], ['<=3', '<=5', '<=7'], ['按讚數最高', '點擊數最高']]
condition_ingrediant = [j for i in condition for j in i]
condition_filter = []
for i in condition:
x = []
for j in keyword_copy:
if j in i:
x.append([j])
if x != []:
condition_filter += x[-1]
else:
condition_filter += ['']
print(condition_filter)
data = generate_data([i for i in keyword_copy if i not in condition_ingrediant] ,5)
#keyword = ['牛奶', '時間管理大師', '30分鐘內完成', '開始搜尋']
#import pandas as pd
#data = pd.read_csv('D:/recipe/results/data_牛奶.csv')
#時間管理大師
if condition_filter[0]=="<15":
data = data[data.烹飪時間<15]
elif condition_filter[0]=="<30":
data = data[data.烹飪時間<30]
elif condition_filter[0]==">=30":
data = data[data.烹飪時間>=30]
#手把手教學
elif condition_filter[1]=="有教學影片":
if "有無影片" in data.columns :
data = data[data['有無影片'] == '有影片']
else:
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text="請搜尋其他食材"))
elif condition_filter[1]=="無教學影片":
if "有無影片" in data.columns :
data = data[data['有無影片'] != '有影片']
else:
data = data
#熱門食譜
elif condition_filter[4]=="按讚數最多":
data = data.sort_values(["按讚數"],ascending=False)
elif condition_filter[4]=="點擊數最多":
data = data.sort_values(["點擊次數"],ascending=False)
#步驟少一點
elif condition_filter[2]=="三步內":
data = data[data.烹飪步驟<=3]
elif condition_filter[2]=="五步內":
data = data[data.烹飪步驟<=5]
#食材不太夠
elif condition_filter[3]=="<=3":
data = data[data.食材種類 <= 3]
elif condition_filter[3]=="<=5":
data = data[data.食材種類 <= 5]
elif condition_filter[3]=="<=7":
data = data[data.食材種類 <= 7]
#判斷是否有值
if data.empty:
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text="請搜尋其他條件"))
else:
data = data.iloc[:,2:12]
data = data.reset_index(drop = True)
food_name = data.loc[0,'食譜名稱']
food_image = data.loc[0,'圖片連結']
food_url = data.loc[0,'url']
print(food_name)
carousel_template_message = TemplateSendMessage(
alt_text='Carousel template',
template=CarouselTemplate(
columns=[
CarouselColumn(
thumbnail_image_url=food_image,
title=str(food_name),
text='準備材料與做法:...',
actions=[
URIAction(
label='點我看更多',
uri=food_url
)
]
),
]
)
)
# print(carousel_template_message)
# line_bot_api.reply_message(
# event.reply_token,
# TextSendMessage(text=food_url))
#line_bot_api.reply_message(event.reply_token, carousel_template_message)
try:
line_bot_api.reply_message(event.reply_token, carousel_template_message)
except:
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=food_url))
if __name__ == "__main__":
app.debug=False
app.run()
## https://ithelp.ithome.com.tw/articles/10279953?sc=iThomeR