public BodyAnalysis(IOptions <ApiConfig> apiConfig, ILogger <BodyAnalysis> logger) { _logger = logger; _client = new Baidu.Aip.BodyAnalysis.Body(apiConfig.Value.API_KEY, apiConfig.Value.SECRET_KEY) { Timeout = 60000 }; }
/// <summary> /// 百度AI识别从背景图截取出封面 /// </summary> /// <param name="backdropPath">背景图</param> /// <param name="config">用户配置</param> /// <returns>处理完的新图像</returns> private void GetCoverByAI(string backdropPath, string coverPath) { var image = File.ReadAllBytes(backdropPath); var baseImg = Image.FromFile(backdropPath); var width = (int)(baseImg.Height * 0.7029702970297); var tempImage = new Bitmap(width, baseImg.Height); try { var left = 0; var client = new Baidu.Aip.BodyAnalysis.Body(config.BD_API_KEY, config.BD_SECRET_KEY) { Timeout = 60000 }; var result = client.BodyAnalysis(image); List <Person> personList = new List <Person>(); if (result["person_num"] != null) { var person_num = int.Parse(result["person_num"].ToString()); for (int i = 0; i < person_num; i++) { var person = new Person(); person.Left = double.Parse(result["person_info"][i]["location"]["left"].ToString()); person.Top = double.Parse(result["person_info"][i]["location"]["top"].ToString()); person.Width = double.Parse(result["person_info"][i]["location"]["width"].ToString()); person.Height = double.Parse(result["person_info"][i]["location"]["height"].ToString()); person.Score = double.Parse(result["person_info"][i]["location"]["score"].ToString()); //过滤哪些不靠谱的人物 if (person.Width >= 100 && person.Height >= 100 && person.Score > config.AIScore) { person.NoseX = double.Parse(result["person_info"][i]["body_parts"]["nose"]["x"].ToString()); personList.Add(person); } } if (personList.Count == 0) { Log.Save($"人物识别准确度太高,请适当降低"); } } //没有找到人脸 if (personList.Count == 0) { left = 0; } else if (personList.Count == 1) { //左边距离够 if (personList[0].NoseX - width / 2 >= 0) { if (personList[0].NoseX - width / 2 + width <= baseImg.Width) { left = (int)(personList[0].NoseX - width / 2); } else { left = baseImg.Width - width; } } else { left = 0; } } else if (personList.Count == 2) { //两个人物离的近 if (width - Math.Abs(personList[1].NoseX - personList[0].NoseX) >= width / 2) { left = (baseImg.Width - width) / 2; } else { double noseX = 0; //比较谁的面积大 if (personList[1].Width * personList[1].Height > personList[0].Width * personList[0].Height) { noseX = personList[1].NoseX; } else { noseX = personList[0].NoseX; } //左边距离够 if (noseX - width / 2 >= 0) { if (noseX - width / 2 + width <= baseImg.Width) { left = (int)(noseX - width / 2); } else { left = baseImg.Width - width; } } else { left = 0; } } } else if (personList.Count > 2) { //如果多于2个人 if (personList.Count > 10) { //多数情况下是影片左边缩略详情图,还是取右边做封面 if (baseImg.Width / 2 > width) { left = baseImg.Width / 2 + (baseImg.Width / 2 - width) / 2; } else { left = (baseImg.Width - width) / 2; } } else { personList.Sort((x, y) => x.Score.CompareTo(y.Score)); //排序后取中间人脸的坐标计算 var noseX = personList[personList.Count - 1].NoseX; //左边距离够 if (noseX - width / 2 >= 0) { if (noseX - width / 2 + width <= baseImg.Width) { left = (int)(noseX - width / 2); } else { left = baseImg.Width - width; } } else { left = 0; } } } using (Graphics g = Graphics.FromImage(tempImage)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.White); g.DrawImage(baseImg, new Rectangle(0, 0, tempImage.Width, tempImage.Height), new Rectangle(left, 0, width, baseImg.Height), GraphicsUnit.Pixel); tempImage.Save(coverPath, ImageFormat.Png); } } catch (Exception e) { throw e; } finally { baseImg.Dispose(); tempImage.Dispose(); } }
public ActionResult RemoveBackground(HttpPostedFileBase file) { var client = new Baidu.Aip.BodyAnalysis.Body(apiKey, secretKey); var options = new Dictionary <string, object> { { "type", "foreground" } }; var result = client.BodySeg(file.InputStream.ToBytes(), options); var imageBase64 = result["foreground"].ToString(); byte[] imageBytes = imageBase64.Base64StrToBuffer(); //剪切背景图 MemoryStream imageBkStream = new MemoryStream(); imageBkStream.Write(imageBytes, 0, imageBytes.Length); Bitmap img = new Bitmap(imageBkStream); Dictionary <int, List <int> > imgs = new Dictionary <int, List <int> >(); for (var x = 0; x < img.Width; x++) { for (var y = 0; y < img.Height; y++) { var p = img.GetPixel(x, y); if (p.A > 0) { if (imgs.ContainsKey(x)) { imgs[x].Add(y); } else { imgs.Add(x, new List <int>()); } } } } int minX = img.Width, minY = img.Height; int maxX = 0, maxY = 0; foreach (var kv in imgs) { if (kv.Key < minX) { minX = kv.Key; } if (kv.Key > maxX) { maxX = kv.Key; } if (kv.Value.Min() < minY) { minY = kv.Value.Min(); } if (kv.Value.Max() > maxY) { maxY = kv.Value.Max(); } } int realWidth = maxX - minX; int realHeight = maxY - minY; Stream newImageBkStream = ImageExtention.GenerateThumbnail(imageBkStream, ImageModelEnum.cut, minX, minY, ref realWidth, ref realHeight); //保存磁盘 string newpath = AppDomain.CurrentDomain.BaseDirectory + "images\\process_photo\\" + DateTime.Now.ToString("yyyyMMdd") + "\\"; string fileName = ObjectId.GenerateNewId().ToString() + ".png"; if (!Directory.Exists(newpath)) { Directory.CreateDirectory(newpath); } using (FileStream fileStream = new FileStream(newpath + fileName, FileMode.Create, FileAccess.Write)) { newImageBkStream.CopyTo(fileStream); } return(Json(new { path = "process_photo-" + DateTime.Now.ToString("yyyyMMdd") + "-" + fileName, width = realWidth, height = realHeight })); }
public Form1() { InitializeComponent(); client = new Baidu.Aip.BodyAnalysis.Body(APIKEY, SECRETKEY); client.Timeout = 60000; // 修改超时时间 }