简体   繁体   English

使用 PIL 模块从 GCS 打开文件

[英]Using PIL module to open file from GCS

I am a beginner in programming, and this is my first little try.我是编程初学者,这是我的第一次小尝试。 I'm currently facing a bottleneck, I would like to ask for the help.我目前面临瓶颈,我想寻求帮助。 Any advice will be welcome.欢迎任何建议。 Thank you in advance!先感谢您!

Here is what I want to do:这是我想要做的:

To make a text detection application and extract the text for the further usage(for instance, to map some of the other relevant information in a data).做一个文本检测应用程序,提取文本以供进一步使用(例如,到 map 数据中的一些其他相关信息)。 So, I devided into two steps: 1.first, to detect the text 2.extract the text and use the regular expression to rearrange it for the data mapping.所以,我分为两个步骤: 1.首先,检测文本 2.提取文本并使用正则表达式重新排列它以进行数据映射。

For the first step, I use google vision api, so I have no probelm reading the image from google cloud storage(code reference 1):第一步,我使用的是google vision api,所以我没有问题从google云存储中读取图像(代码参考1):

However, when it comes to step two, I need a PIL module to open the file for drawing the text.但是,当涉及到第二步时,我需要一个PIL 模块来打开用于绘制文本的文件 When useing the method Image.open() , it requries a path `.使用方法Image.open()时,它需要一个路径`. My question is how do I call the path?我的问题是如何调用路径? (code reference 2): (代码参考2):

code reference 1:代码参考1:

from google.cloud import vision

    image_uri = 'gs://img_platecapture/img_001.jpg'
    client = vision.ImageAnnotatorClient()
    image = vision.Image()
    image.source.image_uri = image_uri  ##  <- THE PATH  ##

    response = client.text_detection(image=image)
    for text in response.text_annotations:
        print('=' * 30)
        print(text.description)
        vertices = ['(%s,%s)' % (v.x, v.y) for v in text.bounding_poly.vertices]
        print('bounds:', ",".join(vertices))

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

code reference 2:代码参考2:

from PIL import Image, ImageDraw
from PIL import ImageFont
import re

img = Image.open(?)                        <- THE PATH  ##
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("simsun.ttc", 18)

for text in response.text_annotations[1::]:
  ocr = text.description
  bound=text.bounding_poly    
  draw.text((bound.vertices[0].x-25, bound.vertices[0].y-25),ocr,fill=(255,0,0),font=font)     
        
  draw.polygon(
         [
             bound.vertices[0].x,
             bound.vertices[0].y,
             bound.vertices[1].x,
             bound.vertices[1].y,
             bound.vertices[2].x,
             bound.vertices[2].y,
             bound.vertices[3].x,
             bound.vertices[3].y,
         ],
         None,
         'yellow',
       
         )
  texts=response.text_annotations

  a=str(texts[0].description.split())
  b=re.sub(u"([^\u4e00-\u9fa5\u0030-u0039])","",a) 
    b1="".join(b)
  

    regex1 = re.search(r"\D{1,2}Dist.",b) 
    if regex1:
        regex1="{}".format(regex1.group(0))

     .........

PIL does not have built in ability to automatically open files from GCS. PIL没有从 GCS 自动打开文件的内置功能。 you will need to either你需要

  1. Download the file to local storage and point PIL to that file or将文件下载到本地存储并将 PIL 指向该文件或

  2. Give PIL a BlobReader which it can use to access the data:为 PIL 提供一个可用于访问数据的BlobReader

     from PIL import Image from google.cloud import storage storage_client = storage.Client() bucket = storage_client.bucket('img_platecapture') blob = bucket.get_blob('img_001.jpg') # use get_blob to fix generation number, so we don't get corruption if blob is overwritten while we read it. with blob.open() as file: img = Image.open(file) #...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 spark-java 从 GCS 读取 csv 文件? - How to read csv file from GCS using spark-java? 将数据从 GCS 中的文件加载到 GCP firestore - Loading data from a file in GCS to GCP firestore 使用 python 将文件上传到 gcs 存储桶中的文件夹 - upload file to a folder in gcs bucket using python 使用 Cloud function 从 url 下载大文件 (800MB) 到 GCS 桶中 - Downloading big file(800MB) from url into GCS bucket using Cloud function Url 使用 Java 从 GCS 存储桶下载文件后仍然可以访问 - Url to download file from GCS bucket still accessible after deleting using Java 使用 scala 获取 GCS 文件元数据 - get GCS file metadata using scala Airflow 将最新文件从 GCS 存储桶复制到本地 - Airflow to copy most recent file from GCS bucket to local 将文件从 Power Automate 流上传到 GCS 存储桶 - Upload a file from a Power Automate flow into a GCS bucket 使用 Cloud Functions 中的服务帐户将数据从 GCS 上传到 Bigquery - Upload data from GCS to Bigquery using service account in Cloud Functions 如何从 gcs 存储桶中解压缩 tsv 文件并将其加载到 Bigquery - How to unzip and load tsv file into Bigquery from gcs bucket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM