标注工具:VGG Image Annotator
效果:
?
?
使用方法:
1,放在目录下双击运行
2,目录下打开cmd,传入参数 log,可以保存日志,查看结果
mod_json.exe log
?3,源码
# -*- coding:utf-8 -*-
import json
import sys
import os
import shutil
from pip._vendor.distlib.compat import raw_input
def mod_jsonVal(file,logFlag,logFile):
with open(file,encoding="utf-8") as f:
data = json.load(f)
# 每张图
for jpgIndex,jpg in enumerate(data['_via_img_metadata']):
flag = False
# 一张图中的每个框
regions = data['_via_img_metadata'][jpg]['regions']
# 筛选 图中的每个矩形框 rect
for index,r in enumerate(regions):
if (r['shape_attributes']['name'] == "rect"):
s = '{} {} {} {}'.format("原数据",index,":",regions[index])
print(s)
save_log(logFlag,logFile,s)
# 矩形数据转化四边形数据
regions[index]['shape_attributes'] = mod_RectToPolygon(regions,index, r)
s1 = '{} {} {} {}'.format("修改后", index, ":", regions[index])
print(s1)
save_log(logFlag,logFile,s1)
flag = True
if flag:
save_newVal(data, file)
s = '{}{} {}'.format(jpg, jpgIndex + 1, "有矩形,已修改\n")
print(s)
save_log(logFlag,logFile,s)
else:
s = '{}{} {}'.format(jpg, jpgIndex+1,"没有发现矩形,无需修改")
print(s)
save_log(logFlag,logFile, s)
# 数据重组 矩形转四边形
def mod_RectToPolygon(regions,index,r):
'''{'name': 'rect', 'x': 477, 'y': 905, 'width': 92, 'height': 42} 转换成如下
{'name': 'polygon', 'all_points_x': [477, 569, 569, 477], 'all_points_y': [905, 905, 946, 946]}'''
# 取得矩形的有效值
x = r['shape_attributes']['x']
y = r['shape_attributes']['y']
width = r['shape_attributes']['width']
height = r['shape_attributes']['height']
# 矩形转换成多边形数据
j = {"name": "polygon", "all_points_x": '', "all_points_y": ''}
# 必须转化为json,否则会变成字符串 --> 'all_points_x': [2606, 2652, 2652, 2606]
j['all_points_x'] = json.loads("[" + str(x) + "," + str(x + width) + "," + str(x + width) + "," + str(x) + "]")
j['all_points_y'] = json.loads("[" + str(y) + "," + str(y) + "," + str(y + height) + "," + str(y + height) + "]")
return j
# 保存新的json
def save_newVal(data1,file):
with open(file, 'w',encoding='utf-8') as f:
json.dump(data1, f, ensure_ascii=False, separators=(',', ':'))
# 遍历 含子目录,筛出 json 文件,并且备份
def list_all_files(rootdir):
_files = []
list = os.listdir(rootdir) # 列出文件夹下所有的目录与文件
for i in range(0, len(list)):
path = os.path.join(rootdir, list[i])
# 是目录则重新调用本方法,递归
if os.path.isdir(path):_files.extend(list_all_files(path))
# 取得文件,筛出 json 文件
if os.path.isfile(path) and os.path.splitext(path)[1] == ".json":
_files.append(path)
# 备份,已备份则跳过。在原目录下
bak = path+".bak"
if not os.path.isfile(bak):
shutil.copy(path,bak)
print("备份了:",bak)
# 删除备份
# if os.path.isfile(path+".bak"):os.remove(path+".bak")
return _files
# 日志保存
def save_log(l,logpath,logs):
if (l): open(logpath, "a").write('{}'.format(logs+'\n'))
# 1.遍历目录,含子目录
# 2.备份
# 3.重组json数据
# 4.日志保存(可选)
# currPath = "E:\\cdzj\\文本行检测标注-test\\文本行检测标注-693"
currPath = os.path.abspath('.').replace("\\","\\\\") # 转义
raw_input("处理当前目录 "+currPath+" (含子目录)下的所有 json文件,按回车确认!") #回车等待确认
# 是否使用日志。如果传入的参数是 log ,就保存日志到json文件同目录下
for _fs in list_all_files(currPath):
if(len(sys.argv) ==2 ):
if (sys.argv[1] == "log"):
print("\n" + _fs)
logFile = _fs.replace("json", "log.txt") # 日志文件完整路径
save_log(True, logFile, _fs + "\n")
mod_jsonVal(_fs, True, logFile)
else:
mod_jsonVal(_fs, False, _fs.replace("json", "log.txt"))
# 等待回车退出
print("\n为了保证结果可用性,不妨重新加载项目看看,检查矩形是否成功转换为矩形")
raw_input("\n按回车退出!每次运行时传入参数 log 可以保存日志哦 「红叶」\n")
下载:
mod_json.exe
|