python篇—实现的Iou与Giou代码
import json
import torch
li1 = [{
"1"}, {
"2"}, {
"3"}, {
"4"}, {
"5"}, {
"6"}]
li = [{
'id': 12443, 'uuid': '988ff6eb-5733-4658-b864-132337ec927c', 'url': '/home/slife/lq/data/source/2023-03-23/CardNO/0005/2023-03-23-07-42-36.jpeg', 'device': '0005', 'imgtime': '2023-03-23 07:42:36', 'platenumber': '沪E88719', 'result': '{\n "cls": [\n "3"\n ],\n "pro": [\n 0.774\n ],\n "x": [\n 1352\n ],\n "y": [\n 607\n ],\n "w": [\n 247\n ],\n "h": [\n 127\n ]\n}', 'flag': '0', 'pro': 0.774, 'img': '/workspace/data/images/2023-03-23/box_imgs/0005/2023-03-23-07-42-36.jpeg'}, {
'id': 12444, 'uuid': '8a4ffae6-6fc7-4b96-b44a-d9aa811693d9', 'url': '/home/slife/lq/data/source/2023-03-23/CardNO/0005/2023-03-23-07-42-40.jpeg', 'device': '0005', 'imgtime': '2023-03-23 07:42:40', 'platenumber': '沪E88719', 'result': '{\n "cls": [\n "3"\n ],\n "pro": [\n 0.811\n ],\n "x": [\n 1349\n ],\n "y": [\n 606\n ],\n "w": [\n 248\n ],\n "h": [\n 127\n ]\n}', 'flag': '0', 'pro': 0.811, 'img': '/workspace/data/images/2023-03-23/box_imgs/0005/2023-03-23-07-42-40.jpeg'}, {
'id': 12445, 'uuid': '0a2f6e29-99ea-4050-9624-64e6dbb2a718', 'url': '/home/slife/lq/data/source/2023-03-23/CardNO/0005/2023-03-23-07-43-20.jpeg', 'device': '0005', 'imgtime': '2023-03-23 07:43:20', 'platenumber': '沪E88719', 'result': '{\n "cls": [\n "3"\n ],\n "pro": [\n 0.82\n ],\n "x": [\n 1353\n ],\n "y": [\n 606\n ],\n "w": [\n 248\n ],\n "h": [\n 128\n ]\n}', 'flag': '0', 'pro': 0.82, 'img': '/workspace/data/images/2023-03-23/box_imgs/0005/2023-03-23-07-43-20.jpeg'}, {
'id': 12446, 'uuid': 'd911b763-4daf-4c14-a7f5-45cb145ba578', 'url': '/home/slife/lq/data/source/2023-03-23/CardNO/0005/2023-03-23-07-43-24.jpeg', 'device': '0005', 'imgtime': '2023-03-23 07:43:24', 'platenumber': '沪E88719', 'result': '{\n "cls": [\n "3"\n ],\n "pro": [\n 0.516\n ],\n "x": [\n 1352\n ],\n "y": [\n 607\n ],\n "w": [\n 245\n ],\n "h": [\n 123\n ]\n}', 'flag': '0', 'pro': 0.516, 'img': '/workspace/data/images/2023-03-23/box_imgs/0005/2023-03-23-07-43-24.jpeg'}]
def box_iou(rec1, rec2):
x1, y1, x2, y2 = rec1
x3, y3, x4, y4 = rec2
area_1 = (x2 - x1) * (y2 - y1)
area_2 = (x4 - x3) * (y4 - y3)
sum_area = area_1 + area_2
w1 = x2 - x1
w2 = x4 - x3
h1 = y2 - y1
h2 = y4 - y3
w = min(x1, x2, x3, x4) + w1 + w2 - max(x1, x2, x3, x4)
h = min(y1, y2, y3, y4) + h1 + h2 - max(y1, y2, y3, y4)
area = w * h
iou = area / (sum_area - area)
return iou
def box_giou(rec1, rec2):
x1, y1, x2, y2 = rec1
x3, y3, x4, y4 = rec2
iou = box_iou(rec1, rec2)
area_c = (max(x1, x2, x3, x4) - min(x1, x2, x3, x4)) * (max(y1, y2, y3, y4) - min(y1, y2, y3, y4))
area_1 = (x2 - x1) * (y2 - y1)
area_2 = (x4 - x3) * (y4 - y3)
sum_area = area_1 + area_2
w1 = x2 - x1
w2 = x4 - x3
h1 = y2 - y1
h2 = y4 - y3
w = min(x1, x2, x3, x4) + w1 + w2 - max(x1, x2, x3, x4)
h = min(y1, y2, y3, y4) + h1 + h2 - max(y1, y2, y3, y4)
area = w * h
add_area = sum_area - area
end_area = (area_c - add_area) / area_c
giou = iou - end_area
return giou
def calculate_giou(box_1, box_2):
"""
calculate giou
:param box_1: (x0, y0, x1, y1)
:param box_2: (x0, y0, x1, y1)
:return: value of giou
"""
area_1 = (box_1[2] - box_1[0]) * (box_1[3] - box_1[1])
area_2 = (box_2[2] - box_2[0]) * (box_2[3] - box_2[1])
area_c = (max(box_1[2], box_2[2]) - min(box_1[0], box_2[0])) * (max(box_1[3], box_2[3]) - min(box_1[1], box_2[1]))
top = max(box_1[0], box_2[0])
left = max(box_1[1], box_2[1])
bottom = min(box_1[2], box_2[2])
right = min(box_1[3], box_2[3])
area_intersection = (right - left) * (bottom - top)
area_union = area_1 + area_2 - area_intersection
iou = float(area_intersection) / area_union
giou = iou - float((area_c - area_union)) / area_c
return giou
if __name__ == '__main__':
for j in zip(li[:-1], li[1:]):
result1 = json.loads(j[0]["result"])
print(result1)
result2 = json.loads(j[1]["result"])
print(result2)
if len(result1["cls"]) == len(result2["cls"]):
box_li1 = []
for x in range(len(result1["cls"])):
box1 = result1["x"][x], result1["y"][x], result1["x"][x] + result1["w"][x], result1["y"][x] + result1["h"][x]
box_li1.append(box1)
print("box_li1", box_li1)
box_li2 = []
for y in range(len(result2["cls"])):
box2 = result2["x"][y], result2["y"][y], result2["x"][y] + result2["w"][y], result2["y"][y] + result2["h"][y]
box_li2.append(box2)
print("box_li2", box_li2)
giou = box_giou(box_li1[0], box_li2[0])
print(giou)
print("*" * 50)
giou1 = calculate_giou(box_li1[0], box_li2[0])
print(giou1)
print("==" * 50)
实现两两比较
li1 = [{
"1"}, {
"2"}, {
"3"}, {
"4"}, {
"5"}, {
"6"}]
for i in zip(li1[:-1], li1[1:]):
print(i)