Skip to content

Commit 685a166

Browse files
committed
trying to fix
1 parent dd87e1d commit 685a166

3 files changed

Lines changed: 50 additions & 52 deletions

File tree

src/openptv_python/_native_convert.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,17 @@ def to_native_target_array(targets: Iterable[Target]):
253253
if not HAS_NATIVE_TARGETS or optv_tracking_framebuf is None:
254254
raise RuntimeError("optv TargetArray is not available")
255255

256-
native_targets = optv_tracking_framebuf.TargetArray()
257-
for target in targets:
258-
native_targets.append(to_native_target(target))
256+
target_count = len(targets)
257+
native_targets = optv_tracking_framebuf.TargetArray(target_count)
258+
for index in range(target_count):
259+
target = targets[index]
260+
converted_target = to_native_target(target)
261+
native_target = native_targets[index]
262+
native_target.set_pnr(int(converted_target.pnr()))
263+
native_target.set_tnr(int(converted_target.tnr()))
264+
native_target.set_pos(tuple(converted_target.pos()))
265+
native_target.set_pixel_counts(*converted_target.count_pixels())
266+
native_target.set_sum_grey_value(int(converted_target.sum_grey_value()))
259267
return native_targets
260268

261269

src/openptv_python/correspondences.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -565,32 +565,6 @@ def py_correspondences(
565565
"""
566566
num_cams = get_num_cams(cparam)
567567

568-
if should_use_native("correspondences") and HAS_NATIVE_CORRESPONDENCES and optv_correspondences is not None:
569-
native_cals = [to_native_calibration(cal) for cal in calib]
570-
native_vparam = to_native_volume_par(vparam)
571-
native_cparam = to_native_control_par(cparam)
572-
native_flat_coords = [
573-
optv_correspondences.MatchedCoords(
574-
to_native_target_array(img_pts[cam]), native_cparam, native_cals[cam]
575-
)
576-
for cam in range(num_cams)
577-
]
578-
579-
if num_cams == 1:
580-
return optv_correspondences.single_cam_correspondence(
581-
img_pts,
582-
native_flat_coords,
583-
native_cals,
584-
)
585-
586-
return optv_correspondences.correspondences(
587-
img_pts,
588-
native_flat_coords,
589-
native_cals,
590-
native_vparam,
591-
native_cparam,
592-
)
593-
594568
frm = Frame(num_cams, MAX_TARGETS)
595569

596570
# Special case of a single camera, follow the single_cam_correspondence docstring

src/pyptv/ptv.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from openptv_python._native_compat import should_use_native
4646
from openptv_python._native_compat import get_multimedia_par
4747
from openptv_python._native_convert import from_native_calibration
48+
from openptv_python._native_convert import from_native_target
4849

4950
"""
5051
example from Tracker documentation:
@@ -590,9 +591,10 @@ def py_correspondences_proc_c(exp, frame=DEFAULT_FRAME_NUM):
590591
short_file_bases = exp.target_filenames
591592
print(f"short_file_bases: {short_file_bases}")
592593
_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)
596598

597599
print(
598600
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
955957
return True # No targets to write, but file created successfully
956958

957959
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+
958968
def _value(field):
959969
return field() if callable(field) else field
960970

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"
971997
)
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-
)
982998
success = True
983999
except OSError as exc:
9841000
_raise_output_write_error(output_path, exc)

0 commit comments

Comments
 (0)