from telegram.ext import Updater, CommandHandler, MessageHandler, Filters import requests import json import telegram import re import uuid import urllib import cv2 import io import numpy as np import json import LFUtilities import base64 endpoint = "http://bilioso.isti.cnr.it:8190/bcir/searchByImgB64" log_folder = "/media/Data/data/extension/img_recognition/bot" def url_to_file(url): dest_file = uuid.uuid4().hex + ".png" dest_path = log_folder + "/" + dest_file req = urllib.request.Request( url, data=None, headers={ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36' } ) resp = urllib.request.urlopen(req) image = np.asarray(bytearray(resp.read()), dtype="uint8") decoded = cv2.imdecode(image, cv2.IMREAD_COLOR) resized = LFUtilities.resize(500, decoded) cv2.imwrite(dest_path, resized) #im = Image.fromarray(image) #im.save(dest_path) return dest_path def start(update, context): chat_id = update.effective_chat.id # custom_keyboard = [['top-left', 'top-right'], ['bottom-left', 'bottom-right']] # reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) # context.bot.send_message(chat_id=chat_id, text = "Custom Keyboard Test", reply_markup = reply_markup) # context.bot.send_message(chat_id=chat_id, text = "Custom Keyboard Test") photo_id = update.message.photo[-1].file_id query = context.bot.getFile(photo_id) params = {'url': query.file_path} img_file = url_to_file(query.file_path) with open(img_file, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()) # r = requests.get(endpoint, params=params) # json_res = json.loads(r.text) response = "Sorry, I can't recognize it :-(" # if json_res is not None: # title = re.sub("\\d", "", json_res['title']) # response = "*" + json_res['author'] + "*, _" + title + "_\n\n" # response += json_res['artworkPage'] # update.message.reply_text(response) text = endpoint + '?tohtml=true&url=' + query.file_path print(text) context.bot.send_message(chat_id=chat_id, text=text, parse_mode=telegram.ParseMode.HTML) data = {'image': encoded_string, 'securityID': 'FA6mMmeWZ8JjWKz5', 'rescorer': 'false', 'lf_impl': 'beblid'} r = requests.post(endpoint, data=data) text = json.loads(r.text) print(text) #context.bot.send_message(chat_id=chat_id, text='GEM', parse_mode=telegram.ParseMode.HTML) context.bot.send_message(chat_id=chat_id, text=text, parse_mode=telegram.ParseMode.HTML) data = {'image': encoded_string, 'securityID': 'FA6mMmeWZ8JjWKz5', 'rescorer': 'true', 'lf_impl': 'beblid'} r = requests.post(endpoint, data=data) text = json.loads(r.text) print(text) # context.bot.send_message(chat_id=chat_id, text='GEM', parse_mode=telegram.ParseMode.HTML) context.bot.send_message(chat_id=chat_id, text=text, parse_mode=telegram.ParseMode.HTML) def hello(bot, update): update.message.reply_text( 'Hello {}'.format(update.message.from_user.first_name)) def call_service(query): files = {'img': query} r = requests.post(endpoint, files=files) return r.text updater = Updater('2140606085:AAHEBR3a_O9b8E8tbJFaWopDUvT4ptAUkP4') updater.dispatcher.add_handler(MessageHandler(Filters.photo, start)) updater.dispatcher.add_handler(CommandHandler('hello', hello)) updater.start_polling() updater.idle()