1

I am writing a telegram bot, one of the functions of which is face recognition. The user sends a photo to the chat. Is it possible to send it as a file to the function parameter without saving the photo to the database?

I have a class "PhotoAnalyser" and a function in it "analyse_photo":

class PhotoAnalyser:

    @staticmethod
    def analyse_photo(file):

        temp = []
        for prediction in model.predict(file, confidence=30, overlap=30).json()['predictions']:
            temp.append(prediction['class'])
        model.predict(file, confidence=30, overlap=30).save(f'{file}_prediction.jpg')
        return temp

I tried to send a photo as message.photo PhotoAnalyser.analyse_photo(message.photo); by ID PhotoAnalyser.analyse_photo(message.photo[0].file_id). But an error popped up that the value should be either str or nparray. Altered the type under nparray, writes that it cannot read the value.

I tried to remove the json() method in the loop in the function "analyse_photo", but that didn't help either.

In this regard, I decided to find out if there is a way to send a photo immediately as a file

1 Answers1

0

First, in the handler we write the following

file: File = await bot.get_file(message.photo[-1].file_id)
download_file: BinaryIO = await bot.download_file(file.file_path)

Pass the download file to the parameter of the desired function

In the function itself, we process the passed download as follows:

photo: Image = Image.open(photo)
with tempfile.NamedTemporaryFile(suffix='.jpg', delete=False) as tmp:
     temp_file_path: str = tmp.name
     self.photo.save(tmp.name)

Now we have a photo file in the temporary storage of the system.