def fakekalman(): #read coords gt = h.readmaarray(clippath + "/groundtruth/gt")[:,2:6] statsinfo = np.load(macropath + "pre/statsdata.npy") gtcen = h.corcen(gt) fakebboxcen = h.createfakedet(gtcen,statsinfo[1,1,:]) fakebboxcor = h.cencor(fakebboxcen) stdvalues = [0.01,5,25] for i in range(len(stdvalues)): #for i in range(0, bbox.shape[0], 1): # bbox.mask[i,:] = True memcen,memk, memp = kalman(fakebboxcen,gtcen, statsinfo, stdvalues[i]) memcor = h.cencor(memcen) memioubefore = h.iouclip(fakebboxcor,gt) memiouafter = h.iouclip(memcor, gt) print(np.mean(memioubefore)) print(np.mean(memiouafter)) ylabel = ["Mittelpunktx [px]","Mittelpunkty [px]","Breite [px]","Höhe [px]"] h.timeplot3(gtcen,fakebboxcen, memcen, "-results", [[0,1920],[1080,0],[0,400],[0,2]], ylabel, ["Frame [k]"]*4, savepath + clipname + "fakedetectionsstd" + str(stdvalues[i]) ) h.plotiou(gt, memcor, ["IoU"], ["Frame [k]"], savepath + clipname + "fakedetectionsstd" + str(stdvalues[i]) + "iouot") h.errorplot(gt, memcor,[1,1,1,1], ylabel, ["Frame [k]"] *4, savepath + clipname + "fakedetectionsstd" + str(stdvalues[i]) + "error") h.viskal(fakebboxcor,gt,memcor,clippath, savepath + clipname + "fakedetectionsQ" + str(stdvalues[i]))
def creategt(path, savepath): #extract vidname vidname = path.split("/")[3] #extract imagename samplelist = os.listdir(path) pathlist = [] for i in range(len(samplelist)): if samplelist[i].split(".")[-1] == "txt": pathlist.append(samplelist[i]) #create masked array gt = ma.array(np.zeros((len(pathlist), 6)), mask=True, dtype=np.float64) for i in range(len(pathlist)): with open(path + pathlist[i], "r") as text: listtext = list(text) comp = [] for j in range(len(listtext)): comp.append(listtext[j].split(" ")[1]) #select bbox by selecting max centerx indx = comp.index(max(comp)) #select relevant row relrow = list(map(float, listtext[indx].split(" "))) dimx = 1920 dimy = 1080 label = relrow[0] centerx = relrow[1] * dimx centery = relrow[2] * dimy width = relrow[3] * dimx height = relrow[4] * dimy #class gt[i, 0] = label #confidence gt[i, 1] = 1 #coords in "cor" format gt[i, 2], gt[i, 3], gt[i, 4], gt[i, 5] = centerx, centery, width, height gt[:, 2:6] = h.cencor(gt[:, 2:6]) h.writemaarray(gt, savepath + "gtsynthetic/" + vidname, "gt from synthetic data")
def kalclip(bbox, gt, model, clippath, stats, ofpoint=0, R=500, Q=500, P=0): #define start(no need to initiate before detections) start = h.getstartindex(bbox) clipname = h.extract(clippath) #for 30fps --> 1/fps #irrelevant in my case --> set 1 (i dont rly make a reference to reality here) deltat = 1 #define what model has what representation for retransformation after: correp = [] cenrep = ["cenof", "simplecen"] asprep = ["aspof", "aspofwidth", "aspofwidthman"] std = 70 #define dimensions and model(depends on representation) if model == "simplecen": #transform representation bbox = h.corcen(bbox) gt = h.corcen(gt) #set parameters n_x, n_z = 4, 4 Kal = KalmanFilter(dim_x=n_x, dim_z=n_z) Kal.F = np.eye(n_x) Kal.H = np.zeros((n_z, n_x)) Kal.H[0][0] = Kal.H[1][1] = Kal.H[2][2] = Kal.H[3][3] = 1 Kal.Q = np.eye(n_x) * std**2 for i in range(len(stats)): Kal.R[i, i] = stats[0, 1, i]**2 #init init = np.zeros(n_x) init[:] = gt[0, :] memp = np.zeros((bbox.shape[0], n_x)) memk = np.zeros((bbox.shape[0], n_x)) elif model == "cenof": #transform representation bbox = h.corcen(bbox) gt = h.corcen(gt) #set parameters n_x, n_z, n_u = 4, 4, 2 Kal = KalmanFilter(dim_x=n_x, dim_z=n_z, dim_u=n_u) Kal.R = np.eye(n_z) * R for i in range(len(stats)): Kal.R[i, i] = stats[1, 1, i]**2 Kal.Q = np.eye(n_x) * std**2 Kal.Q[0, 0], Kal.Q[1, 1] = stats[3, 1, 0], stats[3, 1, 1] Kal.P *= P Kal.F = np.eye(n_x) Kal.H = np.zeros((n_z, n_x)) Kal.H[0, 0] = Kal.H[1, 1] = Kal.H[2, 2] = Kal.H[3, 3] = 1 Kal.B = np.eye(n_x, n_u) #init init = np.zeros(n_x) init[:4] = gt[0, :] memp = np.zeros((bbox.shape[0], n_x)) memk = np.zeros((bbox.shape[0], n_x)) elif model == "aspof": #transform representation bbox = h.corasp(bbox) gt = h.corasp(gt) #set parameters n_x, n_z, n_u = 4, 4, 2 Kal = KalmanFilter(dim_x=n_x, dim_z=n_z, dim_u=n_u) Kal.R = np.eye(n_z) * R for i in range(len(stats)): Kal.R[i, i] = stats[2, 1, i]**2 Kal.Q = np.eye(n_x) * std**2 Kal.Q[0, 0], Kal.Q[1, 1], Kal.Q[3, 3] = stats[3, 1, 0]**2, stats[3, 1, 1]**2, 0.0001 Kal.P *= P Kal.F = np.eye(n_x) Kal.B = np.eye(n_x, n_u) Kal.H = np.zeros((n_z, n_x)) Kal.H[0, 0] = Kal.H[1, 1] = Kal.H[2, 2] = Kal.H[3, 3] = 1 #init init = np.zeros(n_x) init[:4] = gt[0, :] memp = np.zeros((bbox.shape[0], n_x)) memk = np.zeros((bbox.shape[0], n_x)) elif model == "aspofwidth": #transform representation bbox = h.corasp(bbox) gt = h.corasp(gt) #set parameters n_x, n_z, n_u = 4, 4, 3 Kal = KalmanFilter(dim_x=n_x, dim_z=n_z, dim_u=n_u) Kal.R = np.eye(n_z) * R for i in range(len(stats)): Kal.R[i, i] = stats[2, 1, i]**2 Kal.Q = np.eye(n_x) * std**2 #TODO put in stats for std width from of Kal.Q[0, 0], Kal.Q[1, 1], Kal.Q[2, 2], Kal.Q[3, 3] = stats[ 3, 1, 0]**2, stats[3, 1, 1]**2, stats[3, 1, 2]**2, 0.0001 Kal.P *= P Kal.F = np.eye(n_x) Kal.B = np.eye(n_x, n_u) Kal.H = np.zeros((n_z, n_x)) Kal.H[0, 0] = Kal.H[1, 1] = Kal.H[2, 2] = Kal.H[3, 3] = 1 #init init = np.zeros(n_x) init[:4] = gt[0, :] memp = np.zeros((bbox.shape[0], n_x)) memk = np.zeros((bbox.shape[0], n_x)) elif model == "aspofwidthman": #transform representation bbox = h.corasp(bbox) gt = h.corasp(gt) #set parameters n_x, n_z, n_u = 4, 4, 3 Kal = KalmanFilter(dim_x=n_x, dim_z=n_z, dim_u=n_u) Kal.R = np.eye(n_z) * R for i in range(len(stats)): Kal.R[i, i] = stats[2, 1, i]**2 Kal.Q = np.eye(n_x) * std**2 Kal.Q[0, 0], Kal.Q[1, 1], Kal.Q[2, 2], Kal.Q[3, 3] = stats[ 3, 1, 0], stats[3, 1, 1], 1**2, 0.0001 Kal.P *= P Kal.F = np.eye(n_x) Kal.B = np.eye(n_x, n_u) Kal.H = np.zeros((n_z, n_x)) Kal.H[0, 0] = Kal.H[1, 1] = Kal.H[2, 2] = Kal.H[3, 3] = 1 #init init = np.zeros(n_x) init[:4] = gt[0, :] memp = np.zeros((bbox.shape[0], n_x)) memk = np.zeros((bbox.shape[0], n_x)) #parameters to define(try these on all representations) Kal.x = init #create new array for results of the algorithm mem = ma.array(np.zeros_like(bbox), mask=True) mem[0, :] = init for j in range(memp.shape[1]): memp[0, j] = Kal.P[j, j] for j in range(memk.shape[1]): memk[0, j] = Kal.K[j, j] for i in range(1, bbox.shape[0], 1): z = bbox[i, :] if bbox.mask[i, 0] == True: z = None if model == "cenof": vx = ofpoint[i, 0] - ofpoint[i - 1, 0] vy = ofpoint[i, 1] - ofpoint[i - 1, 1] u = np.array([vx, vy]) Kal.predict(u) elif model == "aspof": vx = ofpoint[i, 0] - ofpoint[i - 1, 0] vy = ofpoint[i, 1] - ofpoint[i - 1, 1] u = np.array([vx, vy]) Kal.predict(u) elif model == "aspofwidth": vx = ofpoint[i, 0] - ofpoint[i - 1, 0] vy = ofpoint[i, 1] - ofpoint[i - 1, 1] vwidth = ofpoint[i, 2] - ofpoint[i - 1, 2] u = np.array([vx, vy, vwidth]) Kal.predict(u) elif model == "aspofwidthman": vx = ofpoint[i, 0] - ofpoint[i - 1, 0] vy = ofpoint[i, 1] - ofpoint[i - 1, 1] vwidth = ofpoint[i, 2] - ofpoint[i - 1, 2] u = np.array([vx, vy, vwidth]) Kal.predict(u) else: Kal.predict() Kal.update(z) x = Kal.x y = x[:4] #write into resulting array mem[i, :] = y[:4] for j in range(memp.shape[1]): memp[i, j] = Kal.P[j, j] for j in range(memk.shape[1]): memk[i, j] = Kal.K[j, j] #just return result --> do back-transformation into other representations outside of this function(as well as creating visuals) if model in correp: mem = mem elif model in cenrep: mem = h.cencor(mem) elif model in asprep: mem = h.aspcor(mem) return mem, memp, memk
def onlydetkalman(): textpath = "drawmaininfo1" #load info for clips mainvidpath, info = h.readtimes(textpath) mainvid = mainvidpath.split(".")[-2] mainvid = mainvid.split("/")[-1] datapath = "./data/" macropath = datapath + mainvid + "/macroanalysis/" savepathbase = "./gen/kalman/" #delete folders #create folders for i in range(np.shape(info)[0]): h.makedirsx(savepathbase + info[i][2]) stdvalues = [1, 9, 81] statsinfo = np.load(macropath + "pre/statsdata.npy") memiou = [] for i in range(len(stdvalues)): memiou.append([i]) for i in range(np.shape(info)[0]): savepath = savepathbase + info[i][2] + "/" clipname = info[i][2] clippath = datapath + mainvid + "/" + clipname #read coords gt = h.readmaarray(clippath + "/groundtruth/gt")[:,2:6] bbox = h.readmaarray(clippath + "/detections/cleancorr") #define start and end where detectios appear start = h.firstindex(bbox) end = h.lastindex(bbox) #change gt and bbox to start at first detection gt = gt[start:,:] bbox = bbox[start:,:] #transform to coordinates that are used in experiment gtcen = h.corcen(gt) bboxcen = h.corcen(bbox) #loop over std values to test each for j in range(len(stdvalues)): #for i in range(0, bbox.shape[0], 1): # bbox.mask[i,:] = True memcen,memk, memp = kalman(bboxcen,gtcen, statsinfo, stdvalues[j]) memcor = h.cencor(memcen) memioubefore = h.iouclip(bbox,gt) memiouafter = h.iouclip(memcor, gt) print(np.mean(memioubefore)) print(np.mean(memiouafter)) memiou[j].extend(h.iouclip(memcor, gt)) ylabel = ["Mittelpunktx [px]","Mittelpunkty [px]","Breite [px]","Höhe [px]"] h.timeplot3(gtcen,bboxcen, memcen, "-results", [[0,1920],[1080,0],[0,400],[0,2]], ylabel, ["Frame [k]"]*4, savepath + clipname + "fakedetectionsstd" + str(stdvalues[j]) ) h.plotiou(gt, memcor, ["IoU"], ["Frame [k]"], savepath + clipname + "fakedetectionsstd" + str(stdvalues[j]) + "iouot") h.errorplot(gt, memcor,[1,1,1,1], ylabel, ["Frame [k]"] *4, savepath + clipname + "fakedetectionsstd" + str(stdvalues[j]) + "error") h.viskal(bbox,gt,memcor,clippath, savepath + clipname + "bbox" + str(stdvalues[j]), start = start) for j in range(len(stdvalues)): np.savetxt(savepathbase + "std" + str(stdvalues[j]) + "iou.txt", np.array(memiou[j][1:]),fmt = "%1.2f",header ="iou of all clips(compared at places where yolo originally detected a bbox)") np.savetxt(savepathbase + "std" + str(stdvalues[j]) + "iouavg.txt", [np.array(memiou[j][1:]).mean(),np.array(memiou[j][1:]).std()],fmt = "%1.2f",header ="iou of all clips(compared at places where yolo originally detected a bbox)")