C# CustomVision 调用API

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

        private CustomVisionInfo TagModel;

        private ImageTagModel createTagModel;
        private IterationModel iterationModel;
        private TrainingApi trainingApi;
        // private List 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() { 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() { TagModel.Id.ToString() });

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

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

        }
        //找到对应标签的图片
       public  List ImgUrl(string TagName,string cvName)
        {
            dynamic lst = null;
            List _Lst = new List();
            //初始化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() { 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() { _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 _lst = new List();
           
                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();
                        // 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() { TagModel.Id.ToString() }, null, 250, 0);
                            if (lst.Count()>0)
                            {
                                foreach (var item in lst)
                                {
                                    trainingApi.DeleteImages(projectModel.Id, new List() { 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() { item });
                        }
                    }
                    else
                    {
                        trainingApi.DeleteImages(projectModel.Id, new List() { 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 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 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;
            }
        }

    
    }

你可能感兴趣的:(microsoft,AI,C#,CustomVision)