From 2ee1cdd103c4ab9139c1dce140e35a5332c5da62 Mon Sep 17 00:00:00 2001 From: Jay Turner Date: Sun, 28 Jun 2020 14:18:17 +0100 Subject: [PATCH] Fix retry bug Has been reported at googleapis/python-vision#31 --- PogoOCR/cloudvision.py | 16 ++++++++++++++-- setup.py | 12 ++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/PogoOCR/cloudvision.py b/PogoOCR/cloudvision.py index aed03be..76910d4 100644 --- a/PogoOCR/cloudvision.py +++ b/PogoOCR/cloudvision.py @@ -18,5 +18,17 @@ def __init__(self, service_file, image_content=None, image_uri=None): def get_text(self): print("Requesting TEXT_DETECTION from Google Cloud API. This will cost us 0.0015 USD.") - response = self.google.text_detection(image=self.image, retry=5) - self.text_found = response.text_annotations + attempts = 0 + while attempts < 5: + attempts += 1 + response = self.google.text_detection(image=self.image) + try: + response.text_annotations[0] + except IndexError: + pass + else: + self.text_found = response.text_annotations + break + + if attempts == 5: + raise Exception(response.text_annotations) diff --git a/setup.py b/setup.py index 263098f..4e6c7ec 100644 --- a/setup.py +++ b/setup.py @@ -1,16 +1,16 @@ +import setuptools import re -from setuptools import setup -with open('README.md') as f: +with open('README.md', 'r') as f: readme = f.read() -with open('PogoOCR/__init__.py') as f: +with open('PogoOCR/__init__.py', 'r') as f: version = re.search(r'__version__\s*=\s*[\'"]([^\'"]*)[\'"]', f.read()).group(1) -with open('requirements.txt') as f: - requirements = f.splitlines() +with open('requirements.txt', 'r') as f: + requirements = f.read().splitlines() -setup( +setuptools.setup( name="PogoOCR", version=version, author="Jay Turner",