Skip to content

Commit a491f42

Browse files
authored
Merge pull request #40 from ttngu207/main_external-storage
Use external storage: store result files as filepath@store
2 parents 5465016 + 709fb47 commit a491f42

File tree

2 files changed

+58
-38
lines changed

2 files changed

+58
-38
lines changed

element_facemap/facemap_inference.py

+39-38
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,19 @@ class BodyPartPosition(dj.Part):
323323
likelihood : longblob # model evaluated likelihood
324324
"""
325325

326+
class File(dj.Part):
327+
definition = """
328+
-> master
329+
file_name: varchar(255)
330+
---
331+
file: filepath@facemap-processed
332+
"""
333+
326334
def make(self, key):
327-
""".populate() method will launch training for each FacemapInferenceTask"""
335+
"""
336+
Calls facemap.pose.Pose to run pose estimation on the video files using the specified model.
337+
Video files and model are specified in the FacemapInferenceTask table.
338+
"""
328339
# ID model and directories
329340
task_mode, output_dir = (FacemapInferenceTask & key).fetch1(
330341
"task_mode", "facemap_inference_output_dir"
@@ -353,27 +364,9 @@ def make(self, key):
353364
full_metadata_path = output_dir / f"{vid_name}_FacemapPose_metadata.pkl"
354365

355366
# Load or Trigger Facemap Pose Estimation Inference
356-
if (
367+
if task_mode == "trigger" and not (
357368
facemap_result_path.exists() & full_metadata_path.exists()
358-
) or task_mode == "load": # Load results and do not rerun processing
359-
(
360-
body_part_position_entry,
361-
inference_duration,
362-
total_frame_count,
363-
creation_time,
364-
) = _load_facemap_results(key, facemap_result_path, full_metadata_path)
365-
self.insert1(
366-
{
367-
**key,
368-
"inference_completion_time": creation_time,
369-
"inference_run_duration": inference_duration,
370-
"total_frame_count": total_frame_count,
371-
}
372-
)
373-
self.BodyPartPosition.insert(body_part_position_entry)
374-
return
375-
376-
elif task_mode == "trigger":
369+
):
377370
from facemap.pose import pose as facemap_pose, model_loader
378371

379372
bbox = (FacemapInferenceTask & key).fetch1("bbox") or []
@@ -382,9 +375,10 @@ def make(self, key):
382375
facemap_model_name = (
383376
FacemapModel.File & f'model_id="{key["model_id"]}"'
384377
).fetch1("model_file")
385-
386378
facemap_model_path = Path.cwd() / facemap_model_name
379+
# copy this model file to the facemap model root directory (~/.facemap/models/)
387380
models_root_dir = model_loader.get_models_dir()
381+
shutil.copy(facemap_model_path, models_root_dir)
388382

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

398-
# copy this model file to the facemap model root directory (~/.facemap/models/)
399-
shutil.copy(facemap_model_path, models_root_dir)
400-
401392
# Instantiate Pose object, with filenames specified as video files, and bounding specified in params
402393
# Assumes GUI to be none as we are running CLI implementation
403394
pose = facemap_pose.Pose(
@@ -408,21 +399,32 @@ def make(self, key):
408399
)
409400
pose.run()
410401

411-
(
412-
body_part_position_entry,
413-
inference_duration,
414-
total_frame_count,
415-
creation_time,
416-
) = _load_facemap_results(key, facemap_result_path, full_metadata_path)
417-
self.insert1(
402+
(
403+
body_part_position_entry,
404+
inference_duration,
405+
total_frame_count,
406+
creation_time,
407+
) = _load_facemap_results(key, facemap_result_path, full_metadata_path)
408+
self.insert1(
409+
{
410+
**key,
411+
"inference_completion_time": creation_time,
412+
"inference_run_duration": inference_duration,
413+
"total_frame_count": total_frame_count,
414+
}
415+
)
416+
self.BodyPartPosition.insert(body_part_position_entry)
417+
# Insert result files
418+
self.File.insert(
419+
[
418420
{
419421
**key,
420-
"inference_completion_time": creation_time,
421-
"inference_run_duration": inference_duration,
422-
"total_frame_count": total_frame_count,
422+
"file_name": f.relative_to(output_dir).as_posix(),
423+
"file": f,
423424
}
424-
)
425-
self.BodyPartPosition.insert(body_part_position_entry)
425+
for f in (facemap_result_path, full_metadata_path)
426+
]
427+
)
426428

427429
@classmethod
428430
def get_trajectory(cls, key: dict, body_parts: list = "all") -> pd.DataFrame:
@@ -468,7 +470,6 @@ def get_trajectory(cls, key: dict, body_parts: list = "all") -> pd.DataFrame:
468470

469471
def _load_facemap_results(key, facemap_result_path, full_metadata_path):
470472
"""Load facemap results from h5 and metadata files."""
471-
472473
from facemap import utils
473474

474475
with open(full_metadata_path, "rb") as f:

element_facemap/facial_behavior_estimation.py

+19
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,14 @@ class FacemapProcessing(dj.Computed):
283283
package_version='' : varchar(16)
284284
"""
285285

286+
class File(dj.Part):
287+
definition = """
288+
-> master
289+
file_name: varchar(255)
290+
---
291+
file: filepath@facemap-processed
292+
"""
293+
286294
# Process only the VideoRecordings that have their Info inserted.
287295
@property
288296
def key_source(self):
@@ -331,6 +339,17 @@ def _run_facemap_process():
331339
creation_time = datetime.fromtimestamp(results_proc_fp.stat().st_ctime)
332340

333341
self.insert1({**key, "processing_time": creation_time})
342+
# Insert result files
343+
self.File.insert(
344+
[
345+
{
346+
**key,
347+
"file_name": f.relative_to(output_dir).as_posix(),
348+
"file": f,
349+
}
350+
for f in (results_proc_fp,)
351+
]
352+
)
334353

335354

336355
@schema

0 commit comments

Comments
 (0)