【问题标题】:Loop over .csv file containing paths and add labels to second column循环包含路径的 .csv 文件并将标签添加到第二列
【发布时间】:2020-07-26 18:57:37
【问题描述】:

目前我正在尝试谷歌视觉。我收集了相当多的图像,现在我想使用 google vision 对其进行标记。

到目前为止,我有一个 .csv 文件,其中第一列包含图像的路径。现在我正在尝试遍历路径,然后将相应的标签添加到第二列。

import os
import io
from google.cloud import vision
import csv
import pandas as pd
from google.cloud.vision import types


os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r'C:\Users\user\PycharmProjects\Test1\ServiceAccountToken.json'
client = vision.ImageAnnotatorClient()

csvPath = r'C:\Users\user\PycharmProjects\Test1\pictures\Tests.csv'


with io.open(csvPath, 'rb') as csv_file:
    for lines in csv_file:
        with io.open(lines, 'rb') as image_file:
            content = image_file.read()
            image = vision.types.Image(content=content)
            response = client.label_detection(image=image)
            labels = response.label_annotations.description
        

因此,在运行此代码后,我希望这个 .csv 文件包含第二列中的标签,此时我也收到此错误


Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/Test1/googleVisionTests.py", line 17, in <module>
    with io.open(lines, 'r') as image_file:
OSError: [Errno 22] Invalid argument: b'C:/Users/user/PycharmProjects/Test1/pictures/35mm-tR-hmR1ZGmE-unsplash.jpg\r\n'

希望有人能帮忙!

谢谢!

【问题讨论】:

    标签: python csv google-api google-vision


    【解决方案1】:

    替换

    with io.open(csvPath, 'rb') as csv_file:
    

    with io.open(csvPath, 'r') as csv_file:
    

    'rb' 以二进制模式打开文件,这里不需要 csv 文件

    【讨论】:

      猜你喜欢
      • 2019-08-01
      • 1970-01-01
      • 2016-02-20
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      • 1970-01-01
      • 2021-03-09
      相关资源
      最近更新 更多