0

I have two question:

  1. How do I get the correct file
  2. I know reading a jpg file may be different from a text, is there something in the code I need to be specially aware of?

This is on a line-bot application, I need to be able to read a jpg file that is uploaded as soon as the bot receieved the image from an end user.

I am using GCS filesystem to programmatically talk to GCS in Python once it uploads, but it's not scuuessful (I have revised my code a little bit), it give a new error message saying can't find the file:

  File "C:\officiallandprice\myproject\myenv\lib\site-packages\gcsfs\core.py", line 415, in _request
    validate_response(status, contents, path, args)
  File "C:\officiallandprice\myproject\myenv\lib\site-packages\gcsfs\retry.py", line 84, in validate_response
    raise FileNotFoundError(path)
FileNotFoundError: b/img_platecapture/o/%7Buser_id%7D.jpg

my blob name relates to the end user's line bot user_id. and the one it uploads is completely different than the one it reads(I am using the same parameter)

Blockquote

from google.cloud import storage
from google.cloud import vision
from PIL import Image, ImageDraw
from PIL import ImageFont
import fsspec
import gcsfs





def text_detected(user_id):
    fs=gcsfs.GCSFileSystem(project='My Project GoogleMap')
    with fs.open('img_platecapture/{user_id}.jpg','rb') as f:    #to get the file
        content=f.read()                                          #reading its content

    image = vision.Image(content=content)
    response = vision_client.text_detection(image=image)

    if response.error.message:
        raise Exception(
            '{}\nFor more info on error messages, check: '
            'https://cloud.google.com/apis/design/errors'.format(
                response.error.message))

    img = Image.open('img_platecapture/{user_id}.jpg')

     ...........

@handler.add(MessageEvent, message=ImageMessage)
def handle_content_message(event):
    message_content = line_bot_api.get_message_content(event.message.id)
    user = line_bot_api.get_profile(event.source.user_id) 

    data=b''
    for chunk in message_content.iter_content():
       data+= chunk

    global bucket_name   
    bucket_name = 'img_platecapture'
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(f'{user.user_id}.jpg')
    blob.upload_from_string(data)                      #upload the file
    text_detected1=text_detected(user.user_id)
    
    line_bot_api.reply_message(
        event.reply_token,
        messages=TextSendMessage(
            text=text_detected1
    ))

april
  • 53
  • 1
  • 7
  • Have a look at this [thread](https://www.datasciencelearner.com/typeerror-takes-1-positional-argument-but-2-were-given-solved/) and this stackoverflow [link1](https://stackoverflow.com/questions/73328296/typeerror-person-takes-1-positional-argument-but-2-were-given) & [link2](https://stackoverflow.com/questions/23944657/typeerror-method-takes-1-positional-argument-but-2-were-given) – Sathi Aiswarya Dec 01 '22 at 13:48
  • @SathiAiswarya thank you for the reply and the great reference link! I have reviced my code a little bit, if possible plz give me some more advice. Thank you! – april Dec 02 '22 at 01:24
  • can you go through this stackoverflow [thread](https://stackoverflow.com/questions/74605414/how-to-read-a-jpg-from-google-storage-as-a-path-or-file-type) once – Sathi Aiswarya Dec 05 '22 at 12:20
  • @SathiAiswarya Thank you for the reply, I think I’ve got some ideas – april Dec 06 '22 at 06:19
  • @SathiAiswarya I am now planning to deploy my code on google cloud function, so I think it might not be suitable to use downloas to file(), and I am trying on using gcsfs to get the file for PIL module to execute, (though, have little problem on python code), hope it might work when I figure out the code. Appreciate for your asking. – april Dec 06 '22 at 17:03

0 Answers0