/// <summary> /// 对图片做opencv的人脸分析 /// </summary> /// <param name="imgTag"></param> public static void OpenCVAnalysis(ImageTag imgTag) { var imageFile = imgTag.GetTrueImageFile(); var image = Cv2.ImRead(imageFile); var tps = new List <TagPart>(); foreach (var face in FaceDetect.OpenCvDetectMultiScale(image)) { var body = AITag.Common.Utils.GetBodyRect(face, (double)(64 - 8) / 128, image.Width, image.Height); if (body != Rect.Empty) { tps.Add(new TagPart { Face = face, Body = body }); } } if (tps.Count == 0) { imgTag.Status = "opencv_fail"; } else { imgTag.OpenCvParts = tps; imgTag.Status = "opencv_finish"; } }
void LoadPageShow() { currentShowTag = images[currentIndex]; var image = Cv2.ImRead(currentShowTag.GetTrueImageFile()); var tps = currentShowTag.OpenCvParts; if (tps == null || tps.Count == 0) { // 网上有看到有人先转灰度再识别,实际效果也没好多少 var gray_img = image.CvtColor(ColorConversionCodes.RGB2GRAY); var ract = FaceDetect.OpenCvDetectMultiScale(image); tps = new List <TagPart>(); foreach (var face in ract) { var body = AITag.Common.Utils.GetBodyRect(face, (double)(64 - 8) / 128, image.Width, image.Height); if (body != Rect.Empty) { tps.Add(new TagPart { Face = face, Body = body }); } } } if (tps.Count == 0) { UpdateIamgeTagStatus(currentShowTag, "opencv_fail"); btnNextImage_Click(null, null); return; } currentShowTag.OpenCvParts = tps; panelParts.Children.Clear(); foreach (var item in tps) { var face = item.Face; var body = item.Body; Cv2.Rectangle(image, face, Scalar.Red); var i2 = image.Clone(body); var utp = new UserTagParts(); utp.InitData(item, i2); panelParts.Children.Add(utp); Cv2.Rectangle(image, body, Scalar.Red); var ioa = InputOutputArray.Create(image); // 顺便标注一下尺寸 Cv2.PutText(ioa, $"top:{face.Top} left:{face.Left} w:{face.Width} h:{face.Height}", new Point(face.Left, face.Bottom + 2), HersheyFonts.HersheySimplex, 1, Scalar.Blue); ioa = InputOutputArray.Create(image); Cv2.PutText(ioa, $"top:{body.Top} left:{body.Left} w:{body.Width} h:{body.Height}", new Point(body.Left, body.Top - 10), HersheyFonts.HersheySimplex, 1, Scalar.Blue); } imgShow.Source = image.MatToBitmapImage(); panelCurrentImgTags.Children.Clear(); if (!string.IsNullOrEmpty(currentShowTag.TagsName)) { AppendTagtoCurrentTagPanel(currentShowTag.TagsName); } }