Lecture 10 Cloud Vision API Topics Google Cloud













- Slides: 13

Lecture 10: Cloud Vision API Topics: Google Cloud Vision API, Label Detection

Remember Intent/Camera? • We used implicit intent to take pictures and get the image back. We want to use AI to detect what’s in it! L 2: S 28

Google Cloud APIs • APIs offered by Google Cloud Platform:

Cloud Vision APIs • APIs for – Image labeling, face, logo, landmark detection, optical character recognition, explicit content detection.

Cloud Vision APIs • APIs for – Image labeling, face, logo, landmark detection, optical character recognition, explicit content detection.

Cloud Vision APIs • APIs for – Image labeling, face, logo, landmark detection, optical character recognition, explicit content detection.

Using Cloud Vision API with Android • Get an API key – Create account, create credentials, create API key, enable API, copy the key. https: //cloud. google. com/apis/ • Gradle/Permissions – Edit. gradle and manifest files. build. gradle (module: app) implementation 'com. google. api-client: google-api-client-android: 1. 23. 0' exclude module: 'httpclient' implementation 'com. google. http-client: google-http-client-gson: 1. 23. 0' exclude module: 'httpclient' implementation 'com. google. apis: google-api-services-vision: v 1 -rev 369 -1. 23. 0' Android. Manifest. xml <uses-permission android: name="android. permission. INTERNET" />

Android Code • Step 1 – ENCODE Image to Base 64 Bitmap bitmap = ((Bitmap. Drawable)get. Resources(). get. Drawable(R. drawable. b 1)). get. Bitmap(); Byte. Array. Output. Stream bout = new Byte. Array. Output. Stream(); bitmap. compress(Bitmap. Compress. Format. JPEG, 90, bout); Image myimage = new Image(); myimage. encode. Content(bout. to. Byte. Array());

Android Code • Step 2 – PREPARE Annotate. Image. Request annotate. Image. Request = new Annotate. Image. Request(); annotate. Image. Request. set. Image(myimage); Feature f = new Feature(); f. set. Type("LABEL_DETECTION"); f. set. Max. Results(5); List<Feature> lf = new Array. List<Feature>(); lf. add(f); annotate. Image. Request. set. Features(lf);

Android Code • Step 3 – BUILD Vision Http. Transport http. Transport = Android. Http. new. Compatible. Transport(); Gson. Factory json. Factory = Gson. Factory. get. Default. Instance(); Vision. Builder builder = new Vision. Builder(http. Transport, json. Factory, null); builder. set. Vision. Request. Initializer(new Vision. Request. Initializer(“YOUR_KEY")); Vision vision = builder. build();

Android Code • Step 4 – CALL Vision. Images. Annotate Batch. Annotate. Images. Request request = new Batch. Annotate. Images. Request(); List<Annotate. Image. Request> list = new Array. List<Annotate. Image. Request>(); list. add(annotate. Image. Request); request. set. Requests(list); Vision. Images. Annotate task = vision. images(). annotate(request); Batch. Annotate. Images. Response response = task. execute(); Log. v("MYTAG", response. to. Pretty. String());

Code Practice • Study Google. Cloud. Vision_Label. Detection in the Git. Hub and modify it to integrate camera images. https: //github. com/uncmobile/Google. Could. Vision_Label. Detection

References • https: //cloud. google. com/apis/docs/overview • https: //cloud. google. com/vision/overview/docs/ • https: //cloud. google. com/vision/docs/samples