UCONN

UCONN
UCONN

Cloud Vision

                                                  Cloud Vision

Google Cloud Vision API is used to process images for various functions using Google’s machine learning models.

Images can be sent to the API and based on functionality that you want it returns values extracted from the images.

Some of the main features you can use are:

Label Detection: Looks for a category of an object in the picture and lists the name with a probability that the object is within the pre-defined category.

Optical Character Recognition: Searches the image for text and handwritten characters and returns the characters in text format.

Landmark & Logo Detection: Can identify and return popular landmarks from around the world and recognize major brand logos. The process will return the names of the landmarks and logos and identify corporate logos.

Face Detection: Can detect emotion from a face like joy or anger.

Install python library

pip install --upgrade google-cloud-vision


Enable vision API


gcloud services enable vision.googleapis.com


Need service account JSON key

export GOOGLE_APPLICATION_CREDENTIALS="/home/john_iacovacci1/cloud-project-examples-316c375c6892.json”

Execute program

python3 speech-to-text.py

==================================================================

import os

from google.cloud import vision

from google.cloud import storage


client = vision.ImageAnnotatorClient()


def detect_labels_uri(uri):

    image = vision.Image()

    image.source.image_uri = uri


    response = client.label_detection(image=image)

    labels = response.label_annotations

   

    print(f'Image labels detected in {uri}:')

    for label in labels:

        print(f"{label.description} (Confidence: {label.score:.2%})")


    if response.error.message:

        raise Exception(response.error.message)


if __name__ == "__main__":

    detect_labels_uri('gs://cloud-storage-exam/street.jpg')

=========================================================

Results:


john_iacovacci1@cloudshell:~/vision (cloud-project-examples)$ python3 label_exam.py

Image labels detected in gs://cloud-storage-exam/street.jpg:

Land vehicle (Confidence: 99.17%)

Car (Confidence: 99.07%)

Vehicle (Confidence: 98.93%)

Mode of transport (Confidence: 98.51%)

Transport (Confidence: 97.78%)

Motor vehicle (Confidence: 97.65%)

Wheel (Confidence: 97.60%)

Building (Confidence: 97.51%)

Tire (Confidence: 97.32%)

Automotive Exterior (Confidence: 96.40%)

john_iacovacci1@cloudshell:~/vision (cloud-project-examples)$ 





Optical Character Recognition Example


python3 ocr_exam.py


=======================================================

import io

import os

from google.cloud import vision

from google.cloud import storage

client = vision.ImageAnnotatorClient()


PROJECT_ID = 'cloud-project-examples'

bucket_name = "cloud-storage-exam"


def detect_text_uri(uri):

    image = vision.Image()

    image.source.image_uri = uri

    response = client.text_detection(image=image)

    texts = response.text_annotations


    print(f'Text in image in {uri}:')

   

    if texts:

        print(f'\nText characters found:\n{texts[0].description}')

    else:

        print("text not detected.")


    if response.error.message:

        raise Exception(

            '{}\nInformation on errors: '

            'https://cloud.google.com/apis/design/errors'.format(

                response.error.message))


detect_text_uri('gs://cloud-storage-exam/lunch.jpeg')

====================================================

Results:


john_iacovacci1@cloudshell:~/vision (cloud-project-examples)$ python3 ocr_exam.py

Text in image in gs://cloud-storage-exam/lunch.jpeg:


Text characters found:

Easy and affordable

lunches you'll love

Hot summer days call for laid-back sandwiches just the

way you like them. Pick your favorite meats from our deli

and pair them with fresh, seasonal produce for your

perfect combo.

john_iacovacci1@cloudshell:~/vision (cloud-project-examples)$ 



Face Detection


python3 face_exam.py

=========================================================

import os

from google.cloud import vision

client = vision.ImageAnnotatorClient()


PROJECT_ID = 'cloud-project-examples'

bucket_name = "cloud-storage-exam"


def detect_faces_uri(uri):

    client = vision.ImageAnnotatorClient()

    image = vision.Image()

    image.source.image_uri = uri

    response = client.face_detection(image=image)

    faces = response.face_annotations


    likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',

                       'LIKELY', 'VERY_LIKELY')


    print(f'Detected {len(faces)} face(s) in {uri}:')


    for i, face in enumerate(faces):

        print(f'\n--- Face {i+1} ---')

        print(f'Joy: {likelihood_name[face.joy_likelihood]}')

        print(f'Sorrow: {likelihood_name[face.sorrow_likelihood]}')

        print(f'Anger: {likelihood_name[face.anger_likelihood]}')

        print(f'Surprise: {likelihood_name[face.surprise_likelihood]}')

        print(f'Detection Confidence: {face.detection_confidence:.2%}')


    if response.error.message:

        raise Exception(

            '{}\nFor more info on error messages, check: '

            'https://cloud.google.com/apis/design/errors'.format(

                response.error.message))


detect_faces_uri('gs://cloud-storage-exam/face_sample.jpeg')

==================================================

Results:

john_iacovacci1@cloudshell:~/vision (cloud-project-examples)$ python3 face_exam.py

Detected 4 face(s) in gs://cloud-storage-exam/face_sample.jpeg:

--- Face 1 ---

Joy: VERY_LIKELY

Sorrow: VERY_UNLIKELY

Anger: VERY_UNLIKELY

Surprise: VERY_UNLIKELY

Detection Confidence: 95.31%

--- Face 2 ---

Joy: VERY_LIKELY

Sorrow: VERY_UNLIKELY

Anger: VERY_UNLIKELY

Surprise: VERY_UNLIKELY

Detection Confidence: 50.39%

--- Face 3 ---

Joy: VERY_LIKELY

Sorrow: VERY_UNLIKELY

Anger: VERY_UNLIKELY

Surprise: VERY_UNLIKELY

Detection Confidence: 99.22%

--- Face 4 ---

Joy: VERY_LIKELY

Sorrow: VERY_UNLIKELY

Anger: VERY_UNLIKELY

Surprise: VERY_UNLIKELY

Detection Confidence: 98.05%

john_iacovacci1@cloudshell:~/vision (cloud-project-examples)$ 



No comments:

Post a Comment

Optional Assignment #4

  I created a shorter simpler version for the Python CRUD example for those who were having issues and wish to try it out. https://uconnstam...