diff --git a/openptv_python/track.py b/openptv_python/track.py index 8f85419..3b85599 100644 --- a/openptv_python/track.py +++ b/openptv_python/track.py @@ -670,8 +670,8 @@ def assess_new_position( for cam in range(run.cpar.num_cams): # Convert 3D search position to 2D pixel coordinates pixel = point_to_pixel(pos, run.cal[cam], run.cpar) - print(f"pos {pos}") - print(f"pixel {pixel}") + # print(f"pos {pos}") + # print(f"pixel {pixel}") # Nearest neighbor search num_cands = candsearch_in_pix_rest( @@ -792,6 +792,7 @@ def trackcorr_c_loop(run_info, step): # 3D-position X[1] = vec_copy(curr_path_inf.x) + # print(f"X[1] {X[1]}") # use information from previous to locate new search position # and to calculate values for search area @@ -853,7 +854,7 @@ def trackcorr_c_loop(run_info, step): # print(f"X[4] {X[4]}") diff_pos = vec_subt(X[4], X[3]) - # print(f"inside kk loop {kk}") + # print(f"inside kk loop {kk}") # print(f"diff_pos {diff_pos}") if pos3d_in_bounds(diff_pos, tpar): diff --git a/openptv_python/tracking_frame_buf.py b/openptv_python/tracking_frame_buf.py index cb92720..6a2be12 100644 --- a/openptv_python/tracking_frame_buf.py +++ b/openptv_python/tracking_frame_buf.py @@ -172,6 +172,8 @@ def read_targets(file_base: str, frame_num: int) -> List[Target]: else: filename = f"{file_base}_targets" + # print(f" filename: {filename}") + try: with open(filename, "r", encoding="utf-8") as file: num_targets = int(file.readline().strip()) @@ -199,6 +201,7 @@ def read_targets(file_base: str, frame_num: int) -> List[Target]: print(f"Can't open ascii file: {filename}") raise err + # print(f" read {len(buffer)} targets from {filename}") return buffer @@ -578,26 +581,26 @@ def __init__( self.prio_file_base = prio_file_base self.target_file_base = target_file_base - def write_frame_from_start(self, frame_num): - """Write a frame to disk and advance the buffer.""" - # Write the frame to disk - frame = self.buf[0] # first frame - cor_buf = frame.correspond - path_buf = frame.path_info - num_parts = frame.num_parts - - write_path_frame( - cor_buf, - path_buf, - num_parts, - self.corres_file_base, - self.linkage_file_base, - self.prio_file_base, - frame_num, - ) - - # Advance the buffer - self.buf.appendleft(Frame(self.num_cams, MAX_TARGETS)) + # def write_frame_from_start(self, frame_num): + # """Write a frame to disk and advance the buffer.""" + # # Write the frame to disk + # frame = self.buf[0] # first frame + # cor_buf = frame.correspond + # path_buf = frame.path_info + # num_parts = frame.num_parts + + # write_path_frame( + # cor_buf, + # path_buf, + # num_parts, + # self.corres_file_base, + # self.linkage_file_base, + # self.prio_file_base, + # frame_num, + # ) + + # # Advance the buffer + # # self.buf.appendleft(Frame(self.num_cams, MAX_TARGETS)) def read_frame_at_end(self, frame_num: int, read_links: bool = False) -> None: """Read a frame from the disk and add it to the end of the buffer.""" @@ -650,7 +653,7 @@ def disk_read_frame_at_end(self, frame_num: int, read_links: bool): frame_num, ) - def disk_write_frame_from_start(self, frame_num: int): + def write_frame_from_start(self, frame_num: int): """Write the frame to the first position in the ring. Arguments: @@ -663,6 +666,7 @@ def disk_write_frame_from_start(self, frame_num: int): True on success, false on failure. """ frame = self.buf[0] # always the first one on the left + return frame.write( self.corres_file_base, self.linkage_file_base, @@ -701,7 +705,7 @@ def read_path_frame( """ fname = f"{corres_file_base}.{frame_num}" - print(fname) + # print(fname) try: filein = open(fname, "r", encoding="utf-8") diff --git a/tests/test_tracking_run.py b/tests/test_tracking_run.py index 26ce476..58f5bbb 100644 --- a/tests/test_tracking_run.py +++ b/tests/test_tracking_run.py @@ -13,18 +13,22 @@ import unittest from pathlib import Path +import numpy as np + from openptv_python.calibration import Calibration, read_calibration from openptv_python.parameters import ( read_control_par, ) from openptv_python.track import ( track_forward_start, + trackback_c, trackcorr_c_finish, trackcorr_c_loop, ) from openptv_python.tracking_run import ( tr_new, ) +from openptv_python.vec_utils import vec_norm EPS = 1e-9 @@ -92,6 +96,14 @@ def test_trackcorr_no_add(self): os.chdir(directory) print(os.path.abspath(os.curdir)) + print(Path.cwd()) + + if Path("res/").exists(): + remove_directory("res/") + + if Path("img/").exists(): + remove_directory("img/") + copy_directory("res_orig/", "res/") copy_directory("img_orig/", "img/") @@ -116,8 +128,7 @@ def test_trackcorr_no_add(self): 10000.0, ) run.tpar.add = 0 - - # print(f"run.tpar = {run.tpar}") + print(f"run.seq_par.first = {run.seq_par.first} run.seq_par.last = {run.seq_par.last}") track_forward_start(run) trackcorr_c_loop(run, run.seq_par.first) @@ -125,8 +136,77 @@ def test_trackcorr_no_add(self): for step in range(run.seq_par.first + 1, run.seq_par.last): # print(f"step = {step}") trackcorr_c_loop(run, step) - print(f"run.npart = {run.npart}") - print(f"run.nlinks = {run.nlinks}") + # print(f"run.npart = {run.npart}") + # print(f"run.nlinks = {run.nlinks}") + + trackcorr_c_finish(run, run.seq_par.last) + + + range_val = run.seq_par.last - run.seq_par.first + npart = run.npart / range_val + nlinks = run.nlinks / range_val + + self.assertAlmostEqual(npart, 0.9, delta=EPS) + self.assertAlmostEqual(nlinks, 0.8, delta=EPS) + + remove_directory("res/") + remove_directory("img/") + + os.chdir(current_directory) + + def test_trackcorr_add(self): + """Test tracking without adding particles.""" + # import os + + current_directory = Path.cwd() + print(f"working from {current_directory}") + directory = Path("tests/testing_fodder/track") + + os.chdir(directory) + + print(os.path.abspath(os.curdir)) + print(Path.cwd()) + + if Path("res/").exists(): + remove_directory("res/") + + if Path("img/").exists(): + remove_directory("img/") + + + copy_directory("res_orig/", "res/") + copy_directory("img_orig/", "img/") + + print("----------------------------") + print("Test tracking multiple files 2 cameras, 1 particle") + cpar = read_control_par("parameters/ptv.par") + + calib = read_all_calibration(cpar.num_cams) + # calib.append(Calibration()) + + run = tr_new( + "parameters/sequence.par", + "parameters/track.par", + "parameters/criteria.par", + "parameters/ptv.par", + 4, + 20000, + "res/rt_is", + "res/ptv_is", + "res/added", + calib, + 10000.0, + ) + + run.seq_par.first = 10240 + run.seq_par.last = 10250 + run.tpar.add = 1 + + track_forward_start(run) + trackcorr_c_loop(run, run.seq_par.first) + + for step in range(run.seq_par.first + 1, run.seq_par.last): + trackcorr_c_loop(run, step) trackcorr_c_finish(run, run.seq_par.last) @@ -137,11 +217,101 @@ def test_trackcorr_no_add(self): npart = run.npart / range_val nlinks = run.nlinks / range_val - self.assertAlmostEqual(npart, 1.0, delta=EPS) - self.assertAlmostEqual(nlinks, 1.0, delta=EPS) + self.assertTrue( + abs(npart - 1.0) < EPS, + f"Was expecting npart == 208/210 but found {npart}" + ) + self.assertTrue( + abs(nlinks - 0.7) < EPS, + f"Was expecting nlinks == 328/210 but found {nlinks}" + ) + os.chdir(current_directory) +class TestTrackback(unittest.TestCase): + + def test_trackback(self): + + current_directory = Path.cwd() + print(f"working from {current_directory}") + directory = Path("tests/testing_fodder/track") + + os.chdir(directory) + + print(os.path.abspath(os.curdir)) + print(Path.cwd()) + + if Path("res/").exists(): + remove_directory("res/") + + if Path("img/").exists(): + remove_directory("img/") + + + copy_directory("res_orig/", "res/") + copy_directory("img_orig/", "img/") + + print("----------------------------") + print("Test tracking multiple files 2 cameras, 1 particle") + cpar = read_control_par("parameters/ptv.par") + + calib = read_all_calibration(cpar.num_cams) + # calib.append(Calibration()) + + run = tr_new( + "parameters/sequence.par", + "parameters/track.par", + "parameters/criteria.par", + "parameters/ptv.par", + 4, + 20000, + "res/rt_is", + "res/ptv_is", + "res/added", + calib, + 10000.0, + ) + + run.seq_par.first = 10240 + run.seq_par.last = 10250 + run.tpar.add = 1 + + track_forward_start(run) + trackcorr_c_loop(run, run.seq_par.first) + + for step in range(run.seq_par.first + 1, run.seq_par.last): + trackcorr_c_loop(run, step) + + trackcorr_c_finish(run, run.seq_par.last) + + + run.tpar.dvxmin = run.tpar.dvymin = run.tpar.dvzmin = -50 + run.tpar.dvxmax = run.tpar.dvymax = run.tpar.dvzmax = 50 + + + run.lmax = vec_norm( + np.r_[ + (run.tpar.dvxmin - run.tpar.dvxmax), + (run.tpar.dvymin - run.tpar.dvymax), + (run.tpar.dvzmin - run.tpar.dvzmax) + ] + ) + + nlinks = trackback_c(run) + + print(f"nlinks = {nlinks}") + + # Uncomment the following lines to add assertions + self.assertTrue( + abs(nlinks - 1.0) < EPS, + f"Was expecting nlinks to be 1.0 but found {nlinks}" + ) + + remove_directory("res/") + remove_directory("img/") + + os.chdir(current_directory) if __name__ == "__main__": unittest.main() diff --git a/tests/test_trans_cam_point.py b/tests/test_trans_cam_point.py index ac63e0f..0afc560 100644 --- a/tests/test_trans_cam_point.py +++ b/tests/test_trans_cam_point.py @@ -39,7 +39,7 @@ def test_back_trans_point(self): # test_cal = Calibration(test_Ex, test_I, test_G, test_addp) # - test_mm = MultimediaPar(1, 1.0, (1.49, 0.0, 0.0), (5.0, 0.0, 0.0), 1.33) + test_mm = MultimediaPar(1, 1.0, [1.49, 0.0, 0.0], [5.0, 0.0, 0.0], 1.33) Ex_t = Exterior() @@ -81,7 +81,7 @@ def test_trans_cam_point(self): # ap_52 test_addp = {0., 0., 0., 0., 0., 1., 0.}; # Calibration test_cal = {test_Ex, test_I, test_G, test_addp}; - test_mm = MultimediaPar(1, 1.0, (1.49, 0.0, 0.0), (5.0, 0.0, 0.0), 1.33) + test_mm = MultimediaPar(1, 1.0, [1.49, 0.0, 0.0], [5.0, 0.0, 0.0], 1.33) Ex_t = Exterior() pos_t, cross_p, cross_c = trans_cam_point(test_Ex, test_mm, test_G, pos, Ex_t) diff --git a/tests/test_vec_utils.py b/tests/test_vec_utils.py index de2db30..18aaff1 100644 --- a/tests/test_vec_utils.py +++ b/tests/test_vec_utils.py @@ -85,7 +85,7 @@ def test_vec_cmp(): vec1 = np.array([1.0, 2.0, 3.0]) vec2 = np.array([1.0, 2.0, 3.0]) assert vec_cmp(vec1, vec2, 1e-4) - vec3 = [4.0, 5.0, 6.0] + vec3 = np.array([4.0, 5.0, 6.0]) assert not vec_cmp(vec1, vec3, 1e-4)