def fishImage(query, API_KEY, PROJECT_KEY): #QUERY SEARCH gis = GoogleImagesSearch(API_KEY, PROJECT_KEY) query = query + " peixe" _search_params = { 'q': query, 'num': 1, 'searchType': 'image', 'safe': 'off', 'imgType': 'photo', 'fileType': 'jpg', 'orTerms': 'peixe|fish|pesca|pescaria|rio|isca|nadando|nadar|água|mar|anzol' } #limit google API request in 5 i = 0 images = [] gis.search(search_params=_search_params, path_to_dir=path) while gis.results() == [] and i < 5: try: gis.next_page() except: print('page ' + i + ' nothing found') i = i + 1 if os.path.exists(path): filename = os.listdir(path) os.rename(path + filename[0], path + 'post.jpg') if os.path.exists(path + 'post.jpg'): images.append(path + 'post.jpg') return images
# Try to correct spelling mistakes from imperfect text # detection for i, token in enumerate(tokens): tokens[i] = spell.correction(token) # Remove too short sentences if (len(tokens) > 2): sentences.append(tokens) # Append to csv file with open('../../data/sentences.csv', 'a') as csvfile: writer = csv.writer(csvfile, delimiter=',') for sentence in sentences: writer.writerow(sentence) # Ignore errors, go to next image except Exception: pass # At each loop iteration, go to next page of results # Protect against errors as before next_failed = True next_fail_count = 0 while next_failed: try: gis.next_page() next_failed = False except Exception as e: next_failed = True next_fail_count += 1 if next_fail_count > 10: raise RuntimeError(e)