static float[] GetEmbedding(Bitmap image) { var faces = _faceDetectorLight.Forward(image); using var cropped = Imaging.Crop(image, faces.First()); var points = _faceLandmarksExtractor.Forward(cropped); using var aligned = FaceLandmarksExtractor.Align(cropped, points); return(_faceEmbedder.Forward(aligned)); }
/// <summary> /// Returns face recognition results. /// </summary> /// <param name="image">Image</param> /// <param name="rectangles">Rectangles</param> /// <returns>Array</returns> public float[][] Forward(Bitmap image, params Rectangle[] rectangles) { int length = rectangles.Length; float[][] vector = new float[length][]; for (int i = 0; i < length; i++) { using var cropped = Imaging.Crop(image, rectangles[i]); vector[i] = Forward(cropped); } return(vector); }
static string[] GetEmotionAndBeauty(Bitmap image, Rectangle face) { using var cropped = Imaging.Crop(image, face); var points = _faceLandmarksExtractor.Forward(cropped); using var aligned = FaceLandmarksExtractor.Align(cropped, points); var emotion = _faceEmotionClassifier.Forward(aligned); var emotionLabel = FaceEmotionClassifier.Labels[emotion.Argmax()]; var beauty = _faceBautyClassifier.Forward(aligned); var beautyLabel = $"{Math.Round(2 * beauty.Max(), 1)}/10.0"; Console.WriteLine($"--> classified as [{emotionLabel}] emotion and [{beautyLabel}] beauty"); return(new string[] { emotionLabel, beautyLabel }); }
static string[] GetRaceAndAge(Bitmap image, Rectangle face) { using var cropped = Imaging.Crop(image, face); var points = _faceLandmarksExtractor.Forward(cropped); using var aligned = FaceLandmarksExtractor.Align(cropped, points); var race = _faceRaceClassifier.Forward(aligned); var raceLabel = FaceRaceClassifier.Labels[race.Argmax()]; var age = _faceAgeClassifier.Forward(aligned); var ageLabel = FaceAgeClassifier.Labels[age.Argmax()]; Console.WriteLine($"--> classified as [{raceLabel}] race and [{ageLabel}] age"); return(new string[] { raceLabel, ageLabel }); }
/// <summary> /// Returns face landmarks. /// </summary> /// <param name="image">Image</param> /// <param name="rectangles">Rectangles</param> /// <returns>Points</returns> public Point[][] Forward(Bitmap image, params Rectangle[] rectangles) { var length = rectangles.Length; var vector = new Point[length][]; for (int i = 0; i < length; i++) { var rectangle = rectangles[i]; using var cropped = Imaging.Crop(image, rectangle); var points = Forward(cropped); var count = points.Length; for (int j = 0; j < points.Length; j++) { points[j] = new Point( points[j].X + rectangle.X, points[j].Y + rectangle.Y); } vector[i] = points; } return(vector); }
public override RanorexStepExecutionResponse Execute(Dictionary <string, object> arguments) { RanorexStepExecutionResponse stepResponse = new RanorexStepExecutionResponse(); try { RepoItemInfo repoItemInfo = null; Ranorex.Unknown adapter = null; string target = (string)arguments.GetValueOrDefault <string, object>("target"); int timeout = Convert.ToInt32(arguments.GetValueOrDefault <string, object>("timeout")); bool retrieveImage = (bool)arguments.GetValueOrDefault <string, object>("retrieveImage"); System.Collections.IDictionary imageProps = (System.Collections.IDictionary)arguments["imageValidationProperties"]; System.Collections.IList attributes = (System.Collections.IList)arguments["attributes"]; if (timeout == default(int)) { timeout = 10000; } if (target != default(string)) { repoItemInfo = CreateItemInfo(repo, target, timeout); adapter = CreateAdapter(repoItemInfo); Dictionary <string, object> structuredData = new Dictionary <string, object>(); Dictionary <string, object> receivedAttributes = new Dictionary <string, object>(); foreach (System.Collections.IDictionary attribute in attributes) { string name = (string)attribute["header"]; string value = adapter.Element.GetAttributeValueText(name); stepResponse.message += name + "=" + value + "\n"; receivedAttributes.Add(name, value); } structuredData.Add("attributes", receivedAttributes); if (imageProps != null) { string validationMode = (string)imageProps["validationMode"]; if (validationMode != "NONE") { string image = (string)imageProps["imageData"]; int rectX = Convert.ToInt32(imageProps["clipX"]); int rectY = Convert.ToInt32(imageProps["clipY"]); int rectWidth = Convert.ToInt32(imageProps["clipWidth"]); int rectHeight = Convert.ToInt32(imageProps["clipHeight"]); try { CompressedImage compressedImage = new CompressedImage(image); using (Bitmap bitmap = Imaging.Crop(compressedImage.Image, new Rectangle(rectX, rectY, rectWidth, rectHeight))) { compressedImage = new CompressedImage(bitmap); } if (validationMode == "CONTAINS_IMAGE") { Validate.ContainsImage(repoItemInfo, compressedImage, Imaging.FindOptions.Default); } else if (validationMode == "COMPARE_IMAGE") { Validate.ContainsImage(repoItemInfo, compressedImage, Imaging.FindOptions.Default); } structuredData.Add("imageValidated", "true"); stepResponse.message += "#imageValidated=true\n"; } catch (ValidationException e) { structuredData.Add("imageValidated", "false"); stepResponse.message += "#imageValidated=false\n"; } } } stepResponse.structuredData = structuredData; if (retrieveImage) { stepResponse.image64 = GetImage64(adapter); } } else { throw new Exception("No target defined!"); } stepResponse.success = true; } catch (Exception e) { stepResponse.message = "Ranorex Exception: " + e.Message; stepResponse.success = false; } return(stepResponse); }