Я пытаюсь прочитать свойства изображения по ссылкам, хранящимся в CSV-файле с Google Vision, и записать результаты обратно в новый столбец.
С помощью адаптированного сценария моего друга мне удалось прочитатьCSV, чтобы загрузить изображение, отправить его в Google, чтобы получить результаты - но не записать их обратно в CSV.
У вас есть предложения?
#!/usr/bin/python
import sys
import csv
import os
import urllib
import io
from google.cloud import vision
#from google.cloud.vision import types
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./CREDENTIALS/tourism-219409-61b527f98297.json"
vision_client = vision.ImageAnnotatorClient()
# run with "python start.py <csv-file-path> <csv-header-row-url> <image path>"
# example
# python start.py ./destinations2.csv fotolink ./images
# ------------------
# FUNCTIONS
# ------------------
def printInLine(data):
sys.stdout.write(data)
def getCsvlHeaderIndexOfUrl(csvPath, csvHeaderRowUrl, delimit):
index = None
with open(csvPath, 'r', encoding='UTF8') as csvfile:
reader = csv.reader(csvfile, delimiter=delimit)
interestingrows = [row for idx, row in enumerate(reader) if idx in (0, 0)]
for row in interestingrows:
for i, name in enumerate(row):
if (name == csvHeaderRowUrl):
index = i
print(interestingrows)
print('found: ' + csvHeaderRowUrl + ' @ position: ' + str(index))
return index
def getCsvlValueByHeaderindexAndLinenumber(csvPath, headerUrlIndex, lineNumber, delimit):
print('headerUrlIndex')
print(headerUrlIndex)
print('---')
value = None
with open(csvPath, 'r', encoding='UTF8') as csvfile:
reader = csv.reader(csvfile, delimiter=delimit)
interestingrows = [row for idx, row in enumerate(reader) if idx in (lineNumber, lineNumber)]
print(interestingrows)
value = interestingrows[0][headerUrlIndex]
print('found: ' + value + ' @ lineNumber: ' + str(lineNumber))
return value
def saveLabelsBackToCsvByIndex(csvPath, lineNumber, labelString):
# print('labelString: ' + labelString + str(lineNumber))
# with is like your try .. finally block in this case
with open(csvPath, 'r', encoding='UTF8') as file:
# read a list of lines into data
data = file.readlines()
line = data[lineNumber].strip().replace('\"', '') + labelString + '\n'
# cleanup string
# line = line.replace(';', '')
# print ("Result: " + line)
data[lineNumber] = line
# write everything back
with open(csvPath, 'w', encoding='UTF8') as file:
file.writelines(data)
print(' --> ' + labelString)
def downloadImage(url, imageStorePath, index):
cleanUrl = url.replace('\"', '')
filename, file_extension = os.path.splitext(cleanUrl)
image = imageStorePath + '/' + str(index) + file_extension
print(' --> ' + image)
try:
urllib.request.urlretrieve(cleanUrl, image)
return image
except Exception:
print('ERROR: downloadImage -->', url)
pass
# urllib.request.urlretrieve(cleanUrl, image)
return None
def sendImageToGoogleVision(path):
"""Detects image properties in the file."""
from google.cloud import vision
client = vision.ImageAnnotatorClient()
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.image_properties(image=image)
props = response.image_properties_annotation
print('Properties:')
for color in props.dominant_colors.colors:
print('fraction: {}'.format(color.pixel_fraction))
print('\tr: {}'.format(color.color.red))
print('\tg: {}'.format(color.color.green))
print('\tb: {}'.format(color.color.blue))
print('\ta: {}'.format(color.color.alpha))
# ------------------
# START
# ------------------
# checking parameters
if len(sys.argv) != 4:
printInLine('parameters not correct (should be 5): --> ')
print(len(sys.argv))
printInLine(' ... Quiting app')
sys.exit()
# setting parameters if OK
csvPath = sys.argv[1]
csvHeaderRowUrl = sys.argv[2]
imageStorePath = sys.argv[3]
delimit = ';'
# parameters are OK
if (csvPath and csvHeaderRowUrl and imageStorePath):
print("csvPath: " + csvPath + " csvHeaderRowUrl: " + csvHeaderRowUrl + " imageStorePath: " + imageStorePath)
print('parameters OK ... starting...')
# get index (position) of csv-header url
headerUrlIndex = getCsvlHeaderIndexOfUrl(csvPath, csvHeaderRowUrl, delimit)
# print('headerUrlIndex: ' + str(headerUrlIndex))
# start counter with 1 (after header)
numberOfLines = sum(1 for line in open(csvPath, 'r', encoding='UTF8'))
# print('numberOfLines: ' + str(numberOfLines))
# loop over each line in csv
for i in range(1, numberOfLines):
# row in csv
lineNumber = i
# get url from csv-file
url = getCsvlValueByHeaderindexAndLinenumber(csvPath, headerUrlIndex, lineNumber, delimit)
# download image and get path to stored file (filePath)
filePath = downloadImage(url, imageStorePath, lineNumber)
if filePath:
# send image to vision and get colours
labels = sendImageToGoogleVision(filePath)
# build list of labels
labelString = delimit
# ------ This is where I need your help......
saveLabelsBackToCsvByIndex(csvPath, lineNumber, labelString)
else:
print('parameters missing ... Quiting app')