|
45 | 45 | from openptv_python._native_compat import should_use_native |
46 | 46 | from openptv_python._native_compat import get_multimedia_par |
47 | 47 | from openptv_python._native_convert import from_native_calibration |
| 48 | +from openptv_python._native_convert import from_native_target |
48 | 49 |
|
49 | 50 | """ |
50 | 51 | example from Tracker documentation: |
@@ -590,9 +591,10 @@ def py_correspondences_proc_c(exp, frame=DEFAULT_FRAME_NUM): |
590 | 591 | short_file_bases = exp.target_filenames |
591 | 592 | print(f"short_file_bases: {short_file_bases}") |
592 | 593 | _ensure_target_output_writable(short_file_bases) |
593 | | - |
594 | | - for i_cam in range(exp.num_cams): |
595 | | - write_targets(exp.detections[i_cam], short_file_bases[i_cam], frame) |
| 594 | + native_detections = bool(exp.detections) and type(exp.detections[0]).__module__.startswith("optv.") |
| 595 | + if not native_detections: |
| 596 | + for i_cam in range(exp.num_cams): |
| 597 | + write_targets(exp.detections[i_cam], short_file_bases[i_cam], frame) |
596 | 598 |
|
597 | 599 | print( |
598 | 600 | f"Frame {frame} had {[s.shape[1] for s in sorted_pos]!r} correspondences." |
@@ -955,30 +957,44 @@ def write_targets(targets: TargetArray, short_file_base: str, frame: int) -> boo |
955 | 957 | return True # No targets to write, but file created successfully |
956 | 958 |
|
957 | 959 | try: |
| 960 | + target_list = [] |
| 961 | + is_native_targets = type(targets).__module__.startswith("optv.") |
| 962 | + if num_targets: |
| 963 | + if is_native_targets: |
| 964 | + target_list = [from_native_target(targets[index]) for index in range(num_targets)] |
| 965 | + else: |
| 966 | + target_list = [targets[index] for index in range(num_targets)] |
| 967 | + |
958 | 968 | def _value(field): |
959 | 969 | return field() if callable(field) else field |
960 | 970 |
|
961 | | - target_arr = np.array( |
962 | | - [ |
963 | | - ( |
964 | | - [ |
965 | | - _value(t.pnr), |
966 | | - *t.pos(), |
967 | | - *t.count_pixels(), |
968 | | - t.sum_grey_value(), |
969 | | - _value(t.tnr), |
970 | | - ] |
| 971 | + def _pos(target): |
| 972 | + if callable(getattr(target, "pos", None)): |
| 973 | + return target.pos() |
| 974 | + return target.x, target.y |
| 975 | + |
| 976 | + def _pixel_counts(target): |
| 977 | + if callable(getattr(target, "count_pixels", None)): |
| 978 | + return target.count_pixels() |
| 979 | + return target.n, target.nx, target.ny |
| 980 | + |
| 981 | + def _sum_grey_value(target): |
| 982 | + if callable(getattr(target, "sum_grey_value", None)): |
| 983 | + return target.sum_grey_value() |
| 984 | + return target.sumg |
| 985 | + |
| 986 | + with open(output_path, "w", encoding="utf-8") as file: |
| 987 | + file.write(f"{num_targets}\n") |
| 988 | + for target in target_list: |
| 989 | + pnr = int(_value(getattr(target, "pnr"))) |
| 990 | + x, y = _pos(target) |
| 991 | + n, nx, ny = _pixel_counts(target) |
| 992 | + sumg = int(_sum_grey_value(target)) |
| 993 | + tnr = int(_value(getattr(target, "tnr"))) |
| 994 | + file.write( |
| 995 | + f"{pnr:4d} {float(x):9.4f} {float(y):9.4f} " |
| 996 | + f"{int(n):5d} {int(nx):5d} {int(ny):5d} {sumg:5d} {tnr:5d}\n" |
971 | 997 | ) |
972 | | - for t in targets |
973 | | - ] |
974 | | - ) |
975 | | - np.savetxt( |
976 | | - output_path, |
977 | | - target_arr, |
978 | | - fmt="%4d %9.4f %9.4f %5d %5d %5d %5d %5d", |
979 | | - header=f"{num_targets}", |
980 | | - comments="", |
981 | | - ) |
982 | 998 | success = True |
983 | 999 | except OSError as exc: |
984 | 1000 | _raise_output_write_error(output_path, exc) |
|
0 commit comments