AI: MS AI人脸检测和识别之前,先了解 customvision.ai

AI: MS AI人脸识别和之前先了解 customvision.ai

Customvision 地址 https://www.customvision.ai/,我是订阅用户,登录后可以创建自己图像库。她提供了一个环境,可以让你轻松定制自己的计算机视觉模型,有使用向导和模板。提供API 让你可以在任何环境使用你创建的视觉模型。

至于介绍和用途,官网和搜索引擎都说的太多了,我就不赘述了。直接上例子。

如下图:最近的课程仅仅使用第一个,今天先介绍第一个功能。


  • 使用Customvision 获得图像

登录之后直接到Projects,URL是。

https://www.customvision.ai/projects

创建一个Project,下图的选项是在我实验里用到的。


在下面出来的对话框里直接单添加图片--浏览本地图,我在使用中用到2个部分一个萝卜和苹果,我分开上传以便教会系统如何做识别。


首先上传的萝卜,选择了9张不同的图片,标签名字是Carrot。然后一直下一步上传成功。


按照这个方法,继续上传苹果的照片,以便让程序学会区分。


上传成功之后,就可以使用这些图片来训练图像分类模型了。当然如果学习更准确的话,需要更多的图片。


  • Train(训练)

开始在Customvision 提供的训练模型,训练这些图像。

在上图里上侧有个Train 按钮单击即可开始图片的训练,执行结果如下,


上图:Precision 是精度。Recall 是回收的。全是100%继续下一步,

  • API调用

训练完成以后,Prediction Url,可以看到使用训练结果的API URL


之后回到程序继续进行图像的检测与识别。

***************************************************分割线***********************************************************

下面是一个代码调用示例,

 public class CustomVisionService
    { 
        //CV
        private FileStream testFileStream;
        
        public CustomVisionInfo projectModel;

        private CustomVisionInfo TagModel;

        private ImageTagModel createTagModel;
        private IterationModel iterationModel;
        private TrainingApi trainingApi;
        // private List<IterationModel> IterationLst; //所有迭代
        //配置服务
        ISystemSettingService systemSettingService = new SystemSettingService();
        CreateImageSummaryModel model;
        public   void Initialise( string TagName, string cvName)
        {
            string trainingKey = systemSettingService.GetValueInCache("CustomVisionTrainingKey");

            TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey);
            trainingApi = new TrainingApi(trainingCredentials);

            // Upload the images we need for training and the test image


            //找到对应key下的所有项目
            var projects = trainingApi.GetProjects();

            projectModel = QueryKnowledge(projects, cvName);// "ChimeLongAnimal"
            if (projectModel.Id.ToString().Length > 0)
            {
                try
                {

                   
                    //找到对应项目
                    //var project = trainingApi.GetProject(projectModel.Id);


                    //找对当前项目下的所有标签
                    var allTags = trainingApi.GetTags(projectModel.Id);

                    ////对应的标签
                    TagModel = QueryKnowledge(allTags.Tags, TagName);
                    //找到需要新增的对应的标签,是否已经存在
                    if (TagModel == null)
                    {
                        //设置图片标签
                        createTagModel = trainingApi.CreateTag(projectModel.Id, TagName);
                        TagModel.Id = createTagModel.Id;
                        TagModel.Name = createTagModel.Name;
                     }
                }
                catch (Exception ex)
                {
                    LogHelper.Error("CustomVisionService_Initialise", LogSources.AdminPortal, ex);

                }
            }
        }
        public  void   UpdateTag(string oldTagName, string TagName, string cvName)
        {
           
            try
            {
                string trainingKey = systemSettingService.GetValueInCache("CustomVisionTrainingKey");

                TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey);
                trainingApi = new TrainingApi(trainingCredentials);

                // Upload the images we need for training and the test image


                //找到对应key下的所有项目
                var projects = trainingApi.GetProjects();

                projectModel = QueryKnowledge(projects, cvName);// "ChimeLongAnimal"
                if (projectModel.Id.ToString().Length > 0)
                {
                   
                        //找到对应项目
                        //var project = trainingApi.GetProject(projectModel.Id);


                        //找对当前项目下的所有标签
                        var allTags = trainingApi.GetTags(projectModel.Id);

                 
                   ////对应的标签
                   TagModel = QueryKnowledge(allTags.Tags, oldTagName);
                        //找到需要新增的对应的标签,是否已经存在
                        if (TagModel != null)
                        {
                        ImageTagModel _model= trainingApi.GetTag(projectModel.Id, TagModel.Id);
                        _model.Name = TagName;
                        //设置图片标签
                        trainingApi.UpdateTag(projectModel.Id, TagModel.Id, _model);
                       
                        }
                }

            }
            catch (Exception ex)
            {

                LogHelper.Error("CustomVisionService_UpdateTag", LogSources.AdminPortal, ex);
            }
        }

        //上传图片到CV 训练图片
        public  void SaveImg(string path,string TagName,string cvName)
        {
            //初始化CV 对应项目与标签
            Initialise(TagName, cvName);
            try {

                testFileStream = File.Open(path, FileMode.Open, FileAccess.Read);

                #region  上传图片
                model = trainingApi.CreateImagesFromData(projectModel.Id
               , testFileStream, new List<String>() { TagModel.Id.ToString() });
                #endregion
                testFileStream.Dispose();
                testFileStream.Close();
            

               


            }
            catch (Exception ex)
            {
                LogHelper.Error("CustomVisionService_SaveImg-保存失败", LogSources.AdminPortal, ex);


            }


        }
       
        //检查当前训练库 标签两个 同时每个标签至少有5张图
        public bool CheckTagAndImg(string cvName)
        {
            bool falg = false;
            string trainingKey = systemSettingService.GetValueInCache("CustomVisionTrainingKey");

            TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey);
            trainingApi = new TrainingApi(trainingCredentials);
            


            //找到对应key下的所有项目
            var projects = trainingApi.GetProjects();

            projectModel = QueryKnowledge(projects, cvName);// "ChimeLongAnimal"
            if (projectModel.Id.ToString().Length > 0)
            {
                var lst = trainingApi.GetTags(projectModel.Id);
                if (lst.Tags.Count() > 2)
                {
                    foreach (var item in lst.Tags)
                    {
                        if (item.ImageCount < 6)
                        {
                            if (item.ImageCount==5)
                            {

                            }
                            else
                            {
                                falg = true;
                                break;
                            }
                            
                        }
                    }
                }
                else
                {
                    falg = true;
                }
            }
            return falg;
        }

        public dynamic TraringAnimal(string cvName)
        {
            string trainingKey = systemSettingService.GetValueInCache("CustomVisionTrainingKey");
            dynamic model = null;
            TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey);
            trainingApi = new TrainingApi(trainingCredentials);

            // Upload the images we need for training and the test image


            //找到对应key下的所有项目
            var projects = trainingApi.GetProjects();

            projectModel = QueryKnowledge(projects, cvName);// "ChimeLongAnimal"
            if (projectModel.Id.ToString().Length > 0)
            {
                var iterations = trainingApi.GetIterations(projectModel.Id).OrderBy(x => x.Created);
                //判断当前迭代是否等于20
                if (iterations.Count() >= 20)
                {
                    var iteration = iterations.First();
                    trainingApi.DeleteIteration(projectModel.Id, iteration.Id);
                }

                // var data =trainingApi.TrainProject(projectModel.Id);

                //训练
                try
                {

                    var data = MakePredictionRequestByURL(projectModel.Id);
                }
                catch (Exception ex)
                {

                    model = 1; 
                }

                //10秒
                System.Threading.Thread.Sleep(150000);

                iterationModel = iterations.Last();

                ////设置成默认
                iterationModel.IsDefault = true;
                try
                {
                   // model=UpdateIteration(projectModel.Id, iterationModel);
                    model = trainingApi.UpdateIteration(projectModel.Id, iterationModel.Id, iterationModel);
                }
                catch (Exception ex)
                {
                   // model = trainingApi.UpdateIteration(projectModel.Id, iterationModel.Id, iterationModel);
                    model = 0;
                }

            }
            return model;
        }


         public bool CheckAnialName(string TagName, string cvName)
        {
            bool flag = false;
            string trainingKey = systemSettingService.GetValueInCache("CustomVisionTrainingKey");
            TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey);
            trainingApi = new TrainingApi(trainingCredentials);

           
            //找到对应key下的所有项目
            var projects = trainingApi.GetProjects();

            projectModel = QueryKnowledge(projects, cvName);// "ChimeLongAnimal"
            if (projectModel.Id.ToString().Length > 0)
            {
                //找对当前项目下的所有标签
                var allTags = trainingApi.GetTags(projectModel.Id);

                ////对应的标签
                TagModel = QueryKnowledge(allTags.Tags, TagName);
                //找到需要新增的对应的标签,是否已经存在
                if (TagModel == null)
                {
                    flag = true;
                }
             }

            return flag;
         }
        public void SaveAnimalImg(string path,string TagName,string cvName,string url)
        {
            if (path.Length>0)
            {
                try
                {
                    //初始化CV 对应项目与标签
                    Initialise(TagName, cvName);

                    // 设置参数
                    HttpWebRequest request = WebRequest.Create(path) as HttpWebRequest;
                    //发送请求并获取相应回应数据
                    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                    //直到request.GetResponse()程序才开始向目标网页发送Post请求
                    Stream responseStream = response.GetResponseStream();
                    
                    //创建本地文件写入流
                    Stream stream = new FileStream(url, FileMode.Create,FileAccess.ReadWrite);
                    byte[] bArr = new byte[1024];
                    int size = responseStream.Read(bArr, 0, (int)bArr.Length);
                    while (size > 0)
                    {
                        stream.Write(bArr, 0, size);
                        size = responseStream.Read(bArr, 0, (int)bArr.Length);
                    }
                    stream.Close();
                    responseStream.Close();

                    testFileStream = File.Open(url, FileMode.Open, FileAccess.Read);//base64转为byte数组
                   

                    model = trainingApi.CreateImagesFromData(projectModel.Id
              , testFileStream, new List<String>() { TagModel.Id.ToString() });

                    testFileStream.Dispose();
                    testFileStream.Close();
                    
        
                }
                catch (Exception ex)
                {

                    LogHelper.Error("CustomVisionService_SaveAnimalImg", LogSources.AdminPortal, ex);
                }
                
            }
            

        }
        //找到对应标签的图片
       public  List<dynamic> ImgUrl(string TagName,string cvName)
        {
            dynamic lst = null;
            List<dynamic> _Lst = new List<dynamic>();
            //初始化CV 对应项目与标签
           
                string trainingKey = systemSettingService.GetValueInCache("CustomVisionTrainingKey");

            TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey);
                trainingApi = new TrainingApi(trainingCredentials);

                // Upload the images we need for training and the test image


                //找到对应key下的所有项目
                var projects = trainingApi.GetProjects();

                projectModel = QueryKnowledge(projects, cvName);// "ChimeLongAnimal"
            if (projectModel.Id.ToString().Length > 0)
            {
                try
                {
                    if (TagName.Length > 0)
                    {

                        //找到需要新增的对应的标签,是否已经存在
                        if (!string.IsNullOrEmpty(TagName))
                        {
                            if (TagName.IndexOf(";") > -1)
                            {
                                string[] arr = TagName.Split(';');
                                foreach (var item in arr)
                                {

                                    lst = trainingApi.GetImagesByTags(projectModel.Id, null, new List<String>() { item }, null, 250, 0);
                                    _Lst.AddRange(lst);

                                }
                            }
                            else
                            {
                                Guid gv = new Guid();
                                gv = new Guid(TagName);
                                //找对当前项目下的标签
                                var _tagModel = trainingApi.GetTag(projectModel.Id, gv);
                                if (_tagModel != null)
                                {

                                    lst = trainingApi.GetImagesByTags(projectModel.Id, null, new List<String>() { _tagModel.Id.ToString() }, null, 250, 0);
                                    _Lst.AddRange(lst);
                                }

                            }
                        }

                    }
                    else {
                        try
                        {
                            lst = trainingApi.GetAllTaggedImages(projectModel.Id, null, null, 250, 0);
                            _Lst.AddRange(lst);
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error("CustomVisionService_Initialise", LogSources.AdminPortal, ex);

                        }


                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error("CustomVisionService_Initialise", LogSources.AdminPortal, ex);

                }



            }
            
            
            return _Lst;
        }
      
       
        //获取标签名称
        public dynamic AnimalTags(string TagName, string cvName)
        {
            List<dynamic> _lst = new List<dynamic>();
           
                string trainingKey = systemSettingService.GetValueInCache("CustomVisionTrainingKey");

                TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey);
                trainingApi = new TrainingApi(trainingCredentials);

                // Upload the images we need for training and the test image


                //找到对应key下的所有项目
                var projects = trainingApi.GetProjects();

                projectModel = QueryKnowledge(projects, cvName);// "ChimeLongAnimal"

               
                if (projectModel.Id.ToString().Length > 0)
                {
                var allTags = trainingApi.GetTags(projectModel.Id);
                if (string.IsNullOrEmpty(TagName))
                    {

                        //找对当前项目下的所有标签
                       

                            //为空时拿所有
                            return allTags;
                            //////对应的标签
                            //TagModel = QueryKnowledge(allTags.Tags, TagName);
                       
                    }
                        else
                        {

                        if (TagName.IndexOf(';') > -1)
                        {
                            //找到对应标签
                                    if (allTags.Tags.Count() > 0)
                                    {
                                        foreach (var item in allTags.Tags)
                                        {
                                            if (item.Name.Contains(TagName.Trim()))
                                            {
                                                _lst.Add(item);
                                            }
                                        }
                                    }

                    }
                    else
                    {
                      //  TagModel = QueryKnowledgeTag(allTags.Tags, TagName);
                        if (allTags.Tags.Count() > 0)
                        {
                            foreach (var item in allTags.Tags)
                            {
                                if (item.Name.Contains(TagName.Trim()))
                                {
                                    _lst.Add(item);
                                }
                            }
                        }
                        //  dynamic _data = allTags;
                        //  _data.Tags= allTags.Tags.Where(x => x.Name == TagName).ToList()??new List<ImageTagModel>();
                        // dynamic data = allTags.Tags.Where(x => x.Name == TagName).ToList();
                        //_lst.Add(TagModel);

                    }
                }

            }
            
            return _lst;

        }

        //添加标签
        public void AddTag(string TagName, string cvName)
        {
            if (TagName.Length > 0)
            {
                string trainingKey = systemSettingService.GetValueInCache("CustomVisionTrainingKey");

                TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey);
            trainingApi = new TrainingApi(trainingCredentials);

            // Upload the images we need for training and the test image


            //找到对应key下的所有项目
            var projects = trainingApi.GetProjects();

            projectModel = QueryKnowledge(projects, cvName);// "ChimeLongAnimal"
            if (projectModel.Id.ToString().Length > 0)
            {
                try
                {


                    //找到对应项目
                    //var project = trainingApi.GetProject(projectModel.Id);


                    //找对当前项目下的所有标签
                    var allTags = trainingApi.GetTags(projectModel.Id);

                    ////对应的标签
                    TagModel = QueryKnowledge(allTags.Tags, TagName);
                    //找到需要新增的对应的标签,是否已经存在
                    if (TagModel == null)
                    {
                            if (TagName.IndexOf(';') > 0)
                            {
                                string[] arr = TagName.Split(';');
                                foreach (var item in arr)
                                {
                                    trainingApi.CreateTag(projectModel.Id, item);
                                }
                            }
                            else
                            {
                              var data=trainingApi.CreateTag(projectModel.Id, TagName);
                                
                            }
                         
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error("CustomVisionService_Initialise", LogSources.AdminPortal, ex);

                }
            }
           
               
             
              
            }
           
        }
        //删除标签
        public void DeleteTag(string str, string cvName)
        {
           

           if (str.Length > 0)
            {
                string trainingKey = systemSettingService.GetValueInCache("CustomVisionTrainingKey");

                TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey);
                trainingApi = new TrainingApi(trainingCredentials);

                // Upload the images we need for training and the test image


                //找到对应key下的所有项目
                var projects = trainingApi.GetProjects();

                projectModel = QueryKnowledge(projects, cvName);// "ChimeLongAnimal"
                if (projectModel.Id.ToString().Length > 0)
                {
                    if (str.IndexOf(';') > 0)
                    {
                        string[] arr = str.Split(';');
                        foreach (var item in arr)
                        {
                            trainingApi.DeleteTag(projectModel.Id, new Guid(item));
                        }
                    }
                    else
                    {
                        //找对当前项目下的所有标签
                        var allTags = trainingApi.GetTags(projectModel.Id);

                        ////对应的标签
                        TagModel = QueryKnowledge(allTags.Tags, str);
                        if (TagModel!=null)
                        {
                           
                            //删除对应的照片
                            var lst = trainingApi.GetImagesByTags(projectModel.Id, null, new List<String>() { TagModel.Id.ToString() }, null, 250, 0);
                            if (lst.Count()>0)
                            {
                                foreach (var item in lst)
                                {
                                    trainingApi.DeleteImages(projectModel.Id, new List<String>() { item.Id.ToString() });
                                }

                            }

                            trainingApi.DeleteTag(projectModel.Id, TagModel.Id);


                        }

                    }
                }
               

            }
        }
        //删除对应图片
        public void DeleteImg(string str,string cvName)
        {
            load(cvName);
            if (projectModel.Id.ToString().Length > 0)
            {
                if (str.Length > 0)
                {
                    if (str.IndexOf(';') > 0)
                    {
                        string[] arr = str.Split(';');
                        foreach (var item in arr)
                        {
                            trainingApi.DeleteImages(projectModel.Id, new List<String>() { item });
                        }
                    }
                    else
                    {
                        trainingApi.DeleteImages(projectModel.Id, new List<String>() { str });
                    }

                }
            }
           
        }

        public void load(string cvName)
        {
            string trainingKey = systemSettingService.GetValueInCache("CustomVisionTrainingKey");

            TrainingApiCredentials trainingCredentials = new TrainingApiCredentials(trainingKey);
            trainingApi = new TrainingApi(trainingCredentials);

            // Upload the images we need for training and the test image


            //找到对应key下的所有项目
            var projects = trainingApi.GetProjects();

            projectModel = QueryKnowledge(projects, cvName);// "ChimeLongAnimal"
            
        }
        //找对应的cv图库和对应的标签
        private static CustomVisionInfo QueryKnowledge(dynamic lst, string Name)
        {
            CustomVisionInfo model = null;
            if (lst.Count > 0)
            {
                foreach (var item in lst)
                {
                    if (item.Name == Name)
                    {
                        model = new CustomVisionInfo
                        {
                            Id = item.Id,
                            Name = item.Name
                        };

                    }
                }
            }
            return model;
        }

        private static CustomVisionInfo QueryKnowledgeTag(dynamic lst, string Name)
        {
            CustomVisionInfo model = null;
            if (lst.Count > 0)
            {
                foreach (var item in lst)
                {
                    if (item.Name.Contains(Name))
                    {
                        model = new CustomVisionInfo
                        {
                            Id = item.Id,
                            Name = item.Name,
                            ImageCount=item.ImageCount
                        };

                    }
                }
            }
            return model;
        }

        private async Task<string> MakePredictionRequestByURL(Guid projectid)
        {
            
            string cvKey = systemSettingService.GetValueInCache("CustomVisionTrainingKey");
            string cvAPI = "https://southcentralus.api.cognitive.microsoft.com/customvision/v1.0/Training/projects/" + projectid + "/train";

            var queryString = "{body}";
            HttpResponseMessage response;

            byte[] byteData = Encoding.UTF8.GetBytes(queryString);
            WebClient client = new WebClient();
            client.Headers.Add("Training-key", cvKey);

            try
            {
                using (var content = new ByteArrayContent(byteData))
                {
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    
                    byte[] resultByte = client.UploadData(cvAPI, byteData);
                    
                    string result = Encoding.UTF8.GetString(resultByte);

                    return result;
                }
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }

        private async Task<string> UpdateIteration(Guid projectid, IterationModel iterationModel)
        {

            string cvKey = systemSettingService.GetValueInCache("CustomVisionTrainingKey");
            string cvAPI = "https://southcentralus.api.cognitive.microsoft.com/customvision/v1.0/Training/projects/"+ projectid +"/iterations/"+ iterationModel.Id;

            var queryString = Newtonsoft.Json.JsonConvert.SerializeObject(iterationModel);
            HttpResponseMessage response;

            byte[] byteData = Encoding.UTF8.GetBytes(queryString);
            WebClient client = new WebClient();
            client.Headers.Add("Training-key", cvKey);

            try
            {
                using (var content = new ByteArrayContent(byteData))
                {
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    byte[] resultByte = client.UploadData(cvAPI, byteData);

                    string result = Encoding.UTF8.GetString(resultByte);

                    return result;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

    
    }







猜你喜欢

转载自blog.csdn.net/jason_dct/article/details/80711620
AI