Python google.cloud.vision.Client() Examples

The following are code examples for showing how to use google.cloud.vision.Client(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: inyourface   Author: yacomink   File: Animator.py    MIT License 6 votes vote down vote up
def __init__(self, url, destdir, cache_dir):
        self.vision_client = vision.Client()
        self.url = url[-1]
        if len(url) > 1:
            self.secondary_urls = list(url[0:-1])
        else:
            self.secondary_urls = []
        self.destdir = destdir
        self.cache_dir = cache_dir
        self.cache_provider = False
        self.raw_frames = []
        self.total_frames = len(self.__class__.frames)

        hasher = hashlib.sha1()
        hasher.update(self.__class__.name)
        hasher.update(','.join(url))

        if (self.cache_dir):
            self.cache_provider = inyourface.DefaultCacheProvider.CacheProvider(self.cache_dir)

        self.hash = hasher.hexdigest() 
Example 2
Project: search_within_videos   Author: iRapha   File: vision.py    MIT License 6 votes vote down vote up
def get_labels(frames, progress_cb, so_far, task_weight):
    client = vision.Client('treehacks-159123')
    new_frames = {}
    i = 0
    for timestamp, curr in frames.items():
        curr_img, img_key = curr
        img_bytes = BytesIO()
        curr_img.save(img_bytes, format='png')

        img = client.image(content=img_bytes.getvalue())
        labels = img.detect_labels()
        time.sleep(0.05) # don't set off the firewallll
        new_frames[timestamp] = (img_bytes.getvalue(), [l.description for l in labels], img_key)
        print ('{}/{}'.format(str(i), len(frames)), end='\r')
        i += 1
        progress_cb(so_far + ((i / len(frames.items())) * task_weight), 100)
    return new_frames 
Example 3
Project: eclipse2017   Author: google   File: uploader.py    Apache License 2.0 6 votes vote down vote up
def _get_client(client_type='storage'):
    """
    Returns gcloud client, (either for storage if `client_type` is 'storage',
    or for datastore if `client_type` is 'datastore'). Defaults to storage
    client, including when an invalid `client_type` is given.
    Raises `CouldNotObtainCredentialsError` if there is an error obtaining
    credentials.
    """
    # Raises CouldNotObtainCredentialsError
    credentials = sa.get_credentials()

    if client_type == 'datastore':
        client_class = datastore.Client
    else:
        client_class = storage.client.Client

    return client_class(project=config.PROJECT_ID, credentials=credentials) 
Example 4
Project: eclipse2017   Author: google   File: uploader.py    Apache License 2.0 6 votes vote down vote up
def _check_adult_content(img):
    """
    Checks if img contains adult content.
    Returns True if img contains adult content.
    """
    first, second = getRescaledDimensions(img.width, img.height, 640, 480)
    try:
        resize = img.resize((first, second), Image.ANTIALIAS)
    except IOError:
        logging.error("Invalid image cannot be resized.")
        # Have to assume image is adult content
        return True
    out = io.BytesIO()
    resize.convert('RGB').save(out, format='JPEG')
    vision_client = vision.Client()
    vc_img = vision_client.image(content=out.getvalue())
    safe = vc_img.detect_safe_search()
    if safe.adult == vision.likelihood.Likelihood.LIKELY or safe.adult == vision.likelihood.Likelihood.POSSIBLE:
        logging.error("Detected likely adult content upload.")
        return True
    return False 
Example 5
Project: ohgee   Author: raygeeknyc   File: visionanalyzer.py    GNU General Public License v3.0 5 votes vote down vote up
def analyzeVision(self):
        self._vision_client = vision.Client()
        skipped_images = 0
        frame = None
        while not self._stop_analyzing:
            try:
                frame = self._frames.get(block=False)
                skipped_images += 1
            except Queue.Empty:
                if not frame:
                    logging.debug("Empty image queue, waiting")
                    skipped_images = 0
                    time.sleep(POLL_SECS)
                else:
                    skipped_images -= 1
                    logging.debug("Trailing frame read, skipped {} frames".format(skipped_images))
                    try:
                        results = self._analyzeFrame(frame)
                        buffer = io.BytesIO()
                        results[0].save(buffer, format="JPEG")
                        buffer.seek(0)
                        img_bytes = buffer.getvalue()
                        logging.debug("send image %s" % id(img_bytes))
                        self._vision_queue.send((img_bytes, results[1], results[2], results[3], results[4]))
                    except Exception, e:
                        logging.exception("error reading image")
                    finally:
                        frame = None 
Example 6
Project: appengine-photoalbum-example   Author: GoogleCloudPlatform   File: main.py    Apache License 2.0 5 votes vote down vote up
def get_labels(photo_file):
    vision_client = vision.Client()
    image = vision_client.image(
                source_uri = 'gs://%s/%s' % (bucket_name, photo_file))
    return image.detect_labels(limit=3) 
Example 7
Project: appengine-photoalbum-example   Author: GoogleCloudPlatform   File: main.py    Apache License 2.0 5 votes vote down vote up
def translate_text(text):
    if tag_language == 'en':
        return text
    translate_client = translate.Client()
    result = translate_client.translate(text, target_language=tag_language)
    return result['translatedText']