Created
October 4, 2019 02:01
-
-
Save yosun/4be6c6281cc6ff1c6d4dad318894605c to your computer and use it in GitHub Desktop.
Takes a Unity Texture2D and get a Google Cloud vision annotation response. Please replace api key accordingly and *make sure Google Cloud billing is enabled*
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
namespace GoogleCloudVision { | |
[System.Serializable] | |
public class AnnotateImageRequests | |
{ | |
public List<AnnotateImageRequest> requests; | |
} | |
[System.Serializable] | |
public class AnnotateImageRequest | |
{ | |
public Image image; | |
public List<Feature> features; | |
} | |
[System.Serializable] | |
public class Image | |
{ | |
public string content; | |
} | |
[System.Serializable] | |
public class Feature | |
{ | |
public string type; | |
public int maxResults; | |
} | |
public enum FeatureType | |
{ | |
TYPE_UNSPECIFIED, | |
FACE_DETECTION, | |
LANDMARK_DETECTION, | |
LOGO_DETECTION, | |
LABEL_DETECTION, | |
TEXT_DETECTION, | |
SAFE_SEARCH_DETECTION, | |
IMAGE_PROPERTIES | |
} | |
[System.Serializable] | |
public class AnnotateImageResponses | |
{ | |
public List<AnnotateImageResponse> responses; | |
} | |
[System.Serializable] | |
public class AnnotateImageResponse | |
{ | |
public List<FaceAnnotation> faceAnnotations; | |
public List<EntityAnnotation> landmarkAnnotations; | |
public List<EntityAnnotation> logoAnnotations; | |
public List<EntityAnnotation> labelAnnotations; | |
public List<EntityAnnotation> textAnnotations; | |
} | |
[System.Serializable] | |
public class FaceAnnotation | |
{ | |
public BoundingPoly boundingPoly; | |
public BoundingPoly fdBoundingPoly; | |
public List<Landmark> landmarks; | |
public float rollAngle; | |
public float panAngle; | |
public float tiltAngle; | |
public float detectionConfidence; | |
public float landmarkingConfidence; | |
public string joyLikelihood; | |
public string sorrowLikelihood; | |
public string angerLikelihood; | |
public string surpriseLikelihood; | |
public string underExposedLikelihood; | |
public string blurredLikelihood; | |
public string headwearLikelihood; | |
} | |
[System.Serializable] | |
public class EntityAnnotation | |
{ | |
public string mid; | |
public string locale; | |
public string description; | |
public float score; | |
public float confidence; | |
public float topicality; | |
public BoundingPoly boundingPoly; | |
public List<LocationInfo> locations; | |
public List<Property> properties; | |
} | |
[System.Serializable] | |
public class Vertex | |
{ | |
public float x; | |
public float y; | |
} | |
[System.Serializable] | |
public class BoundingPoly | |
{ | |
public List<Vertex> vertices; | |
} | |
[System.Serializable] | |
public class Position | |
{ | |
public float x; | |
public float y; | |
public float z; | |
} | |
[System.Serializable] | |
public class Landmark | |
{ | |
public string type; | |
public Position position; | |
} | |
[System.Serializable] | |
public class Property | |
{ | |
string name; | |
string value; | |
} | |
[RequireComponent(typeof(WWWPost))] | |
public class Image2GoogleVision : MonoBehaviour | |
{ | |
public Texture2D texture; | |
public int maxResults = 10; | |
public FeatureType featureType = FeatureType.LABEL_DETECTION; | |
private void Awake() | |
{ | |
wp = GetComponent<WWWPost>(); | |
} | |
private void Start() | |
{ | |
SendToGV(texture); | |
} | |
public void SendToGV(Texture2D texture2D) | |
{ | |
string url = "https://vision.googleapis.com/v1/images:annotate"; | |
string key = | |
// texture2D.Apply(false); // Not required. Because we do not need to be uploaded it to GPU | |
byte[] jpg = texture2D.EncodeToJPG(); | |
string base64 = System.Convert.ToBase64String(jpg); | |
#if UNITY_WEBGL | |
Application.ExternalCall("post", this.gameObject.name, "OnSuccessFromBrowser", "OnErrorFromBrowser", this.url + this.apiKey, base64, this.featureType.ToString(), this.maxResults); | |
#else | |
AnnotateImageRequests requests = new AnnotateImageRequests(); | |
requests.requests = new List<AnnotateImageRequest>(); | |
AnnotateImageRequest request = new AnnotateImageRequest(); | |
request.image = new Image(); | |
request.image.content = base64; | |
request.features = new List<Feature>(); | |
Feature feature = new Feature(); | |
feature.type = this.featureType.ToString(); | |
feature.maxResults = this.maxResults; | |
request.features.Add(feature); | |
requests.requests.Add(request); | |
string jsonData = JsonUtility.ToJson(requests, false); | |
if (jsonData != string.Empty) | |
{ | |
byte[] postData = System.Text.Encoding.Default.GetBytes(jsonData); | |
url += "?key=" + key; print(url); | |
wp.Post(url, postData, ProcessReturn);//,WWWPost.SignHeaders(key,true) ); | |
} | |
} | |
void ProcessReturn( string t) | |
{ | |
print(t); | |
Sample_OnAnnotateImageResponses(JsonUtility.FromJson<AnnotateImageResponses>(t)); | |
} | |
WWWPost wp; | |
void Sample_OnAnnotateImageResponses(AnnotateImageResponses responses) | |
{ | |
if (responses.responses.Count > 0) | |
{ | |
if (responses.responses[0].labelAnnotations != null && responses.responses[0].labelAnnotations.Count > 0) | |
{ | |
// Debug.Log("desc: " + responses.responses[0].labelAnnotations[0].description); | |
for(int i=0;i< responses.responses[0].labelAnnotations.Count; i++) | |
{ | |
string desc = responses.responses[0].labelAnnotations[i].description; | |
float conf = (responses.responses[0].labelAnnotations[i].score * 100f);//.ToString("F4"); | |
print(desc + " " + conf); | |
} | |
} | |
} | |
} | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment