简介:在做标注的时候常常会分不同时间不同批次来标注数据,最后合并的时候,有的json标签文件和image文件相对路径不同,则直接把所有图片和标签复制在一起,用labelme打开会出错。
注:最终的文件图片名和json标签名一致,json文件的"imagePath"也与图片一致(json文件和image的相对路径关系可改–prePath)
合并数据集
import json
import os
import cv2 as cv
import shutil
def fromJsonGetImagePath(jsonPath):
"""
return imageName
"""
data = []
with open(jsonPath, "r", encoding='utf-8') as f:
jsonData = json.load(f)
img_name = jsonData["imagePath"]
img_name = img_name.rsplit("\\",1)[-1]
return img_name
def changeJsonImagePath(jsonPath,imagePath):
"""
return imageName
"""
with open(jsonPath, "r", encoding='utf-8') as f:
jsonData = json.load(f)
jsonData["imagePath"] = imagePath
with open(jsonPath,"w",encoding="utf-8") as fo:
json.dump(jsonData,fo)
def getAllJsonFilePath(jsonFolder):
jsonPathList = []
for item in os.listdir(jsonFolder):
if item.rsplit(".",1)[-1] != "json":
continue
jsonPathList.append(os.path.join(jsonFolder,item))
return jsonPathList
不同批次的数据集合并
- 找不同的文件夹中的所有json文件
- 从json文件中找出图片名,获取对应的图片
- 按一定命名规则复制到另一个文件夹
if __name__ == "__main__":
rootPath = r"K:\imageData\SMTdataset\SMTData"
imageFolder1 = rootPath + "\\image"#原始图片
labelFolder1 = rootPath + "\\label6"#标准标注文件的存放路径,标准文件以json格式存储
imageFolder2 = rootPath + "\\image2"#原始图片
labelFolder2 = rootPath + "\\image2"#标准标注文件的存放路径,标准文件以json格式存储
tempFolder = rootPath + "\\temp"
dstImageFolder = tempFolder + "\\image"
dstLabelFolder = tempFolder + "\\label"
#修改json文件名,和位置后,需要把json里面的imagePath也做对应的修改
prePath = "..\\\\image\\\\"#四条\会转为两条,\表示转义
ZFILLN = 8#零填充数
if not os.path.exists(dstImageFolder):
try:
os.makedirs(dstImageFolder)
except:
print("create {} failed!".format(dstImageFolder))
if not os.path.exists(dstLabelFolder):
try:
os.makedirs(dstLabelFolder)
except:
print("create {} failed!".format(dstLabelFolder))
labelPathLists1 = getAllJsonFilePath(labelFolder1)
labelPathLists2 = getAllJsonFilePath(labelFolder2)
imagePathLists1 = [os.path.join(imageFolder1,fromJsonGetImagePath(labelPathLists1[i])) for i in range(len(labelPathLists1))]
imagePathLists2 = [os.path.join(imageFolder2,fromJsonGetImagePath(labelPathLists2[i])) for i in range(len(labelPathLists2))]
for i in range(len(labelPathLists1)):
dstLabelPath = os.path.join(dstLabelFolder,str(i).zfill(ZFILLN)+"."+labelPathLists1[i].rsplit(".",1)[-1])
dstImagePath = os.path.join(dstImageFolder,str(i).zfill(ZFILLN)+"."+imagePathLists1[i].rsplit(".",1)[-1])
print("srcLabel: {} ----> dstLabel:{}".format(labelPathLists1[i],dstLabelPath))
print("srcImage: {} ----> dstImage:{}".format(imagePathLists1[i],dstImagePath))
shutil.copy(labelPathLists1[i],dstLabelPath)
shutil.copy(imagePathLists1[i],dstImagePath)
dstImageName = dstImagePath.rsplit("\\",1)[-1]
changeJsonImagePath(dstLabelPath,prePath + dstImageName)
for j in range(len(labelPathLists2)):
dstLabelPath = os.path.join(dstLabelFolder,str(i+j+1).zfill(ZFILLN)+"."+labelPathLists2[j].rsplit(".",1)[-1])
dstImagePath = os.path.join(dstImageFolder,str(i+j+1).zfill(ZFILLN)+"."+imagePathLists2[j].rsplit(".",1)[-1])
print("srcLabel: {} ----> dstLabel:{}".format(labelPathLists2[j],dstLabelPath))
print("srcImage: {} ----> dstImage:{}".format(imagePathLists2[j],dstImagePath))
shutil.copy(labelPathLists2[j],dstLabelPath)
shutil.copy(imagePathLists2[j],dstImagePath)
dstImageName = dstImagePath.rsplit("\\",1)[-1]
changeJsonImagePath(dstLabelPath,prePath + dstImageName)
srcLabel: K:\imageData\SMTdataset\SMTData\label6\00000003.json ----> dstLabel:K:\imageData\SMTdataset\SMTData\temp\label\00000000.json
srcImage: K:\imageData\SMTdataset\SMTData\image\00000003.bmp ----> dstImage:K:\imageData\SMTdataset\SMTData\temp\image\00000000.bmp
srcLabel: K:\imageData\SMTdataset\SMTData\label6\00000004.json ----> dstLabel:K:\imageData\SMTdataset\SMTData\temp\label\00000001.json
srcImage: K:\imageData\SMTdataset\SMTData\image\00000004.bmp ----> dstImage:K:\imageData\SMTdataset\SMTData\temp\image\00000001.bmp
srcLabel: K:\imageData\SMTdataset\SMTData\label6\00000006.json ----> dstLabel:K:\imageData\SMTdataset\SMTData\temp\label\00000002.json
srcImage: K:\imageData\SMTdataset\SMTData\image\00000006.bmp ----> dstImage:K:\imageData\SMTdataset\SMTData\temp\image\00000002.bmp
srcLabel: K:\imageData\SMTdataset\SMTData\label6\00000007.json ----> dstLabel:K:\imageData\SMTdataset\SMTData\temp\label\00000003.json
srcImage: K:\imageData\SMTdataset\SMTData\image\00000007.bmp ----> dstImage:K:\imageData\SMTdataset\SMTData\temp\image\00000003.bmp
srcLabel: K:\imageData\SMTdataset\SMTData\label6\00000008.json ----> dstLabel:K:\imageData\SMTdataset\SMTData\temp\label\00000004.json
srcImage: K:\imageData\SMTdataset\SMTData\image\00000008.bmp ----> dstImage:K:\imageData\SMTdataset\SMTData\temp\image\00000004.bmp
srcLabel: K:\imageData\SMTdataset\SMTData\label6\00000009.json ----> dstLabel:K:\imageData\SMTdataset\SMTData\temp\label\00000005.json
srcImage: K:\imageData\SMTdataset\SMTData\image\00000009.bmp ----> dstImage:K:\imageData\SMTdataset\SMTData\temp\image\00000005.bmp
srcLabel: K:\imageData\SMTdataset\SMTData\label6\00000015.json ----> dstLabel:K:\imageData\SMTdataset\SMTData\temp\label\00000006.json
srcImage: K:\imageData\SMTdataset\SMTData\image\00000015.bmp ----> dstImage:K:\imageData\SMTdataset\SMTData\temp\image\00000006.bmp
srcLabel: K:\imageData\SMTdataset\SMTData\label6\00000016.json ----> dstLabel:K:\imageData\SMTdataset\SMTData\temp\label\00000007.json
srcImage: K:\imageData\SMTdataset\SMTData\image\00000016.bmp ----> dstImage:K:\imageData\SMTdataset\SMTData\temp\image\00000007.bmp
srcLabel: K:\imageData\SMTdataset\SMTData\label6\00000017.json ----> dstLabel:K:\imageData\SMTdataset\SMTData\temp\label\00000008.json
srcImage: K:\imageData\SMTdataset\SMTData\image\00000017.bmp ----> dstImage:K:\imageData\SMTdataset\SMTData\temp\image\00000008.bmp
srcLabel: K:\imageData\SMTdataset\SMTData\label6\00000018.json ----> dstLabel:K:\imageData\SMTdataset\SMTData\temp\label\00000009.json
srcImage: K:\imageData\SMTdataset\SMTData\image\00000018.bmp ----> dstImage:K:\imageData\SMTdataset\SMTData\temp\image\00000009.bmp