YOLO 用 Python 來計算 IOU

2021-08-02 01:43:23 字數 1390 閱讀 9314

在做yolo目標檢測相關的專案,裡面涉及到計算iou,可以理解為系統**出來的框與原來中標記的框的重合程度。 因為yolo的實現是用tensorflow實現的,而我又要單獨列出來,所以就打算用python來計算 iou。

iou的計算這個問題,其實我們可以轉化成兩個矩形框的重合程度,那麼

這裡有個難點,如何判斷是否重合。我的理解是分別比較兩個矩形的重心在x軸方向上和y軸方向上的距離與兩個矩形的長或者寬的一半的和的大小。如果重心的在x軸和y軸上的距離都比他們邊長和的一半要小就符合相交的條件。

第二個就是計算重合程度,那麼我們採用的是 iou = 相交的面積 /(兩個框的面積和 - 相交的面積)。這裡相交的面積的計算,需要我們知道相交面積的左上角頂點和右下角頂點。這兩個頂點,我們直接比較橫縱座標就可以求出來,比較簡單就不贅述。

那麼下面我就展示我的**了。

def calciou(one_x, one_y, one_w, one_h, two_x, two_y, two_w, two_h):

if((abs(one_x - two_x) < ((one_w + two_w) / 2.0)) and (abs(one_y - two_y) < ((one_h + two_h) / 2.0))):

lu_x_inter = max((one_x - (one_w / 2.0)), (two_x - (two_w / 2.0)))

lu_y_inter = min((one_y + (one_h / 2.0)), (two_y + (two_h / 2.0)))

rd_x_inter = min((one_x + (one_w / 2.0)), (two_x + (two_w / 2.0)))

rd_y_inter = max((one_y - (one_h / 2.0)), (two_y - (two_h / 2.0)))

inter_w = abs(rd_x_inter - lu_x_inter)

inter_h = abs(lu_y_inter - rd_y_inter)

inter_square = inter_w * inter_h

union_square = (one_w * one_h) + (two_w * two_h) - inter_square

calciou = inter_square / union_square * 1.0

print("calciou:", calciou)

else:

print("no intersection!")

return calciou

def main():

calciou(1, 2, 2, 2, 2, 1, 2, 2)

if __name__ == '__main__':

main()

python計算iv值 python計算IV值

1.基於jupyter notebook 導包import numpy as np import math import pandas as pd from sklearn.utils.multiclass import type of target from scipy import stats ...

python計算iv值 python計算IV值

1.基於jupyter notebook 導包import numpy as np import math import pandas as pd from sklearn.utils.multiclass import type of target from scipy import stats ...

python程式計算 用Python計算N個程式

我需要計算包含如下文字的文字檔案的unigrams bigrams和trigrams 僅在美國,囊性纖維化就影響了30000名兒童和年輕人 吸入鹽水的霧氣可以減少充滿囊性纖維化患者氣道的膿液和感染,儘管 包括劇烈的咳嗽和難聞的味道。這是本週出版的 新英格蘭醫學雜誌 上發表的兩項研究的結論。我從pyt...