Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
luiztauffer committed Nov 21, 2024
1 parent db2fc74 commit 2b2ead0
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches:
- main
- dev

- preprocessing

jobs:
run:
Expand Down
2 changes: 1 addition & 1 deletion src/vame/initialize_project/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from vame.schemas.states import VAMEPipelineStatesSchema
from vame.logging.logger import VameLogger
from vame.util.auxiliary import write_config
from vame.util.video import get_video_frame_rate
from vame.video.video import get_video_frame_rate
from vame.io.load_poses import load_pose_estimation


Expand Down
76 changes: 9 additions & 67 deletions src/vame/preprocessing/align_egocentrical.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import pandas as pd
import tqdm
from typing import Tuple, List, Union
from vame.logging.logger import VameLogger, TqdmToLogger
from pathlib import Path

from vame.logging.logger import VameLogger, TqdmToLogger
from vame.util.auxiliary import read_config
from vame.schemas.states import EgocentricAlignmentFunctionSchema, save_state
from vame.schemas.project import PoseEstimationFiletype
Expand All @@ -15,6 +16,7 @@
background,
read_pose_estimation_file,
)
from vame.video import get_video_frame_rate


logger_config = VameLogger(__name__)
Expand Down Expand Up @@ -160,13 +162,6 @@ def align_mouse(
rect = tuple(lst)
center, size, theta = rect

# lst2 = list(rect)
# lst2[0][0] = center[0] - size[0]//2
# lst2[0][1] = center[1] - size[1]//2
# rect = tuple(lst2)
# center[0] -= size[0]//2
# center[1] -= size[0]//2 # added this shift to change center to belly 2/28/2024

# crop image
out, shifted_points = crop_and_flip(
rect,
Expand All @@ -192,52 +187,6 @@ def align_mouse(
return images, points, time_series


def play_aligned_video(
a: List[np.ndarray],
n: List[List[np.ndarray]],
frame_count: int,
) -> None:
"""
Play the aligned video.
Parameters
----------
a : List[np.ndarray]
List of aligned images.
n : List[List[np.ndarray]]
List of aligned DLC points.
frame_count : int
Number of frames in the video.
"""
colors = [
(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
(255, 255, 0),
(255, 0, 255),
(0, 255, 255),
(0, 0, 0),
(255, 255, 255),
]
for i in range(frame_count):
# Capture frame-by-frame
ret, frame = True, a[i]
if ret is True:
# Display the resulting frame
frame = cv.cvtColor(frame.astype("uint8") * 255, cv.COLOR_GRAY2BGR)
im_color = cv.applyColorMap(frame, cv.COLORMAP_JET)
for c, j in enumerate(n[i]):
cv.circle(im_color, (j[0], j[1]), 5, colors[c], -1)
cv.imshow("Frame", im_color)
# Press Q on keyboard to exit
# Break the loop
if cv.waitKey(25) & 0xFF == ord("q"):
break
else:
break
cv.destroyAllWindows()


def alignment(
project_path: str,
session: str,
Expand All @@ -248,7 +197,6 @@ def alignment(
pose_estimation_filetype: PoseEstimationFiletype,
path_to_pose_nwb_series_data: Union[str, None] = None,
use_video: bool = False,
check_video: bool = False,
tqdm_stream: Union[TqdmToLogger, None] = None,
) -> Tuple[np.ndarray, List[np.ndarray]]:
"""
Expand All @@ -274,8 +222,6 @@ def alignment(
Path to the pose series data in nwb files. Defaults to None.
use_video : bool, optional
Whether to use video for alignment. Defaults to False.
check_video : bool, optional
Whether to check the aligned video. Defaults to False.
tqdm_stream : Union[TqdmToLogger, None], optional
Tqdm stream to log the progress. Defaults to None.
Expand Down Expand Up @@ -320,11 +266,7 @@ def alignment(
video_path=video_path,
save_background=False,
)
capture = cv.VideoCapture(video_path)
if not capture.isOpened():
raise Exception(f"Unable to open video file: {video_path}")
frame_count = int(capture.get(cv.CAP_PROP_FRAME_COUNT))
capture.release()
frame_count = get_video_frame_rate(video_path)

Check warning on line 269 in src/vame/preprocessing/align_egocentrical.py

View check run for this annotation

Codecov / codecov/patch

src/vame/preprocessing/align_egocentrical.py#L269

Added line #L269 was not covered by tests
else:
bg = 0
# Change this to an abitrary number if you first want to test the code
Expand All @@ -345,9 +287,6 @@ def alignment(
tqdm_stream=tqdm_stream,
)

if check_video:
play_aligned_video(frames, n, frame_count)

return time_series, frames


Expand Down Expand Up @@ -442,7 +381,6 @@ def egocentric_alignment(
else paths_to_pose_nwb_series_data[i]
),
use_video=use_video,
check_video=check_video,
tqdm_stream=tqdm_stream,
)

Expand All @@ -458,7 +396,11 @@ def egocentric_alignment(
# Save new shifted file
np.save(
os.path.join(
project_path, "data", "processed", session, session + "-PE-seq.npy"
project_path,
"data",
"processed",
session,
session + "-PE-seq.npy",
),
egocentric_time_series_shifted,
)
Expand Down
8 changes: 2 additions & 6 deletions src/vame/util/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@ def report(
with open(project_path / "states" / "states.json") as f:
project_states = json.load(f)

pose_estimation_files = list(
(project_path / "data" / "raw").glob("*.nc")
)
video_files = list(
(project_path / "data" / "raw").glob("*.mp4")
)
pose_estimation_files = list((project_path / "data" / "raw").glob("*.nc"))
video_files = list((project_path / "data" / "raw").glob("*.mp4"))

# Create a report folder for the project, if it does not exist
report_folder = project_path / "reports"
Expand Down
8 changes: 0 additions & 8 deletions src/vame/util/video.py

This file was deleted.

4 changes: 4 additions & 0 deletions src/vame/video/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from vame.video.video import (
get_video_frame_rate,
play_aligned_video,
)
58 changes: 58 additions & 0 deletions src/vame/video/video.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import cv2
from typing import List
import numpy as np


def get_video_frame_rate(video_path):
video = cv2.VideoCapture(video_path)
if not video.isOpened():
raise Exception(f"Unable to open video file: {video_path}")

Check warning on line 9 in src/vame/video/video.py

View check run for this annotation

Codecov / codecov/patch

src/vame/video/video.py#L9

Added line #L9 was not covered by tests
frame_rate = int(video.get(cv2.CAP_PROP_FPS))
video.release()
return frame_rate


def play_aligned_video(
a: List[np.ndarray],
n: List[List[np.ndarray]],
frame_count: int,
) -> None:
"""
Play the aligned video.
Parameters
----------
a : List[np.ndarray]
List of aligned images.
n : List[List[np.ndarray]]
List of aligned DLC points.
frame_count : int
Number of frames in the video.
"""
colors = [

Check warning on line 32 in src/vame/video/video.py

View check run for this annotation

Codecov / codecov/patch

src/vame/video/video.py#L32

Added line #L32 was not covered by tests
(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
(255, 255, 0),
(255, 0, 255),
(0, 255, 255),
(0, 0, 0),
(255, 255, 255),
]
for i in range(frame_count):

Check warning on line 42 in src/vame/video/video.py

View check run for this annotation

Codecov / codecov/patch

src/vame/video/video.py#L42

Added line #L42 was not covered by tests
# Capture frame-by-frame
ret, frame = True, a[i]
if ret is True:

Check warning on line 45 in src/vame/video/video.py

View check run for this annotation

Codecov / codecov/patch

src/vame/video/video.py#L44-L45

Added lines #L44 - L45 were not covered by tests
# Display the resulting frame
frame = cv2.cvtColor(frame.astype("uint8") * 255, cv2.COLOR_GRAY2BGR)
im_color = cv2.applyColorMap(frame, cv2.COLORMAP_JET)
for c, j in enumerate(n[i]):
cv2.circle(im_color, (j[0], j[1]), 5, colors[c], -1)
cv2.imshow("Frame", im_color)

Check warning on line 51 in src/vame/video/video.py

View check run for this annotation

Codecov / codecov/patch

src/vame/video/video.py#L47-L51

Added lines #L47 - L51 were not covered by tests
# Press Q on keyboard to exit
# Break the loop
if cv2.waitKey(25) & 0xFF == ord("q"):
break

Check warning on line 55 in src/vame/video/video.py

View check run for this annotation

Codecov / codecov/patch

src/vame/video/video.py#L54-L55

Added lines #L54 - L55 were not covered by tests
else:
break
cv2.destroyAllWindows()

Check warning on line 58 in src/vame/video/video.py

View check run for this annotation

Codecov / codecov/patch

src/vame/video/video.py#L57-L58

Added lines #L57 - L58 were not covered by tests

0 comments on commit 2b2ead0

Please sign in to comment.