/// <summary> /// Simples the faces detect callback. /// </summary> /// <param name="result">Result.</param> void SimpleFacesDetectCallback(string result) { Debug.Log("SimpleFacesDetectCallback result" + result); string json = result; IDictionary detects = (IDictionary)Json.Deserialize(json); foreach (DictionaryEntry detect in detects) { Debug.Log("detects key " + detect.Key); string key = (string)detect.Key; if (key.Equals("error")) { Debug.Log((string)detects [detect.Key]); } else { IList <object> rects = (IList <object>)detects [detect.Key]; //flip Rects by convenient method, IList <object> flipRects = OpenCVObjectDetector.FlipRects(rects, ((Texture2D)GetComponent <Renderer> ().material.mainTexture).width, ((Texture2D)GetComponent <Renderer> ().material.mainTexture).height, 0); OpenCVObjectDetector.DrawRects((Texture2D)GetComponent <Renderer> ().material.mainTexture, Json.Serialize(flipRects), 0, 0, 255, 2); ResultRectsToResultGameObjects(flipRects, new Color(0.0f, 0.0f, 1.0f, 0.3f), -40); } } }
/// <summary> /// Detects the face parts callback. /// </summary> /// <param name="result">Result.</param> void DetectFacePartsCallback(string result) { Debug.Log("DetectFacePartsCallback result" + result); Texture2D texture = (Texture2D)GetComponent <Renderer> ().material.mainTexture; string json = result; IDictionary detects = (IDictionary)Json.Deserialize(json); foreach (DictionaryEntry detect in detects) { Debug.Log("detects key " + detect.Key); string key = (string)detect.Key; if (key.Equals("error")) { Debug.Log((string)detects [detect.Key]); } else { int r = 0; int g = 0; int b = 0; if (key.Equals("haarcascade_mcs_lefteye")) { r = 255; g = 0; b = 0; } else if (key.Equals("haarcascade_mcs_righteye")) { r = 0; g = 255; b = 0; } else if (key.Equals("haarcascade_mcs_nose")) { r = 255; g = 255; b = 0; } else if (key.Equals("haarcascade_mcs_mouth")) { r = 0; g = 255; b = 255; } IList <object> rects = (IList <object>)detects [detect.Key]; //flip Rects by convenient method, IList <object> flipRects = OpenCVObjectDetector.FlipRects(rects, texture.width, texture.height, 0); OpenCVObjectDetector.DrawRects(texture, Json.Serialize(flipRects), r, g, b, 2); ResultRectsToResultGameObjects(flipRects, new Color((float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, 0.3f), -80); } } }
/// <summary> /// Detects the faces callback. /// </summary> /// <param name="result">Result.</param> void DetectFacesCallback(string result) { Debug.Log("DetectFacesCallback result" + result); Texture2D texture = (Texture2D)GetComponent <Renderer> ().material.mainTexture; string json = result; IDictionary detects = (IDictionary)Json.Deserialize(json); foreach (DictionaryEntry detect in detects) { Debug.Log("detects key " + detect.Key); string key = (string)detect.Key; if (key.Equals("error")) { Debug.Log((string)detects [detect.Key]); } else { IList <object> rects = (IList <object>)detects [detect.Key]; //flip Rects by convenient method, IList <object> flipRects = OpenCVObjectDetector.FlipRects(rects, texture.width, texture.height, 0); OpenCVObjectDetector.DrawRects(texture, Json.Serialize(flipRects), 0, 0, 255, 2); ResultRectsToResultGameObjects(flipRects, new Color(0.0f, 0.0f, 1.0f, 0.3f), -40); OpenCVObjectDetector.RemoveAllObjectDetectorParam(); int id = 0; foreach (IDictionary rect in rects) { IDictionary <string, object> leftEyeParam = new Dictionary <string, object> (); leftEyeParam.Add("filename", "haarcascade_mcs_lefteye"); leftEyeParam.Add("scaleFactor", 1.1); leftEyeParam.Add("minNeighbors", 2); leftEyeParam.Add("flags", 0 | OpenCVObjectDetector.CV_HAAR_SCALE_IMAGE); leftEyeParam.Add("minWidth", (long)rect ["width"] / 15); leftEyeParam.Add("minHeight", (long)rect ["height"] / 15); leftEyeParam.Add("flipCode", 0); IList <object> leftEyeRects = new List <object> (); IDictionary <string, long> leftEyeRect = new Dictionary <string, long> (); leftEyeRect.Add("x", (long)rect ["x"] + (long)rect ["width"] / 3); leftEyeRect.Add("y", (long)rect ["y"]); leftEyeRect.Add("width", (long)rect ["width"] / 3 * 2); leftEyeRect.Add("height", (long)rect ["height"] / 3 * 2); leftEyeRect.Add("id", id); leftEyeRects.Add(leftEyeRect); leftEyeParam.Add("rects", leftEyeRects); OpenCVObjectDetector.AddObjectDetectorParam(Json.Serialize(leftEyeParam)); IDictionary <string, object> rightEyeParam = new Dictionary <string, object> (); rightEyeParam.Add("filename", "haarcascade_mcs_righteye"); rightEyeParam.Add("scaleFactor", 1.1); rightEyeParam.Add("minNeighbors", 2); rightEyeParam.Add("flags", 0 | OpenCVObjectDetector.CV_HAAR_SCALE_IMAGE); rightEyeParam.Add("minWidth", (long)rect ["width"] / 15); rightEyeParam.Add("minHeight", (long)rect ["height"] / 15); rightEyeParam.Add("flipCode", 0); IList <object> rightEyeRects = new List <object> (); IDictionary <string, long> rightEyeRect = new Dictionary <string, long> (); rightEyeRect.Add("x", (long)rect ["x"]); rightEyeRect.Add("y", (long)rect ["y"]); rightEyeRect.Add("width", (long)rect ["width"] / 3 * 2); rightEyeRect.Add("height", (long)rect ["height"] / 3 * 2); rightEyeRect.Add("id", id); rightEyeRects.Add(rightEyeRect); rightEyeParam.Add("rects", rightEyeRects); OpenCVObjectDetector.AddObjectDetectorParam(Json.Serialize(rightEyeParam)); IDictionary <string, object> noseParam = new Dictionary <string, object> (); noseParam.Add("filename", "haarcascade_mcs_nose"); noseParam.Add("scaleFactor", 1.1); noseParam.Add("minNeighbors", 2); noseParam.Add("flags", 0 | OpenCVObjectDetector.CV_HAAR_SCALE_IMAGE); noseParam.Add("minWidth", (long)rect ["width"] / 15); noseParam.Add("minHeight", (long)rect ["height"] / 15); noseParam.Add("flipCode", 0); IList <object> noseRects = new List <object> (); IDictionary <string, long> noseRect = new Dictionary <string, long> (); noseRect.Add("x", (long)rect ["x"] + (long)rect ["width"] / 4); noseRect.Add("y", (long)rect ["y"] + (long)rect ["height"] / 2); noseRect.Add("width", (long)rect ["width"] / 2); noseRect.Add("height", (long)rect ["height"] / 3); noseRect.Add("id", id); noseRects.Add(noseRect); noseParam.Add("rects", noseRects); OpenCVObjectDetector.AddObjectDetectorParam(Json.Serialize(noseParam)); IDictionary <string, object> mouthParam = new Dictionary <string, object> (); mouthParam.Add("filename", "haarcascade_mcs_mouth"); mouthParam.Add("scaleFactor", 1.1); mouthParam.Add("minNeighbors", 2); mouthParam.Add("flags", 0 | OpenCVObjectDetector.CV_HAAR_SCALE_IMAGE); mouthParam.Add("minWidth", (long)rect ["width"] / 10); mouthParam.Add("minHeight", (long)rect ["height"] / 10); mouthParam.Add("flipCode", 0); IList <object> mouthRects = new List <object> (); IDictionary <string, long> mouthRect = new Dictionary <string, long> (); mouthRect.Add("x", (long)rect ["x"]); mouthRect.Add("y", (long)rect ["y"] + (long)rect ["height"] / 2); mouthRect.Add("width", (long)rect ["width"]); mouthRect.Add("height", (long)rect ["height"] / 2); mouthRect.Add("id", id); mouthRects.Add(mouthRect); mouthParam.Add("rects", mouthRects); OpenCVObjectDetector.AddObjectDetectorParam(Json.Serialize(mouthParam)); id++; } OpenCVObjectDetector.DetectAsync(texture, gameObject.name, "DetectFacePartsCallback"); } } }