You can teach your bot to detect what is in the photo. This function can be used for example in channels or chats. To define objects in pictures, you can use the Clarifai service. Below is the code for the Clarifai client in Python.
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 |
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel from clarifai_grpc.grpc.api import service_pb2_grpc, service_pb2, resources_pb2 stub = service_pb2_grpc.V2Stub(ClarifaiChannel.get_grpc_channel()) from clarifai_grpc.grpc.api.status import status_code_pb2 from human_checker import human_analyzer from food_analyzer import food_analyzer metadata = (('authorization', 'Key <your clarifai key>'),) def what_is_photo(filename): answer='' try: #image = Image(file_obj=open(filename, 'rb')) with open(filename, "rb") as f: file_bytes = f.read() request = service_pb2.PostModelOutputsRequest( model_id='aaa03c23b3724a16a56b629203edc62c', inputs=[ resources_pb2.Input(data=resources_pb2.Data(image = resources_pb2.Image(base64=file_bytes))) ]) response = stub.PostModelOutputs(request, metadata=metadata) if response.status.code != status_code_pb2.SUCCESS: raise Exception("Request failed, status code: " + str(response.status.code)) return False i=0 for concept in response.outputs[0].data.concepts: i=i+1 answer = answer + concept.name + ' : '+ str(round(concept.value,2)) +'\n' if i == 20: break answer = 'i see \n\n' + answer return answer except Exception as err: print(err) return False if __name__ == "__main__": print(what_is_photo("downloads/food.jpg")) |
The telegram bot code with the image detection function is below.
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 |
import settings from telegram.ext import * from telegram import * from google.cloud import dialogflow from google.api_core.exceptions import InvalidArgument import os from photo_checker import what_is_photo DIALOGFLOW_PROJECT_ID = 'your dialogflow project id' DIALOGFLOW_LANGUAGE_CODE = 'en' SESSION_ID = 'current-user-id' os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.getcwd()+"/your json key.json" def start(update, context): update.message.reply_text("Hi! I am a bot with Google Dialogflow and Clarifai. Write me something or send a picture.") def echo(update, context): text_to_be_analyzed = update.message.text session_client = dialogflow.SessionsClient() session = session_client.session_path(DIALOGFLOW_PROJECT_ID, SESSION_ID) text_input = dialogflow.TextInput(text=text_to_be_analyzed, language_code=DIALOGFLOW_LANGUAGE_CODE) query_input = dialogflow.QueryInput(text=text_input) try: try: response = session_client.detect_intent(session=session, query_input=query_input) except InvalidArgument: update.message.reply_text('Something went wrong') return update.message.reply_text (response.query_result.fulfillment_text) except Exception as err: update.message.reply_text(err) def what_is_in_photo(update, context): update.message.reply_text('One moment please') photo_file = context.bot.get_file(update.message.photo[-1].file_id) filename = os.path.join('/downloads', '{}.jpg'.format(photo_file.file_id)) photo_file.download(filename) message_photo = what_is_photo(filename) if message_photo: update.message.reply_text(message_photo) else: update.message.reply_text('Sorry. I do not know what is in the picture') def main(): updater = Updater(settings.TELEGRAM_API_KEY, use_context=True) dp = updater.dispatcher dp.add_handler(CommandHandler("start", start)) dp.add_handler(MessageHandler(Filters.text, echo)) dp.add_handler(MessageHandler(Filters.photo, what_is_in_photo)) # Start the Bot updater.start_polling() updater.idle() if __name__ == '__main__': main() |
You can check it yourself by sending a photo to the @Porphyry_bot in Telegram
182