Python中的Google Vision Api停止工作-初学者编码器警报

问题描述

我对编码很感兴趣。因此,今年我找到了一份应该从旧事件程序中抓取文字的工作。质量差的图片,以及恐怖的普通OCR结果。我检查了google vision api并对其进行了手动测试,并在非常出色的结果上进行了测试,因此我借此机会学习了编码。(以前我做了一些python,但是缺乏实用性总是让我失望)。

我写了这个程序,我知道它蹲伏在地上,但是它确实可以工作并且完全按照我三个月以来的要求去做。我不定期使用它,但是今天当我想再次使用它时,它不再起作用,它只是跳到程序的末尾,并且在没有发出api请求的情况下完成,至少在我看来是这样。

我实际上已经完成了大部分工作,并且就效率而言,此请求没有任何意义,但我很好奇为什么我创建的程序突然停止工作。

如果有人可以向我提示正确的方向,那么我将不胜感激,如果有人想使用该程序,如果它对他们有用,请确保让它代替您自己:D

我不确定,但是我正在使用linux mint,也许由于某些python或vision-api的更新或其他原因而停止工作。

# coding=utf-8

from google.cloud import vision
import os
import io
import sys



reload(sys)
sys.setdefaultencoding('utf8')

directory = "/home/weareone/Documents/programming/test/here"

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/home/weareone/Documents/programming/test/key.json"

def workinghard(page):

    client = vision.ImageAnnotatorClient()

    #file_name = os.path.join( os.path.dirname(__file__),page) # Loads the image into memory

    #page_er = os.path.abspath(os.path.join(os.path.dirname(page))) <--- my improvisation

    with io.open( page,'rb') as image_file: # after io.open.(file_name <---exchanged with "page")

        content = image_file.read()

    request = {
        "image": {
            "content": content

        
    },"features": [
            {
            "type": "DOCUMENT_TEXT_DETECTION"
            }
        
        ]
    }

    response = client.annotate_image(request)



    storage = response.full_text_annotation.text


    return storage



def listdirs(folder):
    return [
        d for d in (os.path.join(folder,d1) for d1 in os.listdir(folder))
        if os.path.isdir(d)
    ]



directories = listdirs(directory)

for year in directories:

    logtxt = open(year + ".txt","w+" )


    for root,dirs,files in os.walk(year):

        files.sort()
        
        for file23 in files:



            if file23.endswith('.jpg'):

                pathparent = os.path.join(year,file23)

                logtxt.write(workinghard(pathparent))
                
                logtxt.write("-------------------------------------------------------------------------")

                print(pathparent)

            


    logtxt.close()


print("DONE")

非常感谢您亲爱的互联网

编辑:我通过更改此行来解决此问题,该语句明显等于FALSE。 if file23.endswith('.JPG'):

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...