コード例 #1
0
ファイル: COCODataSetting.cs プロジェクト: ccc030233/oeip
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            COCOItem selectItem = this.comboBox1.SelectedItem as COCOItem;

            selectData = null;
            if (selectItem.index == 0)
            {
                selectData = setting.Data.TrainData;
            }
            else if (selectItem.index == 1)
            {
                selectData = setting.Data.PredData;
            }
            if (selectData == null)
            {
                return;
            }
            this.textBox1.Text = selectData.AnnotationPath;
            this.textBox2.Text = selectData.SourceImagePath;
            this.textBox3.Text = selectData.LabelPath;
            this.textBox4.Text = selectData.DestImagePath;
        }
コード例 #2
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("全部完成");
        }