Пример #1
0
        // PascalVoc Format으로 저장
        public static void SaveAsIAEFormatFromPascalVocFormat(string fullFileName, ref PascalVocFormat pascalVocData)
        {
            // xml 정보 생성
            XmlDocument xmlDoc = new XmlDocument();

            // node 정의
            XmlNode rootNode = xmlDoc.CreateElement("annotation");

            xmlDoc.AppendChild(rootNode);

            XmlNode basicDrawingInformationNpde = xmlDoc.CreateElement("basic_drawing_information");

            rootNode.AppendChild(basicDrawingInformationNpde);

            XmlNode folderNode = xmlDoc.CreateElement("folder");

            basicDrawingInformationNpde.AppendChild(folderNode);

            XmlNode filenameNode = xmlDoc.CreateElement("filename");

            basicDrawingInformationNpde.AppendChild(filenameNode);

            XmlNode pathNode = xmlDoc.CreateElement("path");

            basicDrawingInformationNpde.AppendChild(pathNode);

            XmlNode sizeNode = xmlDoc.CreateElement("size");

            basicDrawingInformationNpde.AppendChild(sizeNode);

            XmlNode widthNode = xmlDoc.CreateElement("width");

            sizeNode.AppendChild(widthNode);

            XmlNode heightNode = xmlDoc.CreateElement("height");

            sizeNode.AppendChild(heightNode);

            XmlNode depthNode = xmlDoc.CreateElement("depth");

            sizeNode.AppendChild(depthNode);

            XmlNode externalBorderLineNode           = xmlDoc.CreateElement("external_border_line");
            XmlNode externalBorderLineBndBoxNode     = xmlDoc.CreateElement("bndbox");
            XmlNode externalBorderLineBndBoxXminNode = xmlDoc.CreateElement("xmin");
            XmlNode externalBorderLineBndBoxYminNode = xmlDoc.CreateElement("ymin");
            XmlNode externalBorderLineBndBoxXmaxNode = xmlDoc.CreateElement("xmax");
            XmlNode externalBorderLineBndBoxYmaxNode = xmlDoc.CreateElement("ymax");

            basicDrawingInformationNpde.AppendChild(externalBorderLineNode);
            externalBorderLineNode.AppendChild(externalBorderLineBndBoxNode);
            externalBorderLineBndBoxNode.AppendChild(externalBorderLineBndBoxXminNode);
            externalBorderLineBndBoxNode.AppendChild(externalBorderLineBndBoxYminNode);
            externalBorderLineBndBoxNode.AppendChild(externalBorderLineBndBoxXmaxNode);
            externalBorderLineBndBoxNode.AppendChild(externalBorderLineBndBoxYmaxNode);

            XmlNode pureDrawingAreaNode               = xmlDoc.CreateElement("pure_drawing_area");
            XmlNode pureDrawingAreaNodeBndBoxNode     = xmlDoc.CreateElement("bndbox");
            XmlNode pureDrawingAreaNodeBndBoxXminNode = xmlDoc.CreateElement("xmin");
            XmlNode pureDrawingAreaNodeBndBoxYminNode = xmlDoc.CreateElement("ymin");
            XmlNode pureDrawingAreaNodeBndBoxXmaxNode = xmlDoc.CreateElement("xmax");
            XmlNode pureDrawingAreaNodeBndBoxYmaxNode = xmlDoc.CreateElement("ymax");

            basicDrawingInformationNpde.AppendChild(pureDrawingAreaNode);
            pureDrawingAreaNode.AppendChild(pureDrawingAreaNodeBndBoxNode);
            pureDrawingAreaNodeBndBoxNode.AppendChild(pureDrawingAreaNodeBndBoxXminNode);
            pureDrawingAreaNodeBndBoxNode.AppendChild(pureDrawingAreaNodeBndBoxYminNode);
            pureDrawingAreaNodeBndBoxNode.AppendChild(pureDrawingAreaNodeBndBoxXmaxNode);
            pureDrawingAreaNodeBndBoxNode.AppendChild(pureDrawingAreaNodeBndBoxYmaxNode);

            XmlNode noteAreaNode               = xmlDoc.CreateElement("note_area");
            XmlNode noteAreaNodeBndBoxNode     = xmlDoc.CreateElement("bndbox");
            XmlNode noteAreaNodeBndBoxXminNode = xmlDoc.CreateElement("xmin");
            XmlNode noteAreaNodeBndBoxYminNode = xmlDoc.CreateElement("ymin");
            XmlNode noteAreaNodeBndBoxXmaxNode = xmlDoc.CreateElement("xmax");
            XmlNode noteAreaNodeBndBoxYmaxNode = xmlDoc.CreateElement("ymax");

            basicDrawingInformationNpde.AppendChild(noteAreaNode);
            noteAreaNode.AppendChild(noteAreaNodeBndBoxNode);
            noteAreaNodeBndBoxNode.AppendChild(noteAreaNodeBndBoxXminNode);
            noteAreaNodeBndBoxNode.AppendChild(noteAreaNodeBndBoxYminNode);
            noteAreaNodeBndBoxNode.AppendChild(noteAreaNodeBndBoxXmaxNode);
            noteAreaNodeBndBoxNode.AppendChild(noteAreaNodeBndBoxYmaxNode);

            XmlNode titleAreaNode               = xmlDoc.CreateElement("title_area");
            XmlNode titleAreaNodeBndBoxNode     = xmlDoc.CreateElement("bndbox");
            XmlNode titleAreaNodeBndBoxXminNode = xmlDoc.CreateElement("xmin");
            XmlNode titleAreaNodeBndBoxYminNode = xmlDoc.CreateElement("ymin");
            XmlNode titleAreaNodeBndBoxXmaxNode = xmlDoc.CreateElement("xmax");
            XmlNode titleAreaNodeBndBoxYmaxNode = xmlDoc.CreateElement("ymax");

            basicDrawingInformationNpde.AppendChild(titleAreaNode);
            titleAreaNode.AppendChild(titleAreaNodeBndBoxNode);
            titleAreaNodeBndBoxNode.AppendChild(titleAreaNodeBndBoxXminNode);
            titleAreaNodeBndBoxNode.AppendChild(titleAreaNodeBndBoxYminNode);
            titleAreaNodeBndBoxNode.AppendChild(titleAreaNodeBndBoxXmaxNode);
            titleAreaNodeBndBoxNode.AppendChild(titleAreaNodeBndBoxYmaxNode);

            XmlNode drawingAreaSeperatorNode           = xmlDoc.CreateElement("drawing_area_separator");
            XmlNode drawingAreaSeperatorEdgeNode       = xmlDoc.CreateElement("edge");
            XmlNode drawingAreaSeperatorEdgeXstartNode = xmlDoc.CreateElement("xstart");
            XmlNode drawingAreaSeperatorEdgeYstartNode = xmlDoc.CreateElement("ystart");
            XmlNode drawingAreaSeperatorEdgeXendNode   = xmlDoc.CreateElement("xend");
            XmlNode drawingAreaSeperatorEdgeYendNode   = xmlDoc.CreateElement("yend");

            basicDrawingInformationNpde.AppendChild(drawingAreaSeperatorNode);
            drawingAreaSeperatorNode.AppendChild(drawingAreaSeperatorEdgeNode);
            drawingAreaSeperatorEdgeNode.AppendChild(drawingAreaSeperatorEdgeXstartNode);
            drawingAreaSeperatorEdgeNode.AppendChild(drawingAreaSeperatorEdgeYstartNode);
            drawingAreaSeperatorEdgeNode.AppendChild(drawingAreaSeperatorEdgeXendNode);
            drawingAreaSeperatorEdgeNode.AppendChild(drawingAreaSeperatorEdgeYendNode);

            // 내용 설정
            folderNode.InnerText   = pascalVocData.folder;
            filenameNode.InnerText = pascalVocData.filename;
            pathNode.InnerText     = pascalVocData.path;

            widthNode.InnerText  = pascalVocData.width.ToString();
            heightNode.InnerText = pascalVocData.height.ToString();
            depthNode.InnerText  = pascalVocData.depth.ToString();

            externalBorderLineBndBoxXminNode.InnerText = 0.ToString();
            externalBorderLineBndBoxYminNode.InnerText = 0.ToString();
            externalBorderLineBndBoxXmaxNode.InnerText = 0.ToString();
            externalBorderLineBndBoxYmaxNode.InnerText = 0.ToString();

            pureDrawingAreaNodeBndBoxXminNode.InnerText = 0.ToString();
            pureDrawingAreaNodeBndBoxYminNode.InnerText = 0.ToString();
            pureDrawingAreaNodeBndBoxXmaxNode.InnerText = 0.ToString();
            pureDrawingAreaNodeBndBoxYmaxNode.InnerText = 0.ToString();

            noteAreaNodeBndBoxXminNode.InnerText = 0.ToString();
            noteAreaNodeBndBoxYminNode.InnerText = 0.ToString();
            noteAreaNodeBndBoxXmaxNode.InnerText = 0.ToString();
            noteAreaNodeBndBoxYmaxNode.InnerText = 0.ToString();

            titleAreaNodeBndBoxXminNode.InnerText = 0.ToString();
            titleAreaNodeBndBoxYminNode.InnerText = 0.ToString();
            titleAreaNodeBndBoxXmaxNode.InnerText = 0.ToString();
            titleAreaNodeBndBoxYmaxNode.InnerText = 0.ToString();

            drawingAreaSeperatorEdgeXstartNode.InnerText = 0.ToString();
            drawingAreaSeperatorEdgeYstartNode.InnerText = 0.ToString();
            drawingAreaSeperatorEdgeXendNode.InnerText   = 0.ToString();
            drawingAreaSeperatorEdgeYendNode.InnerText   = 0.ToString();

            // object 가져오기
            // pascal voc에는 symbol, line의 구분과 type의 구분이 없으므로,
            // symbol object의 unspecified type으로 가져올 것
            int objectCount = pascalVocData.objectList.Count;

            for (int i = 0; i < objectCount; i++)
            {
                XmlNode objectNode           = xmlDoc.CreateElement("symbol_object");
                XmlNode objectTypeNode       = xmlDoc.CreateElement("type");
                XmlNode objectClassNode      = xmlDoc.CreateElement("class");
                XmlNode objectBndboxNode     = xmlDoc.CreateElement("bndbox");
                XmlNode objectBndboxXminNode = xmlDoc.CreateElement("xmin");
                XmlNode objectBndboxYminNode = xmlDoc.CreateElement("ymin");
                XmlNode objectBndboxXmaxNode = xmlDoc.CreateElement("xmax");
                XmlNode objectBndboxYmaxNode = xmlDoc.CreateElement("ymax");
                XmlNode objectDegreeNode     = xmlDoc.CreateElement("degree");
                XmlNode objectFlipNode       = xmlDoc.CreateElement("flip");

                rootNode.AppendChild(objectNode);
                objectNode.AppendChild(objectTypeNode);
                objectNode.AppendChild(objectClassNode);
                objectNode.AppendChild(objectBndboxNode);
                objectBndboxNode.AppendChild(objectBndboxXminNode);
                objectBndboxNode.AppendChild(objectBndboxYminNode);
                objectBndboxNode.AppendChild(objectBndboxXmaxNode);
                objectBndboxNode.AppendChild(objectBndboxYmaxNode);
                objectNode.AppendChild(objectDegreeNode);

                // node node 내용 정의
                objectTypeNode.InnerText       = "unspecified_symbol";
                objectClassNode.InnerText      = pascalVocData.objectList[i].name;
                objectBndboxXminNode.InnerText = pascalVocData.objectList[i].xMin.ToString();
                objectBndboxYminNode.InnerText = pascalVocData.objectList[i].yMin.ToString();
                objectBndboxXmaxNode.InnerText = pascalVocData.objectList[i].xMax.ToString();
                objectBndboxYmaxNode.InnerText = pascalVocData.objectList[i].yMax.ToString();
                objectDegreeNode.InnerText     = 0.ToString();
                objectFlipNode.InnerText       = "n";
            }

            // 파일명으로 저장
            xmlDoc.Save(fullFileName);
        }
Пример #2
0
        private void buttonSaveAsIntegratedPascalVocFormat_Click(object sender, EventArgs e)
        {
            int iListCount = listViewFiles.Items.Count;

            if (iListCount < 2)
            {
                MessageBox.Show("통합할 파일을 2개 이상 추가해 주세요");
                return;
            }
            else
            {
                // 경고 메시지
                DialogResult ret = MessageBox.Show("Filename will be overwritten. If you need, you should make a copy.", "Filename Overwritten Warning", MessageBoxButtons.YesNo);

                if (ret == DialogResult.No)
                {
                    return;
                }

                // 통합할 파일 여러개 돌면서, 리스트에 추가
                List <PascalVocFormat> pascalVocDataList = new List <PascalVocFormat>();

                for (int i = 0; i < iListCount; i++)
                {
                    // ListView에서 이름 가져와서, 가져올 파일명 만들기
                    string fileName      = listViewFiles.Items[i].SubItems[1].Text;
                    string directoryName = listViewFiles.Items[i].SubItems[2].Text;
                    string fullFileName  = directoryName + "\\" + fileName;

                    PascalVocFormat pascalVocData = new PascalVocFormat();

                    // 파일 있는지 확인
                    if (File.Exists(fullFileName) == false)
                    {
                        return;
                    }

                    // PascalVocl Format으로 읽어오기
                    pascalVocData = CommonUtil.ReadPascalVocFormat(fullFileName);
                    pascalVocDataList.Add(pascalVocData);
                }



                // 통합할 파일 여러개 돌면서, 파일 내용 통합
                // 일반적 정보는 첫번째 파일 정보로 대체
                // object 정보는 둘을 통합

                // ListView에서 이름 가져와서, 가져올 파일명 만들기
                string firstFilename      = listViewFiles.Items[0].SubItems[1].Text;
                string firstDirectoryName = listViewFiles.Items[0].SubItems[2].Text;
                string firstFullFileName  = firstDirectoryName + "\\" + firstFilename;
                string newFullFieName     = CommonUtil.AddTailToFullFileName(firstFullFileName, "_integrated");

                // xml 정보 생성
                XmlDocument xmlDoc = new XmlDocument();

                // node 정의
                XmlNode rootNode      = xmlDoc.CreateElement("annotation");
                XmlNode folderNode    = xmlDoc.CreateElement("folder");
                XmlNode filenameNode  = xmlDoc.CreateElement("filename");
                XmlNode pathNode      = xmlDoc.CreateElement("path");
                XmlNode sourceNode    = xmlDoc.CreateElement("source");
                XmlNode databaseNode  = xmlDoc.CreateElement("database");
                XmlNode sizeNode      = xmlDoc.CreateElement("size");
                XmlNode widthNode     = xmlDoc.CreateElement("width");
                XmlNode heightNode    = xmlDoc.CreateElement("height");
                XmlNode depthNode     = xmlDoc.CreateElement("depth");
                XmlNode segmentedNode = xmlDoc.CreateElement("segmented");

                // 구조 정의
                xmlDoc.AppendChild(rootNode);
                rootNode.AppendChild(folderNode);
                rootNode.AppendChild(filenameNode);
                rootNode.AppendChild(pathNode);
                rootNode.AppendChild(sourceNode);
                sourceNode.AppendChild(databaseNode);
                rootNode.AppendChild(sizeNode);
                sizeNode.AppendChild(widthNode);
                sizeNode.AppendChild(heightNode);
                sizeNode.AppendChild(depthNode);
                rootNode.AppendChild(segmentedNode);

                for (int i = 0; i < pascalVocDataList.Count; i++)
                {
                    // 첫번째 파일에서 heder 정보 (folder, filename, path, source, size, segmented) 가져오기
                    // 첫번째 파일에서 object 정보 가져오기
                    if (i == 0)
                    {
                        folderNode.InnerText    = pascalVocDataList[i].folder;
                        filenameNode.InnerText  = pascalVocDataList[i].filename;
                        pathNode.InnerText      = pascalVocDataList[i].path;
                        databaseNode.InnerText  = "Unknown";
                        widthNode.InnerText     = pascalVocDataList[i].width.ToString();
                        heightNode.InnerText    = pascalVocDataList[i].height.ToString();
                        depthNode.InnerText     = pascalVocDataList[i].depth.ToString();
                        segmentedNode.InnerText = 0.ToString();
                    }

                    // 모든 파일에서 object 정보만 가져와서 저장
                    // object
                    for (int j = 0; j < pascalVocDataList[i].objectList.Count; j++)
                    {
                        XmlNode objectNode           = xmlDoc.CreateElement("object");
                        XmlNode objectNameNode       = xmlDoc.CreateElement("name");
                        XmlNode objectPoseNode       = xmlDoc.CreateElement("pose");
                        XmlNode objectTruncatedNode  = xmlDoc.CreateElement("truncated");
                        XmlNode objectDifficultNode  = xmlDoc.CreateElement("difficult");
                        XmlNode objectOccludedNode   = xmlDoc.CreateElement("occluded");
                        XmlNode objectBndboxNode     = xmlDoc.CreateElement("bndbox");
                        XmlNode objectBndboxXminNode = xmlDoc.CreateElement("xmin");
                        XmlNode objectBndboxYminNode = xmlDoc.CreateElement("ymin");
                        XmlNode objectBndboxXmaxNode = xmlDoc.CreateElement("xmax");
                        XmlNode objectBndboxYmaxNode = xmlDoc.CreateElement("ymax");

                        rootNode.AppendChild(objectNode);
                        objectNode.AppendChild(objectNameNode);
                        objectNode.AppendChild(objectPoseNode);
                        objectNode.AppendChild(objectTruncatedNode);
                        objectNode.AppendChild(objectDifficultNode);
                        objectNode.AppendChild(objectOccludedNode);
                        objectNode.AppendChild(objectBndboxNode);
                        objectBndboxNode.AppendChild(objectBndboxXminNode);
                        objectBndboxNode.AppendChild(objectBndboxYminNode);
                        objectBndboxNode.AppendChild(objectBndboxXmaxNode);
                        objectBndboxNode.AppendChild(objectBndboxYmaxNode);

                        // node node 내용 정의
                        objectNameNode.InnerText       = pascalVocDataList[i].objectList[j].name;
                        objectPoseNode.InnerText       = "Unspecified";
                        objectTruncatedNode.InnerText  = "0";
                        objectDifficultNode.InnerText  = "0";
                        objectOccludedNode.InnerText   = "0";
                        objectBndboxXminNode.InnerText = pascalVocDataList[i].objectList[j].xMin.ToString();
                        objectBndboxYminNode.InnerText = pascalVocDataList[i].objectList[j].yMin.ToString();
                        objectBndboxXmaxNode.InnerText = pascalVocDataList[i].objectList[j].xMax.ToString();
                        objectBndboxYmaxNode.InnerText = pascalVocDataList[i].objectList[j].yMax.ToString();
                    }
                }

                // 파일명으로 저장
                xmlDoc.Save(newFullFieName);
            }

            MessageBox.Show("변환이 완료되었습니다.");
        }
Пример #3
0
        // pascal voc format 파일 읽기
        public static PascalVocFormat ReadPascalVocFormat(string fullFilename)
        {
            // 메모리에 저장
            PascalVocFormat data = new PascalVocFormat();

            // 문서 정의
            XmlDocument xmlDoc = new XmlDocument();

            // 문서 부르기
            xmlDoc.Load(fullFilename);

            // 일반 node 선택 및 정의
            XmlNodeList xmlAnnotationNodes = xmlDoc.SelectNodes("/annotation");
            XmlNode     xmlAnnotationNode  = xmlAnnotationNodes.Item(0);

            // 일반 node 정보 가져오기
            data.folder    = xmlAnnotationNode.SelectSingleNode("folder").InnerText;
            data.filename  = xmlAnnotationNode.SelectSingleNode("filename").InnerText;
            data.path      = xmlAnnotationNode.SelectSingleNode("path").InnerText;
            data.database  = xmlAnnotationNode.SelectSingleNode("source").SelectSingleNode("database").InnerText;
            data.width     = Convert.ToInt32(xmlAnnotationNode.SelectSingleNode("size").SelectSingleNode("width").InnerText);
            data.height    = Convert.ToInt32(xmlAnnotationNode.SelectSingleNode("size").SelectSingleNode("height").InnerText);
            data.depth     = Convert.ToInt32(xmlAnnotationNode.SelectSingleNode("size").SelectSingleNode("depth").InnerText);
            data.segmented = xmlAnnotationNode.SelectSingleNode("segmented").InnerText;

            // object node 선택 및 정의
            XmlNodeList xmlObjectNodes = xmlDoc.SelectNodes("/annotation/object");

            // 메모리에 저장

            List <PascalVocObject> pascalVocObjectList = new List <PascalVocObject>();

            // object node 갯수만큼 돌면서 정보 가져오기
            foreach (XmlNode xmlObjectNode in xmlObjectNodes)
            {
                PascalVocObject objectData = new PascalVocObject();

                // xml 파일에 노드가 비어있으면 발생하는 에러 처리
                objectData.name = xmlObjectNode.SelectSingleNode("name").InnerText;
                objectData.pose = xmlObjectNode.SelectSingleNode("pose").InnerText;

                XmlNode truncatedNode = xmlObjectNode.SelectSingleNode("truncated");
                if (truncatedNode != null)
                {
                    objectData.truncated = Convert.ToInt32(truncatedNode.InnerText);
                }

                XmlNode difficultNode = xmlObjectNode.SelectSingleNode("difficult");
                if (difficultNode != null)
                {
                    objectData.truncated = Convert.ToInt32(difficultNode.InnerText);
                }

                XmlNode occludedNode = xmlObjectNode.SelectSingleNode("truncated");
                if (occludedNode != null)
                {
                    objectData.truncated = Convert.ToInt32(occludedNode.InnerText);
                }

                // 바운딩 박스 선택
                XmlNode xmlObjectBndboxNode = xmlObjectNode.SelectSingleNode("bndbox");
                objectData.xMin = Convert.ToInt32(xmlObjectBndboxNode.SelectSingleNode("xmin").InnerText);
                objectData.yMin = Convert.ToInt32(xmlObjectBndboxNode.SelectSingleNode("ymin").InnerText);
                objectData.xMax = Convert.ToInt32(xmlObjectBndboxNode.SelectSingleNode("xmax").InnerText);
                objectData.yMax = Convert.ToInt32(xmlObjectBndboxNode.SelectSingleNode("ymax").InnerText);

                pascalVocObjectList.Add(objectData);
            }

            data.objectList = pascalVocObjectList;

            return(data);
        }