我正在检测图像中的物体,并尝试使用不同的检测算法。我想比较数据集上算法的结果。我已经将每种算法的结果存储为[filepath,detection_box_coordinates,otherstuff]的列表。我想考虑两个检测是相同的,如果文件路径相同并且检测框中的重叠部分超过某个阈值。我想为任意两个算法结果A和B生成列表only_in_A,only_in_B和in_both。我希望找到一种“并且最好只有一种明显的方法”,但是到目前为止,我的搜索已经产生了几种不必要的明显方法。
由于计算in_both,我考虑过要进行以下操作:
现在,我可以看到以下与这些想法有关的缺点:
我的哪个主意是好还是坏?有什么明显的方法我想念的吗?
我认为通过进行一些修改后,检测类将是一个好主意:
class Detection():
Instances = dict()
In_A = dict()
In_B = dict()
def __new__(cls,*args,**kwargs):
if filepath in Detection.Instances.keys():
_instance = Detection.Instances[filepath]
else:
_instance = super(Detection, cls).__new__(cls, *args, **kwargs)
return _instance
def __init__(self,filepath,coords,other,From_Algo):
if From_Algo = "A"
self.filepath = filepath
self.coords_A = coords
self.in_A = True
self.other_A = other
Detection.In_A[filepath] = self # Make sure that filepath is hashable
if From_Algo = "B"
self.filepath = filepath
self.coords_B = coords
self.in_B = True
self.other_B = other
Detection.In_B[filepath] = self # Make sure that filepath is hashable
Detection.Instances[filepath]=self # Make sure that filepath is hashable
@classmethod
def Check_coord_relation(cls,A_coords,B_coords):
...
# compare A_coords and B_coords
...
return Result
@classmethod
def Get_In_Both(cls):
cls._Get_In_Both = [Det in Det for cls.Instances.values() if (hasattr(Det,"In_A") and hasattr(Det,"In_B") and cls.Check_coord_relation(Det.coords_A,coords_B))]
@classmethod
def Get_Only_In_A(cls):
cls._Only_In_A = [Det in Det for cls.In_A.values() if Det not in cls._Get_In_Both]
@classmethod
def Get_Only_In_B(cls):
cls._Only_In_B = [Det in Det for cls.In_B.values() if Det not in cls._Get_In_Both]
@classmethod
def Calc_Interseciton(cls):
cls.Get_In_Both()
cls.Get_Only_In_A()
cls.Get_Only_In_B()
您可以使用__new__来检查实例是否已经存在,因此可以更新与算法不同的属性,然后该类可以处理所有创建的实例。
首先检查两个算法中的检测,然后将它们从A和B中删除。
我无法尝试此操作,希望这对您有所帮助或为您提供新的想法。
类变量似乎不需要是字典,但是字典确实非常快。