Exemplo n.º 1
0
        public instances LoadInstance(string path)
        {
            instances instance = new instances();

            if (File.Exists(path))
            {
                var jsonTex = File.ReadAllText(path);
                instance = JsonConvert.DeserializeObject <instances>(jsonTex);
            }
            return(instance);
        }
Exemplo n.º 2
0
        public async Task <instances> LoadInstanceAsync(string path)
        {
            instances instance = new instances();

            if (File.Exists(path))
            {
                var jsonTex = await Task.FromResult(File.ReadAllText(path));

                instance = await Task.FromResult(JsonConvert.DeserializeObject <instances>(jsonTex));
            }
            return(instance);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 数据经过funcFilterLabel过滤,过滤后的数据需要全部满足discardFilterLabel
        /// </summary>
        /// <param name="instData"></param>
        /// <param name="funcFilterLabel">满足条件就采用</param>
        /// <param name="discardFilterLabel">需要所有标签满足的条件</param>
        /// <returns></returns>
        public List <ImageLabel> CreateYoloLabel(instances instData,
                                                 Func <annotationOD, image, bool> funcFilterLabel,
                                                 Func <annotationOD, image, bool> discardFilterLabel)
        {
            List <ImageLabel> labels = new List <ImageLabel>();

            //foreach (var image in instData.images)
            Parallel.ForEach(instData.images, (image image) =>
            {
                var anns       = instData.annotations.FindAll(p => p.image_id == image.id && funcFilterLabel(p, image));
                bool bReserved = anns.TrueForAll((annotationOD ao) => discardFilterLabel(ao, image));
                if (anns.Count > 0 && bReserved)
                {
                    ImageLabel iml = new ImageLabel();
                    iml.imageId    = image.id;
                    iml.name       = image.file_name;
                    float dw       = 1.0f / image.width;
                    float dh       = 1.0f / image.height;
                    foreach (var ann in anns)
                    {
                        BoxIndex boxIndex    = new BoxIndex();
                        boxIndex.box.xcenter = (ann.bbox[0] + ann.bbox[2] / 2.0f) * dw;
                        boxIndex.box.ycenter = (ann.bbox[1] + ann.bbox[3] / 2.0f) * dh;

                        boxIndex.box.width  = ann.bbox[2] * dw;
                        boxIndex.box.height = ann.bbox[3] * dh;
                        //注册
                        boxIndex.catId = findCategoryId(instData.categories, ann.category_id);
                        if (boxIndex.catId >= 0)
                        {
                            iml.boxs.Add(boxIndex);
                        }
                    }
                    if (iml.boxs.Count > 0)
                    {
                        lock (labels)
                        {
                            labels.Add(iml);
                        }
                    }
                }
            });
            return(labels);
        }
Exemplo n.º 4
0
        public async void BuildYoloData(DataPath dataPath, string txtListName)
        {
            instances instance = new instances();

            if (!File.Exists(dataPath.AnnotationPath))
            {
                setText(dataPath.AnnotationPath + " 路径不存在.");
                return;
            }
            setText("正在读取文件中:" + Environment.NewLine + dataPath.AnnotationPath);
            var jsonTex = await Task.FromResult(File.ReadAllText(dataPath.AnnotationPath));

            setText("正在解析文件中:" + Environment.NewLine + dataPath.AnnotationPath);
            instance = await Task.FromResult(JsonConvert.DeserializeObject <instances>(jsonTex));

            setText("正在分析文件包含人物图像:" + instance.images.Count + "个");
            List <ImageLabel> labels = await Task.FromResult(COCODataManager.Instance.CreateYoloLabel(
                                                                 instance,
                                                                 (annotationOD at, image image) =>
            {
                //是否人类
                return(at.category_id == 1);
            },
                                                                 (annotationOD at, image image) =>
            {
                //是否满足所有人类标签都面积占比都大于十分之一
                return((at.bbox[2] / image.width) * (at.bbox[3] / image.height) > 0.1f);
            }));

            setText("正在生成label文件:" + Environment.NewLine + dataPath.LabelPath);
            if (!Directory.Exists(dataPath.LabelPath))
            {
                Directory.CreateDirectory(dataPath.LabelPath);
            }
            await Task.Run(() =>
            {
                Parallel.ForEach(labels, (ImageLabel imageLabel) =>
                {
                    string fileName = Path.Combine(dataPath.LabelPath,
                                                   Path.GetFileNameWithoutExtension(imageLabel.name) + ".txt");
                    using (var file = new StreamWriter(Path.Combine(dataPath.LabelPath, fileName), false))
                    {
                        foreach (var label in imageLabel.boxs)
                        {
                            file.WriteLine(label.catId + " " + label.box.xcenter + " " + label.box.ycenter +
                                           " " + label.box.width + " " + label.box.height + " ");
                        }
                    }
                });
                string path = Path.Combine(Directory.GetParent(dataPath.LabelPath).FullName,
                                           txtListName + ".txt");
                using (var file = new StreamWriter(path, false))
                {
                    foreach (var label in labels)
                    {
                        string lpath = Path.Combine(dataPath.DestImagePath, label.name);
                        file.WriteLine(lpath);
                    }
                }
            });

            setText("正在复制需要的文件到指定目录:" + dataPath.AnnotationPath);
            await Task.Run(() =>
            {
                Parallel.ForEach(labels, (ImageLabel imageLabel) =>
                {
                    string spath = Path.Combine(dataPath.SourceImagePath, imageLabel.name);
                    string dpsth = Path.Combine(dataPath.DestImagePath, imageLabel.name);
                    if (File.Exists(spath))
                    {
                        File.Copy(spath, dpsth, true);
                    }
                });
            });

            setText("全部完成");
        }