crowhuman annotation_val.odgt标签解析
import cv2
import json
data = {"ID": "273278,600e5000db6370fb", "gtboxes": [{"fbox": [67, 60, 333, 810], "tag": "person", "hbox": [203, 68, 86, 93], "extra": {"box_id": 0, "occ": 0}, "vbox": [67, 60, 333, 740], "head_attr": {"ignore": 0, "occ": 1, "unsure": 0}}, {"fbox": [438, 205, 175, 541], "tag": "person", "hbox": [509, 233, 50, 66], "extra": {"box_id": 1, "occ": 0}, "vbox": [438, 205, 175, 541], "head_attr": {"ignore": 0, "occ": 0, "unsure": 0}}, {"fbox": [568, 208, 194, 550], "tag": "person", "hbox": [606, 218, 61, 69], "extra": {"box_id": 2, "occ": 1}, "vbox": [576, 212, 185, 543], "head_attr": {"ignore": 0, "occ": 0, "unsure": 0}}, {"fbox": [637, 215, 139, 393], "tag": "person", "hbox": [676, 220, 40, 53], "extra": {"box_id": 3, "occ": 1}, "vbox": [670, 216, 108, 389], "head_attr": {"ignore": 0, "occ": 0, "unsure": 0}}, {"fbox": [776, 382, 25, 220], "tag": "mask", "hbox": [776, 382, 25, 220], "extra": {"ignore": 1}, "vbox": [776, 382, 25, 220], "head_attr": {}}]}
img=cv2.imread("test.jpg")
print(data["ID"])
for obj in data["gtboxes"]:
draw_0 = cv2.rectangle(img, (obj['hbox'][0], obj['hbox'][1]), (obj['hbox'][0]+obj['hbox'][2], obj['hbox'][1]+obj['hbox'][3]), (255, 0, 0), 1)
# print('head_attr',obj['head_attr'])
draw_0 = cv2.rectangle(img, (obj['fbox'][0], obj['fbox'][1]),(obj['fbox'][0] + obj['fbox'][2], obj['fbox'][1] + obj['fbox'][3]), (0, 255, 0), 1)
draw_0 = cv2.rectangle(img, (obj['vbox'][0], obj['vbox'][1]),(obj['vbox'][0] + obj['vbox'][2], obj['vbox'][1] + obj['vbox'][3]), (0, 0,255), 1)
# print('extra',obj['extra'])
cv2.imshow("test",img)
cv2.waitKey(0)
提取annotation_val.odgt里面的fbox,并输出result.txt固定的格式。
annotation_val.odgt格式基本如下:
{“ID”: “273271,c9db000d5146c15”, “gtboxes”: [{“fbox”: [72, 202, 163, 503], “tag”: “person”, “hbox”: [171, 208, 62, 83], “extra”: {“box_id”: 0, “occ”: 0}, “vbox”: [72, 202, 163, 398], “head_attr”: {“ignore”: 0, “occ”: 0, “unsure”: 0}}, {“fbox”: [199, 180, 144, 499], “tag”: “person”, “hbox”: [268, 183, 60, 83], “extra”: {“box_id”: 1, “occ”: 0}, “vbox”: [199, 180, 144, 420], “head_attr”: {“ignore”: 0, “occ”: 0, “unsure”: 0}}, {“fbox”: [310, 200, 162, 497], “tag”: “person”, “hbox”: [363, 219, 54, 71], “extra”: {“box_id”: 2, “occ”: 0}, “vbox”: [310, 200, 162, 400], “head_attr”: {“ignore”: 0, “occ”: 0, “unsure”: 0}}, {“fbox”: [417, 182, 139, 518], “tag”: “person”, “hbox”: [455, 190, 53, 78], “extra”: {“box_id”: 3, “occ”: 0}, “vbox”: [417, 182, 139, 418], “head_attr”: {“ignore”: 0, “occ”: 0, “unsure”: 0}}, {“fbox”: [429, 171, 224, 511], “tag”: “person”, “hbox”: [537, 187, 55, 73], “extra”: {“box_id”: 4, “occ”: 1}, “vbox”: [534, 171, 113, 431], “head_attr”: {“ignore”: 0, “occ”: 0, “unsure”: 0}}, {“fbox”: [543, 178, 262, 570], “tag”: “person”, “hbox”: [602, 186, 71, 93], “extra”: {“box_id”: 5, “occ”: 0}, “vbox”: [543, 178, 257, 422], “head_attr”: {“ignore”: 0, “occ”: 0, “unsure”: 0}}]}
{“ID”: “273271,1c72c000a2ee47d5”, “gtboxes”: [{“fbox”: [23, 230, 228, 597], “tag”: “person”, “hbox”: [125, 233, 82, 110], “extra”: {“box_id”: 0, “occ”: 1}, “vbox”: [62, 234, 186, 487], “head_attr”: {“ignore”: 0, “occ”: 0, “unsure”: 0}}, {“fbox”: [162, 249, 156, 558], “tag”: “person”, “hbox”: [213, 259, 69, 87], “extra”: {“box_id”: 1, “occ”: 1}, “vbox”: [198, 256, 111, 466], “head_attr”: {“ignore”: 0, “occ”: 0, “unsure”: 0}}, {“fbox”: [271, 204, 167, 561], “tag”: “person”, “hbox”: [338, 210, 63, 92], “extra”: {“box_id”: 2, “occ”: 0}, “vbox”: [271, 204, 167, 516], “head_attr”: {“ignore”: 0, “occ”: 0, “unsure”: 0}}, {“fbox”: [384, 191, 207, 583], “tag”: “person”, “hbox”: [384, 191, 207, 529], “extra”: {“box_id”: 3, “occ”: 1}, “vbox”: [401, 193, 155, 530], “head_attr”: {“ignore”: 1, “occ”: 0, “unsure”: 0}}, {“fbox”: [448, 187, 204, 575], “tag”: “person”, “hbox”: [537, 223, 72, 94], “extra”: {“box_id”: 4, “occ”: 0}, “vbox”: [448, 187, 204, 533], “head_attr”: {“ignore”: 0, “occ”: 0, “unsure”: 0}}, {“fbox”: [595, 183, 167, 589], “tag”: “person”, “hbox”: [657, 193, 73, 98], “extra”: {“box_id”: 5, “occ”: 1}, “vbox”: [625, 184, 119, 537], “head_attr”: {“ignore”: 0, “occ”: 0, “unsure”: 0}}, {“fbox”: [724, 165, 183, 652], “tag”: “person”, “hbox”: [752, 180, 93, 108], “extra”: {“box_id”: 6, “occ”: 1}, “vbox”: [735, 180, 169, 539], “head_attr”: {“ignore”: 0, “occ”: 0, “unsure”: 0}}, {“fbox”: [0, 308, 164, 416], “tag”: “mask”, “hbox”: [0, 308, 164, 416], “extra”: {“ignore”: 1}, “vbox”: [0, 308, 164, 416], “head_attr”: {}}]}
输出result.txt格式如下:
{“height”:600,“ID”:“273271,c9db000d5146c15”,“width”:800,“dtboxes”:[{“socre”:1.0,“tag”:1,“box”:[72, 202, 235, 705]},{“socre”:1.0,“tag”:1,“box”:[199, 180, 343, 679]},{“socre”:1.0,“tag”:1,“box”:[310, 200, 472, 697]},{“socre”:1.0,“tag”:1,“box”:[417, 182, 556, 700]},{“socre”:1.0,“tag”:1,“box”:[429, 171, 653, 682]},{“socre”:1.0,“tag”:1,“box”:[543, 178, 805, 748]}]}
{“height”:720,“ID”:“273271,1c72c000a2ee47d5”,“width”:960,“dtboxes”:[{“socre”:1.0,“tag”:1,“box”:[23, 230, 251, 827]},{“socre”:1.0,“tag”:1,“box”:[162, 249, 318, 807]},{“socre”:1.0,“tag”:1,“box”:[271, 204, 438, 765]},{“socre”:1.0,“tag”:1,“box”:[384, 191, 591, 774]},{“socre”:1.0,“tag”:1,“box”:[448, 187, 652, 762]},{“socre”:1.0,“tag”:1,“box”:[595, 183, 762, 772]},{“socre”:1.0,“tag”:1,“box”:[724, 165, 907, 817]},{“socre”:1.0,“tag”:1,“box”:[0, 308, 164, 724]}]}
import os
import cv2
with open((os.path.join('annotation_val.txt')), 'r') as f:
data=f.readlines()
for line in data:
odom=line.split("]}")
data_dict = eval(odom[0]+"]}")
str_ = ""
for obj in data_dict["gtboxes"]:
box = [obj['fbox'][0],obj['fbox'][1],obj['fbox'][0]+obj['fbox'][2],obj['fbox'][1]+obj['fbox'][3]]
str_ = str_+ '{"score": 1.0, "tag": 1, \"box\": %s}, '%(str(box))
#print(str_)
#print(data_dict["ID"])
img = cv2.imread(data_dict["ID"]+".jpg")
str_height = str(img.shape[0])
str_width = str(img.shape[1])
str_all = "{"+"\"height\": "+str_height+", \"ID\": \""+data_dict["ID"]+"\", \"width\": "+str_width+", \"dtboxes\": ["+str_[:-2]+"]}"+"\n"
#print(str_all)
with open("hhh.txt", "a") as f:
f.write(str_all)
object365 json文件提取
result.json类似于一个这样的列表
[{“image_id”: 354609, “area”: 9945.156009311546, “category_id”: 21, “id”: 0, “iscrowd”: 0, “bbox”: [133.8088989172, 178.6294555648, 80.1972656534, 124.0086669824]}, {“image_id”: 354609, “area”: 13467.531777104436, “category_id”: 3, “id”: 1, “iscrowd”: 0, “bbox”: [289.74798586220004, 215.75781248, 127.72149654100002, 105.44451906559996]}, {“image_id”: 354609, “area”: 54688.424399724776, “category_id”: 3, “id”: 2, “iscrowd”: 0, “bbox”: [339.4999999909, 279.6185912832, 258.41333009830004, 211.6315918336]}]
test_data.json类似于一个这样的列表,经过一下程序得到一个字典再加上[]即为列表
[{“image_id”: 354609, “category_id”: 21, “bbox”: [133.8088989172, 178.6294555648, 80.1972656534, 124.0086669824], “score”: 1.0},{“image_id”: 354609, “category_id”: 3, “bbox”: [289.74798586220004, 215.75781248, 127.72149654100002, 105.44451906559996], “score”: 1.0},{“image_id”: 354609, “category_id”: 3, “bbox”: [339.4999999909, 279.6185912832, 258.41333009830004, 211.6315918336], “score”: 1.0},{“image_id”: 354609, “category_id”: 7, “bbox”: [495.3330078154, 234.7993164288, 45.82702636130006, 64.92156979200001], “score”: 1.0}]
import json
image_id = []
category_id = []
bbox = []
score = []
with open('result.json', 'r') as f:
data = json.load(f)
for i in range(0,len(data)):
image_id.append(data[i]["image_id"])
category_id.append(data[i]["category_id"])
bbox.append(data[i]["bbox"])
score.append(1.0)
print("提取完成")
with open('test_data.json', 'w') as json_file:
for i in range(0,len(data)):
dict = {"image_id":image_id[i],"category_id":category_id[i],"bbox":bbox[i],"score":score[i]}
json_str = json.dumps(dict)+","
json_file.write(json_str)
print("写入完成")
txt读取和写入
import os
with open((os.path.join('result.txt')), 'r') as f:
data=f.readlines()
for line in data:
odom=line.split(" ")
tmp_str = "".join(odom)
with open((os.path.join('result_out.txt')), 'w') as f:
f.write(tmp_str)