IndexError: list index out of range use google vision

IndexError: list index out of range use google vision

import io
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="future-shuttle-323208-1e6aebdb018d.json"
# Imports the Google Cloud client library
from google.cloud import vision        
from PIL import Image
import cv2

write1=[]
write2=[]

for i in range(0,235):      

    # Instantiates a client
    client = vision.ImageAnnotatorClient()

    # The name of the image file to annotate
    file_name = os.path.abspath("v2i/%d.png"%(i))

    # Loads the image into memory
    
    with io.open(file_name, 'rb') as image_file:
        content = image_file.read()
    

    image = vision.Image(content=content)

    response = client.text_detection(image=image)
    texts = response.text_annotations
    
    i_str=str(i)
    filename='i2tvision/'+i_str+'.txt'
    
    
    for text in texts:
        
        
        
        write1.append('\n"{}"'.format(text.description))  
        
        vertices = (['({},{})'.format(vertex.x, vertex.y)
                      for vertex in text.bounding_poly.vertices])
        
        write2.append('bounds: {}'.format(','.join(vertices)))
        
        
        #指定文字檔路徑
        
        f = open(filename, 'w')
        f.write(write1[i])
        f.write(write2[i])
        f.close()
       

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

    

    #指定文字檔路徑
    #i_str=str(i)
    #filename='i2tvision/'+i_str+'.txt'
    #f = open(filename, 'w')

    #f.write(write1[i])
    #f.write(write2[i])
    
    #f.close()
    
    

我的目标是使用google视觉将多张照片转换成多个文本文件,但是在转换过程中(在for循环中)我一直遇到一个bug"runfile('/home/lab/chinese/i2tvision .py', wdir= '/home/lab/chinese') 回溯(最近调用最后):

文件“/home/lab/chinese/i2tvision.py”,第 50 行,位于 f.write(write1[i])

IndexError: 列表索引超出范围" 有什么方向可以解决这个问题吗?谢谢

您根本不需要这两个列表。只需在生成它们时写下这些行。

    f = open(filename, 'w')    
    for text in texts:        
        f.write('\n"{}"'.format(text.description))  
        vertices = (['({},{})'.format(vertex.x, vertex.y)
                      for vertex in text.bounding_poly.vertices])
        f.write('bounds: {}'.format(','.join(vertices)))
    f.close()