Skip to content

Commit f06d1f4

Browse files
committed
check for null when detecting faces
1 parent 12ff0e6 commit f06d1f4

3 files changed

Lines changed: 254 additions & 241 deletions

File tree

Examples/ServiceExamples/Scripts/ExampleVisualRecognition.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,14 @@ private void OnDetectFaces(FacesTopLevelMultiple multipleImages, string data)
194194
Log.Debug("ExampleVisualRecognition", "\tsource_url: {0}, resolved_url: {1}", faces.source_url, faces.resolved_url);
195195
foreach (OneFaceResult face in faces.faces)
196196
{
197-
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);
198-
Log.Debug("ExampleVisulaRecognition", "\t\tGender: {0}, Score: {1}", face.gender.gender, face.gender.score);
199-
Log.Debug("ExampleVisulaRecognition", "\t\tAge Min: {0}, Age Max: {1}, Score: {2}", face.age.min, face.age.max, face.age.score);
200-
Log.Debug("ExampleVisulaRecognition", "\t\tName: {0}, Score: {1}, Type Heiarchy: {2}", face.identity.name, face.identity.score, face.identity.type_hierarchy);
197+
if(face.face_location != null)
198+
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);
199+
if(face.gender != null)
200+
Log.Debug("ExampleVisulaRecognition", "\t\tGender: {0}, Score: {1}", face.gender.gender, face.gender.score);
201+
if(face.age != null)
202+
Log.Debug("ExampleVisulaRecognition", "\t\tAge Min: {0}, Age Max: {1}, Score: {2}", face.age.min, face.age.max, face.age.score);
203+
if(face.identity != null)
204+
Log.Debug("ExampleVisulaRecognition", "\t\tName: {0}, Score: {1}, Type Heiarchy: {2}", face.identity.name, face.identity.score, face.identity.type_hierarchy);
201205
}
202206
}
203207
}

Examples/WidgetExamples/Scripts/WebCamRecognition.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
using IBM.Watson.DeveloperCloud.Services.VisualRecognition.v3;
2323
using IBM.Watson.DeveloperCloud.Logging;
2424

25+
/// <summary>
26+
/// This is an example class showing how to use Visual Recognition with the WebCam.
27+
/// </summary>
2528
public class WebCamRecognition : MonoBehaviour
2629
{
2730
#region Private Data
@@ -85,6 +88,8 @@ public void RecognizeText()
8588
image.Apply();
8689

8790
File.WriteAllBytes(filePath + fileName, image.EncodeToPNG());
91+
92+
Log.Debug("WebCamRecognition", "File writeen to {0}{1}", filePath, fileName);
8893
}
8994

9095
private IEnumerator ClassifyImage()
@@ -160,10 +165,15 @@ private void OnDetectFaces(FacesTopLevelMultiple multipleImages, string data)
160165
Log.Debug("WebCamRecognition", "\tsource_url: {0}, resolved_url: {1}", faces.source_url, faces.resolved_url);
161166
foreach (OneFaceResult face in faces.faces)
162167
{
163-
Log.Debug("WebCamRecognition", "\t\tFace location: {0}, {1}, {2}, {3}", face.face_location.left, face.face_location.top, face.face_location.width, face.face_location.height);
164-
Log.Debug("WebCamRecognition", "\t\tGender: {0}, Score: {1}", face.gender.gender, face.gender.score);
165-
Log.Debug("WebCamRecognition", "\t\tAge Min: {0}, Age Max: {1}, Score: {2}", face.age.min, face.age.max, face.age.score);
166-
Log.Debug("WebCamRecognition", "\t\tName: {0}, Score: {1}, Type Heiarchy: {2}", face.identity.name, face.identity.score, face.identity.type_hierarchy);
168+
if(face.face_location != null)
169+
Log.Debug("WebCamRecognition", "\t\tFace location: {0}, {1}, {2}, {3}", face.face_location.left, face.face_location.top, face.face_location.width, face.face_location.height);
170+
if(face.gender != null)
171+
Log.Debug("WebCamRecognition", "\t\tGender: {0}, Score: {1}", face.gender.gender, face.gender.score);
172+
if(face.age != null)
173+
Log.Debug("WebCamRecognition", "\t\tAge Min: {0}, Age Max: {1}, Score: {2}", face.age.min, face.age.max, face.age.score);
174+
175+
if(face.identity != null)
176+
Log.Debug("WebCamRecognition", "\t\tName: {0}, Score: {1}, Type Heiarchy: {2}", face.identity.name, face.identity.score, face.identity.type_hierarchy);
167177
}
168178
}
169179
}

0 commit comments

Comments
 (0)