|
| 1 | +/** |
| 2 | +* Copyright 2015 IBM Corp. All Rights Reserved. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +* |
| 16 | +*/ |
| 17 | + |
| 18 | +using UnityEngine; |
| 19 | +using System.Collections; |
| 20 | +using IBM.Watson.DeveloperCloud.Services.VisualRecognition.v3; |
| 21 | +using IBM.Watson.DeveloperCloud.Logging; |
| 22 | +using IBM.Watson.DeveloperCloud.Utilities; |
| 23 | + |
| 24 | +public class ExampleVisualRecognition : MonoBehaviour { |
| 25 | + private VisualRecognition m_VisualRecognition = new VisualRecognition(); |
| 26 | + private string m_classifierName = "Apples_OptionalParams"; |
| 27 | + private string m_classifierID = "Apples_OptionalParams_1213405640"; |
| 28 | + private string m_classifierToDelete = "unitytestclassifier2b_37849361"; |
| 29 | + private string m_imageURL = "https://upload.wikimedia.org/wikipedia/commons/e/e9/Official_portrait_of_Barack_Obama.jpg"; |
| 30 | + private string m_imageTextURL = "http://i.stack.imgur.com/ZS6nH.png"; |
| 31 | + |
| 32 | + void Start () |
| 33 | + { |
| 34 | + LogSystem.InstallDefaultReactors(); |
| 35 | + |
| 36 | +// Get all classifiers |
| 37 | + if(!m_VisualRecognition.GetClassifiers(OnGetClassifiers)) |
| 38 | + Log.Debug("ExampleVisualRecognition", "Getting classifiers failed!"); |
| 39 | + |
| 40 | +// Find classifier by name |
| 41 | + m_VisualRecognition.FindClassifier(m_classifierName, OnFindClassifier); |
| 42 | + |
| 43 | +// Find classifier by ID |
| 44 | + if(!m_VisualRecognition.GetClassifier(m_classifierID, OnGetClassifier)) |
| 45 | + Log.Debug("ExampleVisualRecognition", "Getting classifier failed!"); |
| 46 | + |
| 47 | +// Delete classifier by ID |
| 48 | + if(!m_VisualRecognition.DeleteClassifier(m_classifierToDelete, OnDeleteClassifier)) |
| 49 | + Log.Debug("ExampleVisualRecognition", "Deleting classifier failed!"); |
| 50 | + |
| 51 | +// Train classifier |
| 52 | + string m_positiveExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/taj_positive_examples.zip"; |
| 53 | + string m_negativeExamplesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/negative_examples.zip"; |
| 54 | + if(!m_VisualRecognition.TrainClassifier("unity-test-classifier5", "taj", m_positiveExamplesPath, m_negativeExamplesPath, OnTrainClassifier)) |
| 55 | + Log.Debug("ExampleVisualRecognition", "Train classifier failed!"); |
| 56 | + |
| 57 | +// Classify get |
| 58 | + if(!m_VisualRecognition.Classify(m_imageURL, OnClassify)) |
| 59 | + Log.Debug("ExampleVisualRecognition", "Classify image failed!"); |
| 60 | + |
| 61 | +// Classify post image |
| 62 | + string m_imagesPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/obama.jpg"; |
| 63 | + string[] m_owners = {"IBM", "me"}; |
| 64 | + string[] m_classifierIDs = {"default"}; |
| 65 | + if(!m_VisualRecognition.Classify(OnClassify, m_imagesPath, m_owners, m_classifierIDs, 0.5f)) |
| 66 | + Log.Debug("ExampleVisualRecognition", "Classify image failed!"); |
| 67 | + |
| 68 | + |
| 69 | +// Detect faces get |
| 70 | + if(!m_VisualRecognition.DetectFaces(m_imageURL, OnDetectFaces)) |
| 71 | + Log.Debug("ExampleVisualRecogntiion", "Detect faces failed!"); |
| 72 | + |
| 73 | +// Detect faces post image |
| 74 | + string m_faceExamplePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/obama.jpg"; |
| 75 | + if(!m_VisualRecognition.DetectFaces(OnDetectFaces, m_faceExamplePath)) |
| 76 | + Log.Debug("ExampleVisualRecognition", "Detect faces failed!"); |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +// Recognize text get |
| 81 | + if(!m_VisualRecognition.RecognizeText(m_imageTextURL, OnRecognizeText)) |
| 82 | + Log.Debug("ExampleVisualRecognition", "Recognize text failed!"); |
| 83 | + |
| 84 | +// Recognize text post image |
| 85 | + string m_textExamplePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/from_platos_apology.png"; |
| 86 | + if(!m_VisualRecognition.RecognizeText(OnRecognizeText, m_textExamplePath)) |
| 87 | + Log.Debug("ExampleVisualRecognition", "Recognize text failed!"); |
| 88 | + } |
| 89 | + |
| 90 | + private void OnGetClassifiers (GetClassifiersTopLevelBrief classifiers) |
| 91 | + { |
| 92 | + if(classifiers != null && classifiers.classifiers.Length > 0) |
| 93 | + { |
| 94 | + foreach(GetClassifiersPerClassifierBrief classifier in classifiers.classifiers) |
| 95 | + { |
| 96 | + Log.Debug("ExampleVisualRecognition", "Classifier: " + classifier.name + ", " + classifier.classifier_id); |
| 97 | + } |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + Log.Debug("ExampleVisualRecognition", "Failed to get classifiers!"); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private void OnFindClassifier(GetClassifiersPerClassifierVerbose classifier) |
| 106 | + { |
| 107 | + if(classifier != null) |
| 108 | + { |
| 109 | + Log.Debug("ExampleVisualRecognition", "Classifier " + m_classifierName + " found! ClassifierID: " + classifier.classifier_id); |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + Log.Debug("ExampleVisualRecognition", "Failed to find classifier by name!"); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private void OnGetClassifier(GetClassifiersPerClassifierVerbose classifier) |
| 118 | + { |
| 119 | + if(classifier != null) |
| 120 | + { |
| 121 | + Log.Debug("ExampleVisualRecognition", "Classifier " + m_classifierID + " found! Classifier name: " + classifier.name); |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + Log.Debug("ExampleVisualRecognition", "Failed to find classifier by ID!"); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private void OnDeleteClassifier(bool success) |
| 130 | + { |
| 131 | + if(success) |
| 132 | + { |
| 133 | + Log.Debug("ExampleVisualRecognition", "Deleted classifier " + m_classifierToDelete); |
| 134 | + } |
| 135 | + else |
| 136 | + { |
| 137 | + Log.Debug("ExampleVisualRecognition", "Failed to delete classifier by ID!"); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private void OnTrainClassifier(GetClassifiersPerClassifierVerbose classifier) |
| 142 | + { |
| 143 | + if(classifier != null) |
| 144 | + { |
| 145 | + Log.Debug("ExampleVisualRecognition", "Classifier is training! " + classifier); |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + Log.Debug("ExampleVisualRecognition", "Failed to train classifier!"); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private void OnClassify(ClassifyTopLevelMultiple classify) |
| 154 | + { |
| 155 | + if(classify != null) |
| 156 | + { |
| 157 | + Log.Debug("ExampleVisualRecognition", "images processed: " + classify.images_processed); |
| 158 | + foreach(ClassifyTopLevelSingle image in classify.images) |
| 159 | + { |
| 160 | + Log.Debug("ExampleVisualRecognition", "\tsource_url: " + image.source_url + ", resolved_url: " + image.resolved_url); |
| 161 | + foreach(ClassifyPerClassifier classifier in image.classifiers) |
| 162 | + { |
| 163 | + Log.Debug("ExampleVisualRecognition", "\t\tclassifier_id: " + classifier.classifier_id + ", name: " + classifier.name); |
| 164 | + foreach(ClassResult classResult in classifier.classes) |
| 165 | + Log.Debug("ExampleVisualRecognition", "\t\t\tclass: " + classResult.m_class + ", score: " + classResult.score + ", type_hierarchy: " + classResult.type_hierarchy); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + else |
| 170 | + { |
| 171 | + Log.Debug("ExampleVisualRecognition", "Classification failed!"); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private void OnDetectFaces(FacesTopLevelMultiple multipleImages) |
| 176 | + { |
| 177 | + if(multipleImages != null) |
| 178 | + { |
| 179 | + Log.Debug("ExampleVisualRecognition", "images processed: {0}", multipleImages.images_processed); |
| 180 | + foreach(FacesTopLevelSingle faces in multipleImages.images) |
| 181 | + { |
| 182 | + Log.Debug("ExampleVisualRecognition", "\tsource_url: {0}, resolved_url: {1}", faces.source_url, faces.resolved_url); |
| 183 | + foreach(OneFaceResult face in faces.faces) |
| 184 | + { |
| 185 | + Log.Debug("ExampleVisulaRecognition", "\t\tFace location: {0}, {1}, {2}, {3}", face.face_location.left, face.face_location.top, face.face_location.width, face.face_location.height); |
| 186 | + Log.Debug("ExampleVisulaRecognition", "\t\tGender: {0}, Score: {1}", face.gender.gender, face.gender.score); |
| 187 | + Log.Debug("ExampleVisulaRecognition", "\t\tAge Min: {0}, Age Max: {1}, Score: {2}", face.age.min, face.age.max, face.age.score); |
| 188 | + Log.Debug("ExampleVisulaRecognition", "\t\tName: {0}, Score: {1}, Type Heiarchy: {2}", face.identity.name, face.identity.score, face.identity.type_hierarchy); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + else |
| 193 | + { |
| 194 | + Log.Debug("ExampleVisualRecognition", "Detect faces failed!"); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + private void OnRecognizeText(TextRecogTopLevelMultiple multipleImages) |
| 199 | + { |
| 200 | + if(multipleImages != null) |
| 201 | + { |
| 202 | + Log.Debug("ExampleVisualRecognition", "images processed: {0}", multipleImages.images_processed); |
| 203 | + foreach(TextRecogTopLevelSingle texts in multipleImages.images) |
| 204 | + { |
| 205 | + Log.Debug("ExampleVisualRecognition", "\tsource_url: {0}, resolved_url: {1}", texts.source_url, texts.resolved_url); |
| 206 | + Log.Debug("ExampleVisualRecognition", "\ttext: {0}", texts.text); |
| 207 | + foreach(TextRecogOneWord text in texts.words) |
| 208 | + { |
| 209 | + Log.Debug("ExampleVisulaRecognition", "\t\ttext location: {0}, {1}, {2}, {3}", text.location.left, text.location.top, text.location.width, text.location.height); |
| 210 | + Log.Debug("ExampleVisulaRecognition", "\t\tLine number: {0}", text.line_number); |
| 211 | + Log.Debug("ExampleVisulaRecognition", "\t\tword: {0}, Score: {1}", text.word, text.score); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + else |
| 216 | + { |
| 217 | + Log.Debug("ExampleVisualRecognition", "RecognizeText failed!"); |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments