Python googlevisionapi与语言检测

Python googlevisionapi与语言检测,python,google-vision,Python,Google Vision,我正在尝试用谷歌视觉API支持的一些区域语言对图像执行OCR。但是,我无法指定要从图像中提取的多种语言,如英语、印度语。下面是我的代码: import os import sys import json import time import base64 import urllib import urllib2 def main(): if len(sys.argv) != 2: print 'Usage: python {} [api-key]'.format(sys.argv[0])

我正在尝试用谷歌视觉API支持的一些区域语言对图像执行OCR。但是,我无法指定要从图像中提取的多种语言,如英语、印度语。下面是我的代码:

import os
import sys
import json
import time
import base64
import urllib
import urllib2

def main():
if len(sys.argv) != 2:
   print 'Usage: python {} [api-key]'.format(sys.argv[0])
   return
to_check = ('inputs', 'processed', 'outputs')
if any([not os.path.exists(x) for x in to_check]):
  print 'The script expects the following folders to exist: {}'.format(', '.join(to_check))

answer = None

while answer is None or answer.lower() not in ['y', 'n']:
  answer = raw_input('Do you want the script to create them? [yn] ').lower()

if answer == 'n':
  return
for folder in to_check:
  if not os.path.exists(folder):
    os.makedirs(folder)

print 'Using API key: {}\n'.format(sys.argv[1])

before = []

while True:
  files = os.listdir('inputs')
  added = [f for f in files if f not in before]
  removed = [f for f in before if f not in files]

  print 'Added:', added
  print 'Removed:', removed
  print 'Processing new files..'

  for file in added:
          parts = os.path.splitext(os.path.basename(file))
          text_path = os.path.join('outputs', '.'.join(parts[:-1]) + 
          '.txt')
          processed_path = os.path.join('processed', 
          os.path.basename(file))

if os.path.exists(text_path):
    print('Output file already exists, just moving image.')
    os.rename(os.path.join('inputs', file), processed_path)
  else:
    process(os.path.join('inputs', file), text_path)
    os.rename(os.path.join('inputs', file), processed_path)

print('---')

before = files
time.sleep(5)

def process(fname, output):
  print('Processing {}'.format(fname))

url = 'https://vision.googleapis.com/v1/images:annotate?' + 
urllib.urlencode({'key': sys.argv[1]
})

payload = json.dumps(get_payload([fname], 5)).encode('utf-8')

request = urllib2.Request(url)
request.add_header('Content-Type', 'application/json')
request.add_header('Content-Length', len(payload))

try:
  response = json.loads(urllib2.urlopen(request, payload).read())
except urllib2.HTTPError as e:
  print('Wrong api key. Please check it.')
  print(e.read())
  sys.exit(1)
  return

text_response = response['responses'][0]

text = text_response['fullTextAnnotation']['text']

with open(output, 'wb+') as file:

  file.write(text.encode('utf-8'))

print('Done! Text written to {}'.format(output))

def get_payload(paths, max_results):
   requests = []
   for path in paths:
     with open(path, 'rb') as file:
       content = {'content': 
       base64.b64encode(file.read()).decode('utf-8')}
     requests.append({
       'image': content,
  'features': [{'type': 'TEXT_DETECTION', 'maxResults': max_results}]
})
return {'requests': requests}

if __name__ == '__main__':
  main()

语言提示是GVision请求的“ImageContext”的一部分。谷歌有一个有用的文档和界面,可以在


要获取LocalLanguageCode-

您到底需要什么帮助?请指定。需要知道如何在代码中添加语言组件…谢谢。我已经经历过这一次了。但是,我的问题是如何在代码中指定语言代码。
{
  "requests": [
    {
      "imageContext": {
        "languageHints": [
          MyLocalLanguageCode
        ]
      },
      "features": [
        {
          "type": "TEXT_DETECTION"
        }
      ],
      "image": {
        "content": my_image_content
      }
    }
  ]
}