Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: minor code cleanup #39

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 23 additions & 41 deletions element_facemap/facemap_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,10 @@ class BodyPartPosition(dj.Part):
"""

def make(self, key):
""".populate() method will launch training for each FacemapInferenceTask"""
"""
Calls facemap.pose.Pose to run pose estimation on the video files using the specified model.
Video files and model are specified in the FacemapInferenceTask table.
"""
# ID model and directories
task_mode, output_dir = (FacemapInferenceTask & key).fetch1(
"task_mode", "facemap_inference_output_dir"
Expand Down Expand Up @@ -353,27 +356,9 @@ def make(self, key):
full_metadata_path = output_dir / f"{vid_name}_FacemapPose_metadata.pkl"

# Load or Trigger Facemap Pose Estimation Inference
if (
if task_mode == "trigger" and not (
facemap_result_path.exists() & full_metadata_path.exists()
) or task_mode == "load": # Load results and do not rerun processing
(
body_part_position_entry,
inference_duration,
total_frame_count,
creation_time,
) = _load_facemap_results(key, facemap_result_path, full_metadata_path)
self.insert1(
{
**key,
"inference_completion_time": creation_time,
"inference_run_duration": inference_duration,
"total_frame_count": total_frame_count,
}
)
self.BodyPartPosition.insert(body_part_position_entry)
return

elif task_mode == "trigger":
):
from facemap.pose import pose as facemap_pose, model_loader

bbox = (FacemapInferenceTask & key).fetch1("bbox") or []
Expand All @@ -382,9 +367,10 @@ def make(self, key):
facemap_model_name = (
FacemapModel.File & f'model_id="{key["model_id"]}"'
).fetch1("model_file")

facemap_model_path = Path.cwd() / facemap_model_name
# copy this model file to the facemap model root directory (~/.facemap/models/)
models_root_dir = model_loader.get_models_dir()
shutil.copy(facemap_model_path, models_root_dir)

# Create Symbolic Links to raw video data files from outbox directory
video_symlinks = []
Expand All @@ -395,9 +381,6 @@ def make(self, key):
video_symlink.symlink_to(video_file)
video_symlinks.append(video_symlink.as_posix())

# copy this model file to the facemap model root directory (~/.facemap/models/)
shutil.copy(facemap_model_path, models_root_dir)

# Instantiate Pose object, with filenames specified as video files, and bounding specified in params
# Assumes GUI to be none as we are running CLI implementation
pose = facemap_pose.Pose(
Expand All @@ -408,21 +391,21 @@ def make(self, key):
)
pose.run()

(
body_part_position_entry,
inference_duration,
total_frame_count,
creation_time,
) = _load_facemap_results(key, facemap_result_path, full_metadata_path)
self.insert1(
{
**key,
"inference_completion_time": creation_time,
"inference_run_duration": inference_duration,
"total_frame_count": total_frame_count,
}
)
self.BodyPartPosition.insert(body_part_position_entry)
(
body_part_position_entry,
inference_duration,
total_frame_count,
creation_time,
) = _load_facemap_results(key, facemap_result_path, full_metadata_path)
self.insert1(
{
**key,
"inference_completion_time": creation_time,
"inference_run_duration": inference_duration,
"total_frame_count": total_frame_count,
}
)
self.BodyPartPosition.insert(body_part_position_entry)

@classmethod
def get_trajectory(cls, key: dict, body_parts: list = "all") -> pd.DataFrame:
Expand Down Expand Up @@ -468,7 +451,6 @@ def get_trajectory(cls, key: dict, body_parts: list = "all") -> pd.DataFrame:

def _load_facemap_results(key, facemap_result_path, full_metadata_path):
"""Load facemap results from h5 and metadata files."""

from facemap import utils

with open(full_metadata_path, "rb") as f:
Expand Down
Loading