def detect_document(path): """Detects document features in an image.""" client = vision.ImageAnnotatorClient() with io.open(path, 'rb') as image_file: content = image_file.read() image = vision.types.Image(content=content) response = client.document_text_detection(image=image) for page in response.full_text_annotation.pages: for block in page.blocks: block_words = [] for paragraph in block.paragraphs: block_words.extend(paragraph.words) print(u'Paragraph Confidence: {}\n'.format( paragraph.confidence)) block_text = '' block_symbols = [] for word in block_words: block_symbols.extend(word.symbols) word_text = '' for symbol in word.symbols: word_text = word_text + symbol.text print(u'\tSymbol text: {} (confidence: {})'.format( symbol.text, symbol.confidence)) print(u'Word text: {} (confidence: {})\n'.format( word_text, word.confidence)) block_text += ' ' + word_text print(u'Block Content: {}\n'.format(block_text)) print(u'Block Confidence:\n {}\n'.format(block.confidence))
def test_batch_annotate_images(self): # Setup Expected Response expected_response = {} expected_response = image_annotator_pb2.BatchAnnotateImagesResponse( **expected_response ) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = vision_v1p1beta1.ImageAnnotatorClient() # Setup Request requests = [] response = client.batch_annotate_images(requests) assert expected_response == response assert len(channel.requests) == 1 expected_request = image_annotator_pb2.BatchAnnotateImagesRequest( requests=requests ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def __init__(self, result): self.client = vision.ImageAnnotatorClient() self.result = {} self.address_val = {} self.licence_id = {} self.result = result self.ssn = {} self.name = {} self.date = {} self.pay_Val = Queue() self.keys = [] self.values = [] self.dict = {} self.date = {} self.conf_keys, self.conf_values = [], [] self.conf_result = {} self.emp_name, self.employee_name = {}, {} self.emp_address, self.employee_address = {}, {} self.description = [] self.text_val = [] self.location_json = '' self.regular1, self.regular2, self.regular3, self.regular4, self.regular5, self.regular6, self.regular7, self.regular8, self.regular9, self.regular10 = {}, {}, {}, {}, {}, {}, {}, {}, {}, {} self.tax1, self.tax2, self.tax3, self.tax4, self.tax5, self.tax6, self.tax7, self.tax8, self.tax9, self.tax10 = {}, {}, {}, {}, {}, {}, {}, {}, {}, {} self.deduction1, self.deduction2, self.deduction3, self.deduction4, self.deduction5, self.deduction6, self.deduction7, self.deduction8, self.deduction9, self.deduction10, self.deduction11, self.deduction12, self.deduction13, self.deduction14, self.deduction15 = {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} self.pay_start_date, self.pay_end_date, self.pay_date = {}, {}, {}
def annotate_image(imagePath): apiClient = auth.get_Api_client() # Instantiates a client client = vision.ImageAnnotatorClient(credentials=apiClient._credentials) # The name of the image file to annotate # file_name = os.path.join(os.path.dirname(__file__), imagePath) file_name = imagePath # Loads the image into memory with io.open(file_name, 'rb') as image_file: content = image_file.read() image = types.Image(content=content) features = [ types.Feature(type=types.Feature.LABEL_DETECTION, max_results=8), types.Feature(type=types.Feature.LOGO_DETECTION, max_results=8), types.Feature(type=types.Feature.TEXT_DETECTION, max_results=8), types.Feature(type=types.Feature.WEB_DETECTION, max_results=15), # types.Feature(type=types.Feature., max_results=8), ] request = { 'image': image, 'features': features, } response = client.annotate_image(request) respStr = MessageToJson(response) responseObj = json.loads(respStr) return responseObj
def detect_document_uri(uri): """Detects document features in the file located in Google Cloud Storage.""" client = vision.ImageAnnotatorClient() image = vision.types.Image() image.source.image_uri = uri response = client.document_text_detection(image=image) for page in response.full_text_annotation.pages: for block in page.blocks: block_words = [] for paragraph in block.paragraphs: block_words.extend(paragraph.words) print(u'Paragraph Confidence: {}\n'.format( paragraph.confidence)) block_text = '' block_symbols = [] for word in block_words: block_symbols.extend(word.symbols) word_text = '' for symbol in word.symbols: word_text = word_text + symbol.text print(u'\tSymbol text: {} (confidence: {})'.format( symbol.text, symbol.confidence)) print(u'Word text: {} (confidence: {})\n'.format( word_text, word.confidence)) block_text += ' ' + word_text print(u'Block Content: {}\n'.format(block_text)) print(u'Block Confidence:\n {}\n'.format(block.confidence))
def test_batch_annotate_images_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = vision_v1p1beta1.ImageAnnotatorClient(channel=channel) # Setup request requests = [] with pytest.raises(CustomException): client.batch_annotate_images(requests)
def detect_web(path): """Detects web annotations given an image.""" client = vision_v1p1beta1.ImageAnnotatorClient() with io.open(path, 'rb') as image_file: content = image_file.read() image = vision_v1p1beta1.types.Image(content=content) # how doe we get this response = client.web_detection(image=image) annotations = response.web_detection for label in annotations.best_guess_labels: print('\nBest guess label: {}'.format(label.label)) best_g.append(format(label.label)) # def report(annotations): """Prints detected features in the provided web annotations.""" if annotations.pages_with_matching_images: print('\n{} Pages with matching images retrieved'.format( len(annotations.pages_with_matching_images))) for page in annotations.pages_with_matching_images: print('Url : {}'.format(page.url)) # if annotations.best_guess_labels: # for label in annotations.best_guess_labels: # print('\nBest guess label: {}'.format(label.label)) if annotations.full_matching_images: print ('\n{} Full Matches found: '.format( len(annotations.full_matching_images))) for image in annotations.full_matching_images: print('Url : {}'.format(image.url)) if annotations.partial_matching_images: print ('\n{} Partial Matches found: '.format( len(annotations.partial_matching_images))) for image in annotations.partial_matching_images: print('Url : {}'.format(image.url)) if annotations.web_entities: print ('\n{} Web entities found: '.format( len(annotations.web_entities))) for entity in annotations.web_entities: print('Score : {}'.format(entity.score)) print('Description: {}'.format(entity.description)) if (entity.score>0.90): web_scores.append(entity.description)
def web_entities(path): client = vision.ImageAnnotatorClient() with io.open(path, 'rb') as image_file: content = image_file.read() image = vision.types.Image(content=content) response = client.web_detection(image=image) for entity in response.web_detection.web_entities: print('\n\tScore : {}'.format(entity.score)) print(u'\tDescription: {}'.format(entity.description))
def test_batch_annotate_images_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = vision_v1p1beta1.ImageAnnotatorClient() # Setup request requests = [] with pytest.raises(CustomException): client.batch_annotate_images(requests)
def sample_batch_annotate_images(): # Create a client client = vision_v1p1beta1.ImageAnnotatorClient() # Initialize request argument(s) request = vision_v1p1beta1.BatchAnnotateImagesRequest( ) # Make the request response = client.batch_annotate_images(request=request) # Handle the response print(response)
def detect_web(path): """Detects web annotations given an image.""" client = vision.ImageAnnotatorClient() with io.open(path, 'rb') as image_file: content = image_file.read() image = vision.types.Image(content=content) response = client.web_detection(image=image) annotations = response.web_detection if annotations.best_guess_labels: for label in annotations.best_guess_labels: print('\nBest guess label: {}'.format(label.label)) if annotations.pages_with_matching_images: print('\n{} Pages with matching images found:'.format( len(annotations.pages_with_matching_images))) for page in annotations.pages_with_matching_images: print('\n\tPage url : {}'.format(page.url)) if page.full_matching_images: print('\t{} Full Matches found: '.format( len(page.full_matching_images))) for image in page.full_matching_images: print('\t\tImage url : {}'.format(image.url)) if page.partial_matching_images: print('\t{} Partial Matches found: '.format( len(page.partial_matching_images))) for image in page.partial_matching_images: print('\t\tImage url : {}'.format(image.url)) if annotations.web_entities: print('\n{} Web entities found: '.format(len( annotations.web_entities))) for entity in annotations.web_entities: print('\n\tScore : {}'.format(entity.score)) print(u'\tDescription: {}'.format(entity.description)) if annotations.visually_similar_images: print('\n{} visually similar images found:\n'.format( len(annotations.visually_similar_images))) for image in annotations.visually_similar_images: print('\tImage url : {}'.format(image.url))
def detect_web_uri(uri): """Detects web annotations in the file located in Google Cloud Storage.""" client = vision.ImageAnnotatorClient() image = vision.types.Image() image.source.image_uri = uri response = client.web_detection(image=image) annotations = response.web_detection if annotations.best_guess_labels: for label in annotations.best_guess_labels: print('\nBest guess label: {}'.format(label.label)) if annotations.pages_with_matching_images: print('\n{} Pages with matching images found:'.format( len(annotations.pages_with_matching_images))) for page in annotations.pages_with_matching_images: print('\n\tPage url : {}'.format(page.url)) if page.full_matching_images: print('\t{} Full Matches found: '.format( len(page.full_matching_images))) for image in page.full_matching_images: print('\t\tImage url : {}'.format(image.url)) if page.partial_matching_images: print('\t{} Partial Matches found: '.format( len(page.partial_matching_images))) for image in page.partial_matching_images: print('\t\tImage url : {}'.format(image.url)) if annotations.web_entities: print('\n{} Web entities found: '.format(len( annotations.web_entities))) for entity in annotations.web_entities: print('\n\tScore : {}'.format(entity.score)) print(u'\tDescription: {}'.format(entity.description)) if annotations.visually_similar_images: print('\n{} visually similar images found:\n'.format( len(annotations.visually_similar_images))) for image in annotations.visually_similar_images: print('\tImage url : {}'.format(image.url))
def web_entities_include_geo_results_uri(uri): client = vision.ImageAnnotatorClient() image = vision.types.Image() image.source.image_uri = uri web_detection_params = vision.types.WebDetectionParams( include_geo_results=True) image_context = vision.types.ImageContext( web_detection_params=web_detection_params) response = client.web_detection(image=image, image_context=image_context) for entity in response.web_detection.web_entities: print('\n\tScore : {}'.format(entity.score)) print(u'\tDescription: {}'.format(entity.description))
def web_entities_include_geo_results(path): client = vision.ImageAnnotatorClient() with io.open(path, 'rb') as image_file: content = image_file.read() image = vision.types.Image(content=content) web_detection_params = vision.types.WebDetectionParams( include_geo_results=True) image_context = vision.types.ImageContext( web_detection_params=web_detection_params) response = client.web_detection(image=image, image_context=image_context) for entity in response.web_detection.web_entities: print('\n\tScore : {}'.format(entity.score)) print(u'\tDescription: {}'.format(entity.description))
def annotate_image_batch(imagePathes): apiClient = auth.get_Api_client() # Instantiates a client client = vision.ImageAnnotatorClient(credentials=apiClient._credentials) features = [ types.Feature(type=types.Feature.LABEL_DETECTION), types.Feature(type=types.Feature.LOGO_DETECTION), types.Feature(type=types.Feature.TEXT_DETECTION), types.Feature(type=types.Feature.WEB_DETECTION), # types.Feature(type=types.Feature., max_results=8), ] requests = [] for imagePath in imagePathes: pass # The name of the image file to annotate file_name = imagePath # Loads the image into memory with io.open(file_name, 'rb') as image_file: content = image_file.read() image = types.Image(content=content) request = { 'image': image, 'features': features, } requests.append(request) responses = [] i = 0 batch = 10 while i < len(requests): to = min(i + batch, len(requests)) resp = client.batch_annotate_images(requests[i:to]) i = i + batch for response in resp.responses: respStr = MessageToJson(response) tt = json.loads(respStr) responses.append(tt) return responses
def detect_safe_search_uri(uri): """Detects unsafe features in the file located in Google Cloud Storage or on the Web.""" client = vision.ImageAnnotatorClient() image = vision.types.Image() image.source.image_uri = uri response = client.safe_search_detection(image=image) safe = response.safe_search_annotation # Names of likelihood from google.cloud.vision.enums likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE', 'LIKELY', 'VERY_LIKELY') print('Safe search:') print('adult: {}'.format(likelihood_name[safe.adult])) print('medical: {}'.format(likelihood_name[safe.medical])) print('spoofed: {}'.format(likelihood_name[safe.spoof])) print('violence: {}'.format(likelihood_name[safe.violence])) print('racy: {}'.format(likelihood_name[safe.racy]))
def test_batch_annotate_images(self): # Setup Expected Response expected_response = {} expected_response = image_annotator_pb2.BatchAnnotateImagesResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = vision_v1p1beta1.ImageAnnotatorClient(channel=channel) # Setup Request requests = [] response = client.batch_annotate_images(requests) assert expected_response == response assert len(channel.requests) == 1 expected_request = image_annotator_pb2.BatchAnnotateImagesRequest( requests=requests) actual_request = channel.requests[0][1] assert expected_request == actual_request
def detect_safe_search(path): """Detects unsafe features in the file.""" client = vision.ImageAnnotatorClient() with io.open(path, 'rb') as image_file: content = image_file.read() image = vision.types.Image(content=content) response = client.safe_search_detection(image=image) safe = response.safe_search_annotation # Names of likelihood from google.cloud.vision.enums likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE', 'LIKELY', 'VERY_LIKELY') print('Safe search:') print('adult: {}'.format(likelihood_name[safe.adult])) print('medical: {}'.format(likelihood_name[safe.medical])) print('spoofed: {}'.format(likelihood_name[safe.spoof])) print('violence: {}'.format(likelihood_name[safe.violence])) print('racy: {}'.format(likelihood_name[safe.racy]))
def classify_image(image_name): os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/Users/chappers/Documents/DataScience/0_Method_DS/MethodDS-KaggleKat-8b57553f8dbf.json" # ### Start of Code to classify the image utilising the Google Vision API # Instantiates a client client = vision_v1p1beta1.ImageAnnotatorClient() # The name of the image file to annotate file_name = os.path.join( os.path.dirname('__file__'), image_name) # Loads the image into memory with io.open(file_name, 'rb') as image_file: content = image_file.read() image = types.Image(content=content) # Performs label detection on the image file response = client.label_detection(image=image) labels = response.label_annotations # create str_search to be used in the EBAY API call for KEYWORDS str_search = str(labels[0].description) # ### Code to utilise the Best Guess Label within Google Vision API web_scores=[] best_g=[] def detect_web(path): """Detects web annotations given an image.""" client = vision_v1p1beta1.ImageAnnotatorClient() with io.open(path, 'rb') as image_file: content = image_file.read() image = vision_v1p1beta1.types.Image(content=content) # how doe we get this response = client.web_detection(image=image) annotations = response.web_detection for label in annotations.best_guess_labels: print('\nBest guess label: {}'.format(label.label)) best_g.append(format(label.label)) # def report(annotations): """Prints detected features in the provided web annotations.""" if annotations.pages_with_matching_images: print('\n{} Pages with matching images retrieved'.format( len(annotations.pages_with_matching_images))) for page in annotations.pages_with_matching_images: print('Url : {}'.format(page.url)) # if annotations.best_guess_labels: # for label in annotations.best_guess_labels: # print('\nBest guess label: {}'.format(label.label)) if annotations.full_matching_images: print ('\n{} Full Matches found: '.format( len(annotations.full_matching_images))) for image in annotations.full_matching_images: print('Url : {}'.format(image.url)) if annotations.partial_matching_images: print ('\n{} Partial Matches found: '.format( len(annotations.partial_matching_images))) for image in annotations.partial_matching_images: print('Url : {}'.format(image.url)) if annotations.web_entities: print ('\n{} Web entities found: '.format( len(annotations.web_entities))) for entity in annotations.web_entities: print('Score : {}'.format(entity.score)) print('Description: {}'.format(entity.description)) if (entity.score>0.90): web_scores.append(entity.description) # report(annotate('IMG_2934.jpg')) detect_web('IMG_2934.jpg') for rows in range(0,len(web_scores)): str_search=(str_search+" , "+ web_scores[rows]) # ### Creating keyword string to pass to EBAY API # Create the Keyword String to then feed into the Ebay API, note the use of brackets and comma's, according to API # documentation, this then results in items being returned with any of words seperated by comma's, ie. OR logic keyword_string = best_g[0] # kstring = keyword_string.split(" ") keyword_string = str("(" + keyword_string + ")") keyword_string = keyword_string.replace(" ",",") return keyword_string
def get_texts_from_bytes(self, content): client = vision.ImageAnnotatorClient() image = vision.types.Image(content=content) response = client.document_text_detection(image=image) texts = response.text_annotations return texts, response