I have two question:
- How do I get the correct file
- 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)
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
))