-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
120 lines (89 loc) · 3.31 KB
/
app.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
## 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
import pandas as pd
import urllib.request
import json
from web_crawler.combine_all 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')
#message_content = line_bot_api.get_message_content('<message_id>')
# 必須放上自己的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'
@handler.add(MessageEvent) #, message=TextMessage
def handle_message(event):
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()
keyword = 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()
keyword = [translator.translate(i, dest='zh-TW').text for i in keyword]
else :
keyword=event.message.text
if type(keyword)==list:
data=generate_data(keyword,5)
else :
data=generate_data([keyword],5)
food_name=data['Name'][0]
food_material=data['ingredients'][0]
food_image=data['img'][0]
food_method=data['steps'][0]
food_url=data["URL"][0]
carousel_template_message = TemplateSendMessage(
alt_text='Carousel template',
template=CarouselTemplate(
columns=[
CarouselColumn(
thumbnail_image_url=food_image,
title=str(food_name),
# text = str(keyword),
text=f'{keyword} - 準備材料與做法:...',
actions=[
URIAction(
label='點我看更多',
uri=food_url
)
]
)
]
)
)
line_bot_api.reply_message(event.reply_token, carousel_template_message)
if __name__ == "__main__":
app.debug=True
app.run()
## https://ithelp.ithome.com.tw/articles/10279953?sc=iThomeR