张一极
抽取coco格式iou小于0.5数据进行可视化
iou = 交集面积/并集面积
iou<0.5意味着交重面积小于0.5
iou=1意味着完全重叠。
文件树结构:
val:测试文件图片
├── all_val.json ├── getcase.py ├── new.json ├── result ├── results.json ├── val └── val.zip
xxxxxxxxxx
1import json
2import io
3import numpy as np
4import cv2 as cv
5class BBox:
6 def __init__(self,x,y,w,h):
7 self.x = x
8 self.y = y
9 self.w = w
10 self.h = h
11def iou(a,b):
12
13 assert isinstance(a,BBox)
14 assert isinstance(b,BBox)
15
16 area_a = a.w * a.h
17 area_b = b.w * b.h
18
19 w = min(b.x+b.w,a.x+a.w) - max(a.x,b.x)
20 h = min(b.y+b.h,a.y+a.h) - max(a.y,b.y)
21
22 if w <= 0 or h <= 0:
23 return 0
24
25 area_c = w * h
26 return area_c / (area_a + area_b - area_c)
27f =io.open('results.json','r',encoding='utf-8')
28res=f.read()
29data= json.loads(res)
30f2 =io.open('all_val.json','r',encoding='utf-8')
31res2=f2.read()
32data2= json.loads(res2)
33# print(dict(data2))
34now = 1
35count = 0
36max_score = 0
37list_box = []
38dict_file_id = {}
39for i in data:
40 if i["image_id"] == now:
41 if i["score"]>max_score:
42 max_score = i["score"]
43 else:
44 list_box.append(i)#最高score的box
45 # print(i)
46 max_score = 0
47 count=0
48 now += 1
49for i in list_box:
50 # print(i)
51 for file_name in data2["images"]:
52 if i["image_id"] == file_name["id"]:
53 dict_file_id[i["image_id"]]=file_name["file_name"]
54 # print(file_name["file_name"])
55 for target in data2["annotations"]:
56 if target["image_id"]== i["image_id"]:
57 # print(target["bbox"])
58 box1 = BBox(target["bbox"][0],target["bbox"][1],target["bbox"][2],target["bbox"][3])
59 # print(i["bbox"])
60 box2 = BBox(i["bbox"][0],i["bbox"][1],i["bbox"][2],i["bbox"][3])
61 if iou(box1,box2)<0.5:
62 print(iou(box1,box2))
63 print(i["bbox"])
64 print(target["bbox"])
65 print(dict_file_id)
66 img = cv.imread('val/'+dict_file_id[i["image_id"]], cv.IMREAD_UNCHANGED)
67 cv.rectangle(img,(target["bbox"][0],target["bbox"][1]),(target["bbox"][0]+target["bbox"][2],target["bbox"][1]+target["bbox"][3]),(0,255,0),1,4)
68 cv.rectangle(img,(int(i["bbox"][0]),int(i["bbox"][1])),(int(i["bbox"][0]+i["bbox"][2]),int(i["bbox"][1]+i["bbox"][3])),(0,0,255),1,8)
69 cv.imwrite('result/'+dict_file_id[i["image_id"]],img)