예제 #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();

            file.InitialDirectory = ".";
            file.Filter           = "所有文件(*.*)|*.*";
            file.ShowDialog();

            filepath1 = file.FileName;   //获得文件的绝对路径
            if (filepath1 == null || filepath1 == "")
            {
                return;
            }
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox1.Image    = Image.FromFile(filepath1);
            var      image          = File.ReadAllBytes(filepath1);
            var      result         = client.BodyAnalysis(image);
            var      person_pix_arr = (JArray)result["person_info"];
            var      bmp            = new Bitmap(pictureBox1.Image);
            Graphics g = Graphics.FromImage(bmp);//创建一个画板

            foreach (JObject person_pix in person_pix_arr)
            {
                foreach (var item in (JObject)person_pix["body_parts"])
                {
                    var x = item.Value["x"];
                    var y = item.Value["y"];
                    g.DrawEllipse(new Pen(Color.Red, 5), Convert.ToInt32(x), Convert.ToInt32(y), 4, 4);
                }
            }
            pictureBox1.Image = bmp;
        }
예제 #2
0
        public async Task <string> BodyDetect(string filePath)
        {
            return(await Task.Run(() =>
            {
                try
                {
                    var image = File.ReadAllBytes(filePath);
                    // 调用通用文字识别, 图片参数为本地图片,可能会抛出网络等异常,请使用try/catch捕获

                    var result = _client.BodyAnalysis(image);
                    return result.ToString();
                }
                catch (Exception e)
                {
                    _logger.LogError(null, e);
                    throw;
                }
            }));
        }
예제 #3
0
        /// <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();
            }
        }