예제 #1
0
def detect_img_labels(img_bin):
    global cpath
    my_retry = retry.Retry(deadline=60)
    creds = Credentials.from_service_account_file(r'gcvServiceKey.json')
    client = ImageAnnotatorClient(credentials=creds)
    image = vision.types.Image(content=img_bin)
    response = client.label_detection(image, my_retry, timeout=10)
    return response.label_annotations
예제 #2
0
 def do_image(self, path):
     
     # Instantiate a speech client
     client = ImageAnnotatorClient()
     resp = None
     
     # Get file by URI, else upload local file
     if path.startswith('http') or path.startswith('gs:'):
         resp = analyze_image(path, client)
     else:
         # TODO: Check if the file exists
         with open(path, 'rb') as fp:
             img = FileStorage(fp)
             url = upload_file(img.read(), img.filename, img.content_type, IMAGE_STORAGE_BUCKET)
             if url is not '':
                 resp = analyze_image(url, client)
             
     # Process and extract as much data as possible. We upload to limit the
     # number of API calls. 
     if resp is not None:
         print_face_details(resp)
         print_logo_details(resp)
         print_text_details(resp)
         print_label_details(resp)
         print_landmark_details(resp)
         print_web_details(resp)
         print_safe_search_details(resp)
     else:
         print('[!] Error processing image...')
예제 #3
0
    def annotator_client(self) -> ImageAnnotatorClient:
        """
        Creates ImageAnnotatorClient.

        :return: Google Image Annotator client object.
        :rtype: google.cloud.vision_v1.ImageAnnotatorClient
        """
        return ImageAnnotatorClient(credentials=self._get_credentials())
예제 #4
0
 def annotator_client(self):
     return ImageAnnotatorClient(credentials=self._get_credentials())
예제 #5
0
 def setUp(self):
     credentials = mock.Mock(spec=Credentials)
     self.client = ImageAnnotatorClient(credentials=credentials)
예제 #6
0
class TestSingleImageHelper(unittest.TestCase):
    def setUp(self):
        credentials = mock.Mock(spec=Credentials)
        self.client = ImageAnnotatorClient(credentials=credentials)

    @mock.patch.object(ImageAnnotatorClient, 'batch_annotate_images')
    def test_all_features_default(self, batch_annotate):
        # Set up an image annotation request with no features.
        image = types.Image(source={
            'image_uri': 'http://foo.com/img.jpg',
        })
        request = types.AnnotateImageRequest(image=image)
        assert not request.features

        # Perform the single image request.
        self.client.annotate_image(request)

        # Evalute the argument sent to batch_annotate_images.
        assert batch_annotate.call_count == 1
        _, args, kwargs = batch_annotate.mock_calls[0]

        # Only a single request object should be sent.
        assert len(args[0]) == 1

        # Evalute the request object to ensure it looks correct.
        request_sent = args[0][0]
        all_features = self.client._get_all_features()
        assert request_sent.image is request.image
        assert len(request_sent.features) == len(all_features)

    @mock.patch.object(ImageAnnotatorClient, 'batch_annotate_images')
    def test_explicit_features(self, batch_annotate):
        # Set up an image annotation request with no features.
        image = types.Image(source={
            'image_uri': 'http://foo.com/img.jpg',
        })
        request = types.AnnotateImageRequest(
            image=image,
            features=[
                types.Feature(type=1),
                types.Feature(type=2),
                types.Feature(type=3),
            ],
        )

        # Perform the single image request.
        self.client.annotate_image(request)

        # Evalute the argument sent to batch_annotate_images.
        assert batch_annotate.call_count == 1
        _, args, kwargs = batch_annotate.mock_calls[0]

        # Only a single request object should be sent.
        assert len(args[0]) == 1

        # Evalute the request object to ensure it looks correct.
        request_sent = args[0][0]
        assert request_sent.image is request.image
        assert len(request_sent.features) == 3
        for feature, i in zip(request_sent.features, range(1, 4)):
            assert feature.type == i
            assert feature.max_results == 0

    @mock.patch.object(ImageAnnotatorClient, 'batch_annotate_images')
    def test_image_file_handler(self, batch_annotate):
        # Set up a file handler.
        file_ = io.BytesIO(b'bogus==')

        # Perform the single image request.
        self.client.annotate_image({'image': file_})

        # Evaluate the argument sent to batch_annotate_images.
        assert batch_annotate.call_count == 1
        _, args, kwargs = batch_annotate.mock_calls[0]

        # Only a single request object should be sent.
        assert len(args[0]) == 1

        # Evalute the request object to ensure it looks correct.
        request_sent = args[0][0]
        assert request_sent['image']['content'] == b'bogus=='

    @mock.patch.object(ImageAnnotatorClient, 'batch_annotate_images')
    @mock.patch.object(io, 'open')
    def test_image_filename(self, io_open, batch_annotate):
        # Make io.open send back a mock with a read method.
        file_ = mock.MagicMock(spec=io.BytesIO)
        io_open.return_value = file_
        file_.__enter__.return_value = file_
        file_.read.return_value = b'imagefile=='

        # Perform the single image request using a filename.
        self.client.annotate_image(
            {'image': {
                'source': {
                    'filename': 'image.jpeg'
                }
            }}, )

        # Establish that my file was opened.
        io_open.assert_called_once_with('image.jpeg', 'rb')

        # Evalute the argument sent to batch_annotate_images.
        assert batch_annotate.call_count == 1
        _, args, kwargs = batch_annotate.mock_calls[0]

        # Only a single request object should be sent.
        assert len(args[0]) == 1

        # Evalute the request object to ensure it looks correct.
        request_sent = args[0][0]
        assert request_sent['image']['content'] == b'imagefile=='
예제 #7
0
__email__ = "*****@*****.**"
__status__ = "Development"
__url__ = "https://github.com/urbanriskmap/timeseries-analysis"

import pickle
import logging
from abstract_labeler import AbstractLabeler

from google.api_core.exceptions import GoogleAPICallError
from google.cloud.vision_v1 import ImageAnnotatorClient

from cognicity_image_loader import CognicityImageLoader

from sqlalchemy import create_engine

client = ImageAnnotatorClient()


class GoogleLabeler(AbstractLabeler):
    def __init__(self, config):
        self.config = config

    def load_labels_from_disk(self, filename='./goog_labels_chennai.p'):
        return pickle.load(open(filename, 'rb'))

    def dump_labels_to_disk(self, labels, filename='./goog_labels_chennai.p'):
        pickle.dump(labels, open(filename, 'wb'))
        return

    def make_feature_vectors(self, inp, allowed):
        '''
예제 #8
0
class TestSingleImageHelper(unittest.TestCase):
    def setUp(self):
        credentials = mock.Mock(spec=Credentials)
        self.client = ImageAnnotatorClient(credentials=credentials)

    @mock.patch.object(ImageAnnotatorClient, "batch_annotate_images")
    def test_all_features_default(self, batch_annotate):
        # Set up an image annotation request with no features.
        image = vision_v1.Image(source={"image_uri": "http://foo.com/img.jpg"})
        request = vision_v1.AnnotateImageRequest(image=image)
        assert not request.features

        # Perform the single image request.
        self.client.annotate_image(request)

        # Evalute the argument sent to batch_annotate_images.
        assert batch_annotate.call_count == 1
        _, args, kwargs = batch_annotate.mock_calls[0]

        # Only a single request object should be sent.
        assert len(kwargs["requests"]) == 1

        # Evalute the request object to ensure it looks correct.
        request_sent = kwargs["requests"][0]
        all_features = self.client._get_all_features()
        assert request_sent.image == request.image
        assert len(request_sent.features) == len(all_features)

    @mock.patch.object(ImageAnnotatorClient, "batch_annotate_images")
    def test_explicit_features(self, batch_annotate):
        # Set up an image annotation request with no features.
        image = vision_v1.Image(source={"image_uri": "http://foo.com/img.jpg"})
        request = vision_v1.AnnotateImageRequest(
            image=image,
            features=[
                vision_v1.Feature(type_=1),
                vision_v1.Feature(type_=2),
                vision_v1.Feature(type_=3),
            ],
        )

        # Perform the single image request.
        self.client.annotate_image(request)

        # Evalute the argument sent to batch_annotate_images.
        assert batch_annotate.call_count == 1
        _, args, kwargs = batch_annotate.mock_calls[0]

        # Only a single request object should be sent.
        assert len(kwargs["requests"]) == 1

        # Evalute the request object to ensure it looks correct.
        request_sent = kwargs["requests"][0]
        assert request_sent.image == request.image
        assert len(request_sent.features) == 3
        for feature, i in zip(request_sent.features, range(1, 4)):
            assert feature.type_ == i
            assert feature.max_results == 0

    @mock.patch.object(ImageAnnotatorClient, "batch_annotate_images")
    def test_image_file_handler(self, batch_annotate):
        # Set up a file handler.
        file_ = io.BytesIO(b"bogus==")

        # Perform the single image request.
        self.client.annotate_image({"image": file_})

        # Evaluate the argument sent to batch_annotate_images.
        assert batch_annotate.call_count == 1
        _, args, kwargs = batch_annotate.mock_calls[0]

        # Only a single request object should be sent.
        assert len(kwargs["requests"]) == 1

        # Evalute the request object to ensure it looks correct.
        request_sent = kwargs["requests"][0]
        assert request_sent["image"]["content"] == b"bogus=="

    @mock.patch.object(ImageAnnotatorClient, "batch_annotate_images")
    @mock.patch.object(builtins, "open")
    def test_image_filename(self, io_open, batch_annotate):
        # Make io.open send back a mock with a read method.
        file_ = mock.MagicMock(spec=io.BytesIO)
        io_open.return_value = file_
        file_.__enter__.return_value = file_
        file_.read.return_value = b"imagefile=="

        # Perform the single image request using a filename.
        self.client.annotate_image(
            {"image": {
                "source": {
                    "filename": "image.jpeg"
                }
            }})

        # Establish that my file was opened.
        io_open.assert_called_once_with("image.jpeg", "rb")

        # Evalute the argument sent to batch_annotate_images.
        assert batch_annotate.call_count == 1
        _, args, kwargs = batch_annotate.mock_calls[0]

        # Only a single request object should be sent.
        assert len(kwargs["requests"]) == 1

        # Evalute the request object to ensure it looks correct.
        request_sent = kwargs["requests"][0]
        assert request_sent["image"]["content"] == b"imagefile=="
예제 #9
0
def detect_faces(face_content: bytes):
    client = ImageAnnotatorClient()

    image = types.Image(content=face_content)
    results = client.face_detection(image=image)
    return results.face_annotations
예제 #10
0
def imageSearchHome(request):
	global objects
	if request.method == 'GET':
		try:
			loggedInUser = request.session['uEmail']
		except:
			return redirect('/welcome')
		response = storage.list_buckets(settings.CLOUD_PROJECT_ID)
		#This statement gets only the bucket whose name starts with datacore
		for lstbct in response['items']:
			if lstbct['name'].startswith('datacore'):
				#blobs contains the list of cropped and the images in the root folder of each bucket
				blobs = []
				#respobject contains the list of all the images in the bucket
				respobject = storage.list_objects(lstbct['name'])
				#This statement gets only the cropped images and the images in the root folder of each bucket
				for index, bct in enumerate(respobject):	
					#This statement gets the images in the root folder of the bucket
					if 'segment/' in str(bct['name']):
						pass
					else:
						blobs.append({'index': index, 'name': str(bct['name']),
									  'public_url': 'https://storage.googleapis.com/' + lstbct['name'] + '/' + str(
									   bct['name']),
									   'timecreated': datetime.strptime(str(bct['timeCreated']),
																	   '%Y-%m-%dT%H:%M:%S.%fZ'),
									   'type': str(bct['contentType'])})
				objects.update({lstbct['name']: blobs})
		#context contains the data to be passed to the HTML page for the post request
		context = {
			'loggedIn' : True,
			'operation' : 'get',
			'objects': objects,
		}
		#This statement returns the response as a HTML page along with the data to be displayed in it
		return render(request, 'imageSearch.html', context)
	else:
		if request.POST['submit'] == 'Image Search':
			#totalSelectedImages contains the list of images selected by the user
			totalSelectedImages = request.POST.getlist('miasImages')
			# client does the required operation on the selected image for annotations
			client = ImageAnnotatorClient()
			image_details = []
			# image_details contains the list of image information such as image url and features that
			# need to be done on the images for annotations
			image_requests = []
			# image_features contains the list of operations that need to be performed on the image
			image_features = [{'type': DETECTION_TYPES[3]}, 
							  {'type': DETECTION_TYPES[4]}, 
			                  {'type': DETECTION_TYPES[5]}]
			# total_images is a counter for counting the total number of images selected by the user
			total_images = 0
			# image_histograms contains the list of histogram information for the selected images
			image_histograms = []
			# This block gets the histogram information as well as the image information
			# for the selected images 
			for eachImage in totalSelectedImages:
				image_requests.append({'image': {'source': {'image_uri': eachImage.split('+')[1]}},
									   'features': image_features,})
				# image_content contains the image data from the url
				image_content = requests.get(eachImage.split('+')[1])
				# print('image_content: ')
				# print(type(image_content.content))
				image_array = np.asarray(bytearray(BytesIO(image_content.content).getvalue()), dtype='uint8')
				# print('image_array: ')
				# print(type(image_array))
				converted_image_array = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
				# print(type('converted_image_array: '))
				# print(type(converted_image_array))
				individual_image_histogram = []
				if converted_image_array.shape[2] != 3:					
					for each_channel in range(0, 3):
						if each_channel == 0:
							individual_image_histogram.append({'redChannel': \
															     cv2.calcHist([converted_image_array], \
																              [0], None, [256], [0, 256]).tolist()
															  })
						elif each_channel == 1:
							individual_image_histogram.append({'greenChannel': \
															     cv2.calcHist([converted_image_array], \
																              [1], None, [256], [0, 256]).tolist()
															  })
						else:
							individual_image_histogram.append({'blueChannel': \
															     cv2.calcHist([converted_image_array], \
																              [2], None, [256], [0, 256]).tolist()
															  })
				else:
					individual_image_histogram.append({'singleChannel': \
					                                    cv2.calcHist([converted_image_array],[0], None, \
														[256], [0, 256]).tolist()
													  })
				
				image_histograms.append({'index': total_images, 'histogram': individual_image_histogram })
				total_images = total_images + 1
			image_response = client.batch_annotate_images(image_requests)
			image_labels = []
			image_logos = []
			for index, eachResponse in enumerate(image_response.responses):
				if len(image_response.responses[index].label_annotations) != 0:
					labels = []
					for eachLabel in range(len(image_response.responses[index].label_annotations)):
						labels.append({'labelDescription': \
						                image_response.responses[index].label_annotations[eachLabel].description, \
									   'labelPercent': \
										str(round(image_response.responses[index].label_annotations[eachLabel].score * 100)),
						              })
					image_labels.append({'index': index, 'labels': labels})
				else:
					image_labels.append({'index': index, 'labels': []})
			for index, eachResponse in enumerate(image_response.responses):
				if len(image_response.responses[index].logo_annotations) != 0:
					logos = []
					for eachLogo in range(len(image_response.responses[index].logo_annotations)):
						logos.append({'logoDescription': \
						               image_response.responses[index].logo_annotations[eachLogo].description, \
									  'logoPercent': \
									   str(round(image_response.responses[index].logo_annotations[eachLogo].score * 100)),
						             })
					image_logos.append({'index': index, 'logos': logos})
				else:
					image_logos.append({'index': index, 'logos': []})
			# datastore_client contains methods for accessing GCP's datastore
			datastore_client = datastore.Client()
			# This block creates entity in the datastore kind MIASJSON for storing image information as a
			# JSON format
			for index, eachImage in enumerate(totalSelectedImages, 0):
				# key creates a key for the kind MIASJSON in the datastore
				key = datastore_client.key('MIASJSON', eachImage.split('+')[0])
				# entity creates an entity in the MIASJSON kind in the datastore
				entity = datastore.entity.Entity(key, ('image_info', 'text_info', \
				                                       'label_info', 'logo_info', \
													   'histogram_info'))
				# This statement creates a property named image_info which stores image data 
				# such as image name and its uri
				entity['image_info'] = json.dumps({'image_name': eachImage.split('+')[0], 
				                                   'image_url': eachImage.split('+')[1],
												  })
				# This statement creates a property named text_info which stores text data 
				# if available in the image
				entity['text_info'] = json.dumps({'text_data': image_response.responses[index].text_annotations[0].description \
 								                  if (len(image_response.responses[index].text_annotations) != 0) \
								                  else 'No text in the image',
											     })
				# This statement creates a property named label_info which stores label data 
				# if available in the image
				entity['label_info'] = json.dumps({'label_data': 'No label in the image' \
										                         if (len(image_labels[index]['labels']) == 0) \
													             else image_labels[index]['labels'],
												  })
				# This statement creates a property named logo_info which stores logo data 
				# if available in the image
				entity['logo_info'] = json.dumps({'logo_data': 'No logo in the image' \
										                       if (len(image_logos[index]['logos']) == 0) \
													           else image_logos[index]['logos'],
												 })
				# This statement creates a property named histogram_info which stores histogram data 
				# if available in the image
				entity['histogram_info'] = json.dumps({'histogram_data': image_histograms[index]['histogram']})
				datastore_client.put(entity)
			# context contains the data to be passed to the HTML page for the post request
			context = {
				'loggedIn': True,
				'operation': 'ImageSearch',
				'objects': objects,
				'result': True,
			}
			#This statement returns the response as a HTML page along with the data to be displayed in it
			return render(request, 'imageSearch.html', context)
		elif request.POST['submit'] == 'Similarity':
			#totalSelectedImages contains the list of images selected by the user
			totalSelectedImages = request.POST.getlist('miasImages')
			ed_result = []
			for eachImage in totalSelectedImages:
				image_content = requests.get(eachImage.split('+')[1])
				image_array = np.asarray(bytearray(BytesIO(image_content.content).getvalue()), dtype='uint8')
				converted_image_array = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
				selected_image_histogram = cv2.calcHist([converted_image_array],[0], None, \
														[256], [0, 256])										
				# datastore_client contains methods for accessing GCP's datastore
				datastore_client = datastore.Client()
				datastore_query = datastore_client.query(kind = 'MIASJSON')
				result = list(datastore_query.fetch())
				similarity_result = []
				for eachResult in result:
					individual_result = json.loads(dict(eachResult)['histogram_info'])
					histogram_data = np.array(individual_result['histogram_data'][0]['singleChannel'])
					euclidean_distance = np.sqrt(np.sum((selected_image_histogram - histogram_data) ** 2))
					similarity_result.append({'image_name': eachResult.key.name, 'distance': euclidean_distance})
					# print(euclidean_distance)
				ed_result.append({'image_name': eachImage.split('+')[0], 'similarity_result': similarity_result})
			# context contains the data to be passed to the HTML page for the post request
			context = {
				'loggedIn': True,
				'operation': 'Similarity',
				'objects': objects,
				'ed_result': ed_result,
				'result': True,
			}
			#This statement returns the response as a HTML page along with the data to be displayed in it
			return render(request, 'imageSearch.html', context)
예제 #11
0
print('\n'.join([d.description for d in resp.text_annotations]))

########################
import io
from google.cloud import vision
client = vision.ImageAnnotatorClient()
imageData = 'picture.png'
with io.open(imageData, 'rb') as image_file:
    content = image_file.read()
    texts = client.document_text_detection(imageData, content=content)
    print(texts[0].description)

#############################

from google.cloud.vision_v1 import ImageAnnotatorClient
client = ImageAnnotatorClient()
request = {
    'image': {
        'source': {
            'image_uri': 'https://foo.com/image.jpg'
        },
    },
}
response = client.annotate_image(request)


###############
#import requests
#import base64
#
#file_name = '/sss.jpg'