我认为 Detection 类进行一些修改会是一个好主意:
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 中删除它们。
我无法尝试这个,我希望这会对你有所帮助或给你新的想法。
类变量似乎不需要是字典,但字典真的很快。