Dropping more old python code.

This commit is contained in:
Daniel
2025-01-28 11:50:50 +01:00
parent a674811fef
commit 1cc71cce57
5 changed files with 102 additions and 699 deletions

View File

@ -1,28 +1,33 @@
import copy
import json
import os
import sys
import time
import matplotlib
import numpy as np
import utils_2d_pose
import utils_pipeline
from skelda import utils_pose, utils_view
sys.path.append("/RapidPoseTriangulation/swig/")
import rpt
from skelda.writers import json_writer
# ==================================================================================================
filepath = os.path.dirname(os.path.realpath(__file__)) + "/"
test_img_dir = filepath + "../data/"
whole_body = {
"foots": False,
"face": False,
"hands": False,
}
config = {
"min_match_score": 0.94,
"min_group_size": 1,
"min_bbox_score": 0.3,
"min_bbox_area": 0.1 * 0.1,
"batch_poses": True,
"whole_body": whole_body,
"take_interval": 1,
}
joint_names_2d = utils_pipeline.get_joint_names(whole_body)
joint_names_3d = list(joint_names_2d)
@ -40,9 +45,15 @@ def update_sample(sample, new_dir=""):
]
# Add placeholders for missing keys
sample["cameras_color"] = sample["cameras"]
sample["imgpaths_color"] = sample["imgpaths"]
sample["cameras_depth"] = []
if not "scene" in sample:
sample["scene"] = "default"
if not "id" in sample:
sample["id"] = "0"
if not "index" in sample:
sample["index"] = 0
for cam in sample["cameras"]:
if not "type" in cam:
cam["type"] = "pinhole"
return sample
@ -51,10 +62,6 @@ def update_sample(sample, new_dir=""):
def main():
if any((whole_body[k] for k in whole_body)):
kpt_model = utils_2d_pose.load_wb_model()
else:
kpt_model = utils_2d_pose.load_model(min_bbox_score=0.3)
# Manually set matplotlib backend
matplotlib.use("TkAgg")
@ -74,68 +81,58 @@ def main():
sample = json.load(file)
sample = update_sample(sample, dirpath)
camparams = sample["cameras_color"]
if len(sample["imgpaths"]) == 1:
# At least two images are required
continue
# Save dataset
labels = [sample]
tmp_export_dir = "/tmp/rpt/"
for label in labels:
if "splits" in label:
label.pop("splits")
json_writer.save_dataset(labels, tmp_export_dir)
# Save config
config_path = tmp_export_dir + "config.json"
utils_pipeline.save_json(config, config_path)
# Call the CPP binary
os.system("/RapidPoseTriangulation/scripts/test_skelda_dataset.bin")
# Load the results
print("Loading exports ...")
res_path = tmp_export_dir + "results.json"
results = utils_pipeline.load_json(res_path)
poses_3d = results["all_poses_3d"][0]
poses_2d = results["all_poses_2d"][0]
joint_names_3d = results["joint_names_3d"]
# Visualize the 2D results
fig1 = utils_view.draw_many_images(
sample["imgpaths"], [], [], poses_2d, joint_names_2d, "2D detections"
)
fig1.savefig(os.path.join(dirpath, "2d-k.png"), dpi=fig1.dpi)
# Visualize the 3D results
print("Detected 3D poses:")
poses_3d = np.array(poses_3d)
print(poses_3d.round(3))
if len(poses_3d) == 0:
utils_view.show_plots()
continue
camparams = sample["cameras"]
roomparams = {
"room_size": sample["room_size"],
"room_center": sample["room_center"],
}
# Load color images
images_2d = []
for i in range(len(sample["cameras_color"])):
imgpath = sample["imgpaths_color"][i]
img = utils_pipeline.load_image(imgpath)
img = utils_pipeline.rgb2bayer(img)
img = utils_pipeline.bayer2rgb(img)
images_2d.append(img)
# Get 2D poses
stime = time.time()
poses_2d = utils_2d_pose.get_2d_pose(kpt_model, images_2d)
poses_2d = utils_pipeline.update_keypoints(poses_2d, joint_names_2d, whole_body)
print("2D time:", time.time() - stime)
# print([np.array(p).round(6).tolist() for p in poses_2d])
fig1 = utils_view.draw_many_images(
sample["imgpaths_color"], [], [], poses_2d, joint_names_2d, "2D detections"
)
fig1.savefig(os.path.join(dirpath, "2d-k.png"), dpi=fig1.dpi)
# draw_utils.utils_view.show_plots()
if len(images_2d) == 1:
utils_view.show_plots()
continue
# Get 3D poses
if sum(np.sum(p) for p in poses_2d) == 0:
poses3D = np.zeros([1, len(joint_names_3d), 4])
poses2D = np.zeros([len(images_2d), 1, len(joint_names_3d), 3])
else:
cameras = rpt.convert_cameras(camparams)
roomp = [roomparams["room_size"], roomparams["room_center"]]
triangulator = rpt.Triangulator(min_match_score=0.94)
stime = time.time()
poses_3d = triangulator.triangulate_poses(
poses_2d, cameras, roomp, joint_names_2d
)
poses3D = np.array(poses_3d)
if len(poses3D) == 0:
poses3D = np.zeros([1, len(joint_names_3d), 4])
print("3D time:", time.time() - stime)
poses2D = []
for cam in camparams:
poses_2d, _ = utils_pose.project_poses(poses3D, cam)
poses2D.append(poses_2d)
print(poses3D)
# print(poses2D)
# print(poses3D.round(3).tolist())
fig2 = utils_view.draw_poses3d(poses3D, joint_names_3d, roomparams, camparams)
poses_2d_proj = []
for cam in camparams:
poses_2d_cam, _ = utils_pose.project_poses(poses_3d, cam)
poses_2d_proj.append(poses_2d_cam)
fig2 = utils_view.draw_poses3d(poses_3d, joint_names_3d, roomparams, camparams)
fig3 = utils_view.draw_many_images(
sample["imgpaths_color"], [], [], poses2D, joint_names_3d, "2D projections"
sample["imgpaths"], [], [], poses_2d_proj, joint_names_3d, "2D projections"
)
fig2.savefig(os.path.join(dirpath, "3d-p.png"), dpi=fig2.dpi)
fig3.savefig(os.path.join(dirpath, "2d-p.png"), dpi=fig3.dpi)