zoukankan      html  css  js  c++  java
  • 统一处理labelme标注的json文件名

    处理json文件中的文件名

    package com.vfsd.core;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class ReadJsonAndSaveNewFile {
        private static List<String> json_list = new ArrayList<String>();
        
        public static void main(String[] args) {
            
            String folder="G:\json_data";
            String endStr=".json";
            boolean isTrue=true;
            listFilesByEndStr(folder, endStr,isTrue);
            
            listJsonFile();
            
        }
        
        public static void listFilesByEndStr(String folder,String endStr,boolean isTrue) {
            File folder1 = new File(folder);
            File[] files = folder1.listFiles();
            
            
            for(File indexFile:files) {
                
                if(indexFile.isDirectory()) {
                    String folderPath = indexFile.getAbsolutePath();
                    listFilesByEndStr(folderPath,endStr,isTrue);
                }else if(indexFile.isFile()) {
                    String fileName = indexFile.getName();
                    if(isTrue) {
                        if(fileName.endsWith(endStr)) {
                            String folderPath = indexFile.getAbsolutePath();
                            //System.out.println(folderPath);
                            
                            json_list.add(folderPath);
                        }
                    }else {
                        if(!fileName.endsWith(endStr)) {
                            //System.out.println(fileName);
                        }
                    }
                    
                }
            }
        }
        
        public static void listJsonFile() {
            
            String outputPath = "G:\json_data2";
            int num=1000;
            for(int k=0;k<json_list.size();k++){
                String indexJsonFilePath = json_list.get(k);
                System.out.println(indexJsonFilePath);
                String imgFilePath = indexJsonFilePath.replace(".json", ".jpg");
                System.out.println(imgFilePath);
                
                String newJsonFilePath = outputPath+"\"+num+".json";
                String newImgFilePath = outputPath+"\"+num+".jpg";
                String imgName = num+".jpg";
                
                try {
                    copyImg(imgFilePath,newImgFilePath);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                
                try {
                    copyJson(indexJsonFilePath,newJsonFilePath,imgName);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                num++;
            }
            
            
            
            
            
        }
        
        
        public static void copyImg(String oldImgPath,String newImgPath) throws IOException{
            File oldFile = new File(oldImgPath);
            File newFile = new File(newImgPath);
            
            FileInputStream fileInputStreamObj = new FileInputStream(oldFile);
            FileOutputStream filOutputStreamObj = new FileOutputStream(newFile);
            
            byte[] bytes = new byte[1024];
            int len=0;
            while((len=fileInputStreamObj.read(bytes))!=-1){
                filOutputStreamObj.write(bytes, 0, len);
            }
            
            filOutputStreamObj.flush();
            filOutputStreamObj.close();
            fileInputStreamObj.close();
            
            
        }
        
        
        public static void copyJson(String oldJsonPath,String newJsonPath,String imgName) throws IOException{
            File oldJsonFile = new File(oldJsonPath);
            File newJsonFile = new File(newJsonPath);
            
            FileReader fileReaderObj = new FileReader(oldJsonFile);
            BufferedReader bufferedReaderObj = new BufferedReader(fileReaderObj);
            
            FileWriter fileWriterObj = new FileWriter(newJsonFile);
            BufferedWriter bufferedWriterObj = new BufferedWriter(fileWriterObj);
            
            String lineStr="";
            while((lineStr=bufferedReaderObj.readLine())!=null) {
                if(lineStr.contains("imagePath")){
                    //  "imagePath": "u=46225769,59147520&fm=26&fmt=auto&gp=0-1414246.jpg",
                    lineStr=""imagePath": ""+imgName+"",";
                }
                bufferedWriterObj.write(lineStr+"
    ");
                
            }
            
            
            bufferedWriterObj.flush();
            fileWriterObj.flush();
            bufferedWriterObj.close();
            fileWriterObj.close();
            
            bufferedReaderObj.close();
            fileReaderObj.close();
            
        }
        ///
    }
    G:json_datau=973425934,2427009704&fm=26&fmt=auto&gp=0-8539165.json
    G:json_datau=973425934,2427009704&fm=26&fmt=auto&gp=0-8539165.jpg
    G:json_datau=98157198,2014981619&fm=26&fmt=auto&gp=0-5779385.json
    G:json_datau=98157198,2014981619&fm=26&fmt=auto&gp=0-5779385.jpg
    G:json_datau=994802799,2240003585&fm=26&fmt=auto&gp=0-8749040.json
    G:json_datau=994802799,2240003585&fm=26&fmt=auto&gp=0-8749040.jpg
    G:json_datau=999068942,1187608502&fm=26&fmt=auto&gp=0-7583789.json
    G:json_datau=999068942,1187608502&fm=26&fmt=auto&gp=0-7583789.jpg
    1000.jpg
    1000.json
    1001.jpg
    1001.json
    1002.jpg
    1002.json

    同时也修改了json文件中的imagePath对应的值

    "imagePath": "1000.jpg",

    ##################3

    QQ 3087438119
  • 相关阅读:
    java8 stream().map().collect()用法
    Java中Collections的emptyList、EMPTY_LIST详解
    zxing实现java二维码生成和解析
    机器学习与数据挖掘—K邻近算法(KNN)
    第一个Web项目(IDEA)
    Ucore操作系统实验-实验课程设计
    数据挖掘与机器学习--损失函数
    机器数据挖掘--常见监督学习算法以及数据挖掘流程
    Tomcat安装-环境变量配置-启动-关闭
    操作系统实验教程(Ucore)--Lab6
  • 原文地址:https://www.cnblogs.com/herd/p/14809475.html
Copyright © 2011-2022 走看看