标签:成新 labelme point image json cut org txt
# 读取json文件内容,返回字典格式
from collections import defaultdict, OrderedDict
import json
import cv2
from labelme import utils
def new_json(jsonname,xgpoints,txtpoints,imagepath,imageH,imageW,imagedata,xg_shapetype='rectangle',txt_shapetype="polygon"):
# xg_dist={
# "label":"xg",
# "points": xgpoints,
# "group_id": None,
# "shape_type": xg_shapetype,
# "flags": {}
# }
txt_dist={
"label":"txt",
"points": txtpoints,
"group_id": None,
"shape_type": txt_shapetype,
"flags": {}
}
shape = []
# shape.append(xg_dist)
shape.append(txt_dist)
test_dict = {
'version': "4.5.9",
"flags": {},
"shapes":shape,
"imagePath": imagepath,
"imageData":imagedata,
"imageHeight": imageH,
"imageWidth": imageW
}
json_str = json.dumps(test_dict, indent=4)
with open(jsonname, 'w') as json_file:
json_file.write(json_str)
def savefile(bs,path):
with open(path,'wb') as ds:
ds.write(bytes(bs))
ds.close()
path ='0926305001_10005.json'
with open(path,'r',encoding='utf8')as fp:
newimage_dir ='newimage/'
newjson_dir = 'newjson/'
json_data = json.load(fp)
labels = json_data.get('shapes')
org_imagepath = json_data.get("imagePath")
org_image = cv2.imread(org_imagepath)
print(org_image.shape)
org_point=[]
txt_point =[]
for i in range(len(labels)):
if labels[i]['label'] =='xg':
org_point = labels[i]['points']
st_x = int(org_point[0][0])
st_y = int(org_point[0][1])
ed_x = int(org_point[1][0])
ed_y = int(org_point[1][1])
cut_image =org_image[st_y:ed_y,st_x:ed_x,:]
cut_image_temp = cv2.cvtColor(cut_image,cv2.COLOR_BGR2RGB)
imagedata=utils.img_arr_to_b64(cut_image_temp).decode('utf-8')
cut_image_data =cv2.imencode(".jpg",cut_image)[1].tobytes()
newimage_name = path.split('.')[0]
newimage_path = newimage_dir + newimage_name +'cut.jpg'
json_name = newjson_dir+newimage_name +'cut.json'
savefile(cut_image_data,newimage_path)
elif labels[i]['label'] =='txt':
txt_point = labels[i]['points']
else:
pass
if len(org_point)>0 and len(txt_point)>0:
for i in range(len(txt_point)):
txt_point[i][0]=txt_point[i][0]-org_point[0][0]
txt_point[i][1]=txt_point[i][1]-org_point[0][1]
org_point[1][0] = org_point[1][0]-org_point[0][0]
org_point[1][1] = org_point[1][1]-org_point[0][1]
org_point[0][0] = 0
org_point[0][1] = 0
new_json(json_name,org_point,txt_point,newimage_path,int(org_point[1][0]),int(org_point[1][1]),imagedata)
break
标签:成新,labelme,point,image,json,cut,org,txt 来源: https://blog.csdn.net/qq_51609636/article/details/120554372
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。