Skip to content

deeplabcut.pose_estimation_3d

Modules:

Name Description
auxfun_multianimal

DeepLabCut2.0 Toolbox (deeplabcut.org)

auxiliaryfunctions

DeepLabCut2.0 Toolbox (deeplabcut.org)

auxiliaryfunctions_3d

DeepLabCut2.0 Toolbox (deeplabcut.org)

camera_calibration
make_labeled_video

DeepLabCut2.0 Toolbox (deeplabcut.org)

plotting3D
triangulation

Functions:

Name Description
calibrate_cameras

Extract corner points from calibration images, calibrate cameras, and store results.

check_undistortion

Undistort calibration images and store them for visual inspection.

create_labeled_video_3d

Create a video with two camera views and 3D reconstruction for selected frames.

triangulate

Triangulate DLC keypoints from two camera views into 3D predictions.

calibrate_cameras

calibrate_cameras(config: str | Path, cbrow=8, cbcol=6, calibrate=False, alpha=0.4, search_window_size=(11, 11))

Extract corner points from calibration images, calibrate cameras, and store results.

Make sure you have around 20-60 pairs of calibration images. The function should be used iteratively to select the right set of calibration images.

A pair of calibration image is considered "correct", if the corners are detected correctly in both the images. It may happen that during the first run of this function, the extracted corners are incorrect or the order of detected corners does not align for the corresponding views (i.e. camera-1 and camera-2 images).

In such a case, remove those pairs of images and re-run this function. Once the right number of calibration images are selected, use the parameter calibrate=True to calibrate the cameras.

Parameters:

Name Type Description Default

config

str | Path

Full path of the config.yaml file as a string.

required

cbrow

int

Integer specifying the number of rows in the calibration image.

8

cbcol

int

Integer specifying the number of columns in the calibration image.

6

calibrate

bool

If True, calibrate cameras with the current calibration images. Set to True only after checking corner detection and removing bad images. Defaults to False.

False

alpha

float

Free scaling parameter between 0 and 1. When alpha = 0, rectified images with only valid pixels are stored (zoomed in). When alpha = 1, all pixels from the original images are retained. For more details: https://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html

0.4

search_window_size

tuple of int

Half of the side length of the search window when refining detected checkerboard corners for subpixel accuracy.

(11, 11)

Examples:

Linux/MacOs/Windows:

deeplabcut.calibrate_cameras(config)

Once the right set of calibration images are selected:

deeplabcut.calibrate_cameras(config, calibrate=True)
Source code in deeplabcut/pose_estimation_3d/camera_calibration.py
def calibrate_cameras(
    config: str | Path,
    cbrow=8,
    cbcol=6,
    calibrate=False,
    alpha=0.4,
    search_window_size=(11, 11),
):
    """Extract corner points from calibration images, calibrate cameras, and store results.

    Make sure you have around 20-60 pairs of calibration images.
    The function should be used iteratively to select the right set of calibration images.

    A pair of calibration image is considered "correct",
    if the corners are detected correctly in both the images.
    It may happen that during the first run of this function,
    the extracted corners are incorrect or the order of detected corners
    does not align for the corresponding views (i.e. camera-1 and camera-2 images).

    In such a case, remove those pairs of images and re-run this function.
    Once the right number of calibration images are selected,
    use the parameter ``calibrate=True`` to calibrate the cameras.

    Args:
        config (str | Path): Full path of the config.yaml file as a string.
        cbrow (int): Integer specifying the number of rows in the calibration image.
        cbcol (int): Integer specifying the number of columns in the calibration image.
        calibrate (bool): If True, calibrate cameras with the current calibration images.
            Set to True only after checking corner detection and removing bad images.
            Defaults to False.
        alpha (float): Free scaling parameter between 0 and 1.
            When alpha = 0, rectified images with only valid pixels are stored (zoomed in).
            When alpha = 1, all pixels from the original images are retained.
            For more details:
            https://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html
        search_window_size (tuple of int): Half of the side length of the search window when
            refining detected checkerboard corners for subpixel accuracy.

    Examples:
        Linux/MacOs/Windows:

            deeplabcut.calibrate_cameras(config)

        Once the right set of calibration images are selected:

            deeplabcut.calibrate_cameras(config, calibrate=True)
    """
    # Termination criteria
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

    # Prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
    objp = np.zeros((cbrow * cbcol, 3), np.float32)
    objp[:, :2] = np.mgrid[0:cbcol, 0:cbrow].T.reshape(-1, 2)

    # Read the config file
    cfg_3d = auxiliaryfunctions.read_config(config)
    (
        img_path,
        path_corners,
        path_camera_matrix,
        path_undistort,
        path_removed_images,
    ) = auxiliaryfunctions_3d.Foldernames3Dproject(cfg_3d)

    images = [str(p) for p in Path(img_path).glob("*.jpg")]
    cam_names = cfg_3d["camera_names"]

    # update the variable snapshot* in config file according to the name of the cameras
    try:
        for i in range(len(cam_names)):
            cfg_3d[str("config_file_" + cam_names[i])] = cfg_3d.pop(str("config_file_camera-" + str(i + 1)))
        for i in range(len(cam_names)):
            cfg_3d[str("shuffle_" + cam_names[i])] = cfg_3d.pop(str("shuffle_camera-" + str(i + 1)))
    except Exception:
        pass

    project_path = cfg_3d["project_path"]
    projconfigfile = str(Path(project_path) / "config.yaml")
    auxiliaryfunctions.write_config_3d(projconfigfile, cfg_3d)

    # Initialize the dictionary
    img_shape = {}
    objpoints = {}  # 3d point in real world space
    imgpoints = {}  # 2d points in image plane.
    dist_pickle = {}
    stereo_params = {}
    for cam in cam_names:
        objpoints.setdefault(cam, [])
        imgpoints.setdefault(cam, [])
        dist_pickle.setdefault(cam, [])

    # Sort the images.
    images.sort(key=lambda f: int("".join(filter(str.isdigit, f))))
    if len(images) == 0:
        raise Exception(
            "No calibration images found. "
            "Make sure the calibration images are saved as .jpg and "
            "with prefix as the camera name as specified in the config.yaml file."
        )

    skip_images = []
    for fname in images:
        for cam in cam_names:
            if cam in fname and Path(fname).name not in skip_images:
                filename = Path(fname).stem
                img = cv2.imread(fname)
                gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

                # Find the chess board corners
                ret, corners = cv2.findChessboardCorners(
                    gray, (cbcol, cbrow), None
                )  #  (8,6) pattern (dimensions = common points of black squares)
                # If found, add object points, image points (after refining them)

                if ret:
                    img_shape[cam] = gray.shape[::-1]
                    objpoints[cam].append(objp)
                    corners = cv2.cornerSubPix(gray, corners, search_window_size, (-1, -1), criteria)
                    imgpoints[cam].append(corners)
                    # Draw the corners and store the images
                    img = cv2.drawChessboardCorners(img, (cbcol, cbrow), corners, ret)
                    cv2.imwrite(str(Path(path_corners) / (filename + "_corner.jpg")), img)
                else:
                    print(f"Corners not found for the image {Path(fname).name}")
                    for new_cam in cam_names:
                        remove_fname = Path(fname).name.replace(cam, new_cam)
                        (Path(img_path) / remove_fname).rename(Path(path_removed_images) / remove_fname)
                        if new_cam != cam:
                            skip_images.append(remove_fname)

    try:
        h, w = img.shape[:2]
    except Exception as e:
        raise Exception(
            "It seems that the name of calibration images does not match "
            "with the camera names in the config file. "
            "Please make sure that the calibration images are named"
            " with camera names as specified in the config.yaml file."
        ) from e

    # Perform calibration for each cameras and store the matrices as a pickle file
    if calibrate:
        # Calibrating each camera
        for cam in cam_names:
            ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(
                objpoints[cam], imgpoints[cam], img_shape[cam], None, None
            )

            # Save the camera calibration result for later use (we won't use rvecs / tvecs)
            dist_pickle[cam] = {
                "mtx": mtx,
                "dist": dist,
                "objpoints": objpoints[cam],
                "imgpoints": imgpoints[cam],
            }
            pickle.dump(
                dist_pickle,
                (Path(path_camera_matrix) / (cam + "_intrinsic_params.pickle")).open("wb"),
            )
            print(f"Saving intrinsic camera calibration matrices for {cam} as a pickle file in {path_camera_matrix}")

            # Compute mean re-projection errors for individual cameras
            mean_error = 0
            for i in range(len(objpoints[cam])):
                imgpoints_proj, _ = cv2.projectPoints(objpoints[cam][i], rvecs[i], tvecs[i], mtx, dist)
                error = cv2.norm(imgpoints[cam][i], imgpoints_proj, cv2.NORM_L2) / len(imgpoints_proj)
                mean_error += error
            print(f"Mean re-projection error for {cam} images: {mean_error / len(objpoints[cam]):.3f} pixels ")

        # Compute stereo calibration for each pair of cameras
        camera_pair = [[cam_names[0], cam_names[1]]]
        for pair in camera_pair:
            print("Computing stereo calibration for ")
            (
                retval,
                cameraMatrix1,
                distCoeffs1,
                cameraMatrix2,
                distCoeffs2,
                R,
                T,
                E,
                F,
            ) = cv2.stereoCalibrate(
                objpoints[pair[0]],
                imgpoints[pair[0]],
                imgpoints[pair[1]],
                dist_pickle[pair[0]]["mtx"],
                dist_pickle[pair[0]]["dist"],
                dist_pickle[pair[1]]["mtx"],
                dist_pickle[pair[1]]["dist"],
                (h, w),
                flags=cv2.CALIB_FIX_INTRINSIC,
            )

            # Stereo Rectification
            rectify_scale = alpha  # Free scaling parameter check this https://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#fisheye-stereorectify
            R1, R2, P1, P2, Q, roi1, roi2 = cv2.stereoRectify(
                cameraMatrix1,
                distCoeffs1,
                cameraMatrix2,
                distCoeffs2,
                (h, w),
                R,
                T,
                alpha=rectify_scale,
            )

            stereo_params[pair[0] + "-" + pair[1]] = {
                "cameraMatrix1": cameraMatrix1,
                "cameraMatrix2": cameraMatrix2,
                "distCoeffs1": distCoeffs1,
                "distCoeffs2": distCoeffs2,
                "R": R,
                "T": T,
                "E": E,
                "F": F,
                "R1": R1,
                "R2": R2,
                "P1": P1,
                "P2": P2,
                "roi1": roi1,
                "roi2": roi2,
                "Q": Q,
                "image_shape": [img_shape[pair[0]], img_shape[pair[1]]],
            }

        print(f"Saving the stereo parameters for every pair of cameras as a pickle file in {path_camera_matrix}")

        auxiliaryfunctions.write_pickle(str(Path(path_camera_matrix) / "stereo_params.pickle"), stereo_params)
        print("Camera calibration done! Use the function ``check_undistortion`` to check the check the calibration")
    else:
        print(
            f"Corners extracted! You may check for the extracted corners in the directory {str(path_corners)}"
            " and remove the pair of images where the corners are incorrectly detected. "
            "If all the corners are detected correctly with right order, "
            "then re-run the same function and use the flag ``calibrate=True``, to calbrate the camera."
        )

check_undistortion

check_undistortion(config: str | Path, cbrow=8, cbcol=6, plot=True)

Undistort calibration images and store them for visual inspection.

Uses camera matrices from calibration to verify they are correct.

Parameters:

Name Type Description Default

config

str | Path

Full path of the config.yaml file as a string.

required

cbrow

int

Number of rows in the calibration image.

8

cbcol

int

Number of columns in the calibration image.

6

plot

bool

If True, save undistortion results as plots. Defaults to True.

True

Examples:

Linux/MacOs/Windows:

deeplabcut.check_undistortion(config, cbrow=8, cbcol=6)
Source code in deeplabcut/pose_estimation_3d/camera_calibration.py
def check_undistortion(config: str | Path, cbrow=8, cbcol=6, plot=True):
    """Undistort calibration images and store them for visual inspection.

    Uses camera matrices from calibration to verify they are correct.

    Args:
        config (str | Path): Full path of the config.yaml file as a string.
        cbrow (int): Number of rows in the calibration image.
        cbcol (int): Number of columns in the calibration image.
        plot (bool, optional): If True, save undistortion results as plots. Defaults to True.

    Examples:
        Linux/MacOs/Windows:

            deeplabcut.check_undistortion(config, cbrow=8, cbcol=6)
    """
    # Read the config file
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
    cfg_3d = auxiliaryfunctions.read_config(config)
    (
        img_path,
        path_corners,
        path_camera_matrix,
        path_undistort,
        path_removed_images,
    ) = auxiliaryfunctions_3d.Foldernames3Dproject(cfg_3d)

    # colormap = plt.get_cmap(cfg_3d['colormap'])
    markerSize = cfg_3d["dotsize"]
    alphaValue = cfg_3d["alphaValue"]
    markerType = cfg_3d["markerType"]
    markerColor = cfg_3d["markerColor"]
    cam_names = cfg_3d["camera_names"]

    images = [str(p) for p in Path(img_path).glob("*.jpg")]

    # Sort the images
    images.sort(key=lambda f: int("".join(filter(str.isdigit, f))))
    """
    for fname in images:
        for cam in cam_names:
            if cam in fname:
                filename = Path(fname).stem
                ext = Path(fname).suffix
                img = cv2.imread(fname)
                gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    """
    camera_pair = [[cam_names[0], cam_names[1]]]
    stereo_params = auxiliaryfunctions.read_pickle(str(Path(path_camera_matrix) / "stereo_params.pickle"))

    for pair in camera_pair:
        map1_x, map1_y = cv2.initUndistortRectifyMap(
            stereo_params[pair[0] + "-" + pair[1]]["cameraMatrix1"],
            stereo_params[pair[0] + "-" + pair[1]]["distCoeffs1"],
            stereo_params[pair[0] + "-" + pair[1]]["R1"],
            stereo_params[pair[0] + "-" + pair[1]]["P1"],
            (stereo_params[pair[0] + "-" + pair[1]]["image_shape"][0]),
            cv2.CV_16SC2,
        )
        map2_x, map2_y = cv2.initUndistortRectifyMap(
            stereo_params[pair[0] + "-" + pair[1]]["cameraMatrix2"],
            stereo_params[pair[0] + "-" + pair[1]]["distCoeffs2"],
            stereo_params[pair[0] + "-" + pair[1]]["R2"],
            stereo_params[pair[0] + "-" + pair[1]]["P2"],
            (stereo_params[pair[0] + "-" + pair[1]]["image_shape"][1]),
            cv2.CV_16SC2,
        )
        cam1_undistort = []
        cam2_undistort = []

        for fname in images:
            if pair[0] in fname:
                filename = Path(fname).stem
                img1 = cv2.imread(fname)
                gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
                h, w = img1.shape[:2]
                _, corners1 = cv2.findChessboardCorners(gray1, (cbcol, cbrow), None)
                corners_origin1 = cv2.cornerSubPix(gray1, corners1, (11, 11), (-1, -1), criteria)

                # Remapping dataFrame_camera1_undistort
                im_remapped1 = cv2.remap(img1, map1_x, map1_y, cv2.INTER_LANCZOS4)
                imgpoints_proj_undistort = cv2.undistortPoints(
                    src=corners_origin1,
                    cameraMatrix=stereo_params[pair[0] + "-" + pair[1]]["cameraMatrix1"],
                    distCoeffs=stereo_params[pair[0] + "-" + pair[1]]["distCoeffs1"],
                    P=stereo_params[pair[0] + "-" + pair[1]]["P1"],
                    R=stereo_params[pair[0] + "-" + pair[1]]["R1"],
                )
                cam1_undistort.append(imgpoints_proj_undistort)
                cv2.imwrite(
                    str(Path(path_undistort) / (filename + "_undistort.jpg")),
                    im_remapped1,
                )
                imgpoints_proj_undistort = []

            elif pair[1] in fname:
                filename = Path(fname).stem
                img2 = cv2.imread(fname)
                gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
                h, w = img2.shape[:2]
                _, corners2 = cv2.findChessboardCorners(gray2, (cbcol, cbrow), None)
                corners_origin2 = cv2.cornerSubPix(gray2, corners2, (11, 11), (-1, -1), criteria)

                # Remapping
                im_remapped2 = cv2.remap(img2, map2_x, map2_y, cv2.INTER_LANCZOS4)
                imgpoints_proj_undistort2 = cv2.undistortPoints(
                    src=corners_origin2,
                    cameraMatrix=stereo_params[pair[0] + "-" + pair[1]]["cameraMatrix2"],
                    distCoeffs=stereo_params[pair[0] + "-" + pair[1]]["distCoeffs2"],
                    P=stereo_params[pair[0] + "-" + pair[1]]["P2"],
                    R=stereo_params[pair[0] + "-" + pair[1]]["R2"],
                )
                cam2_undistort.append(imgpoints_proj_undistort2)
                cv2.imwrite(
                    str(Path(path_undistort) / (filename + "_undistort.jpg")),
                    im_remapped2,
                )
                imgpoints_proj_undistort2 = []

        cam1_undistort = np.array(cam1_undistort)
        cam2_undistort = np.array(cam2_undistort)
        print(f"All images are undistorted and stored in {str(path_undistort)}")
        print("Use the function ``triangulate`` to undistort the dataframes and compute the triangulation")

        if plot:
            f1, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 10))
            f1.suptitle(
                str("Original Image: Views from " + pair[0] + " and " + pair[1]),
                fontsize=25,
            )

            # Display images in RGB
            ax1.imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB))
            ax2.imshow(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB))

            mcolors.Normalize(vmin=0.0, vmax=cam1_undistort.shape[1])
            plt.savefig(str(Path(path_undistort) / "Original_Image.png"))

            # Plot the undistorted corner points
            f2, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 10))
            f2.suptitle("Undistorted corner points on camera-1 and camera-2", fontsize=25)
            ax1.imshow(cv2.cvtColor(im_remapped1, cv2.COLOR_BGR2RGB))
            ax2.imshow(cv2.cvtColor(im_remapped2, cv2.COLOR_BGR2RGB))
            for i in range(cam1_undistort.shape[1]):
                ax1.scatter(
                    [cam1_undistort[-1][i, 0, 0]],
                    [cam1_undistort[-1][i, 0, 1]],
                    marker=markerType,
                    s=markerSize,
                    color=markerColor,
                    alpha=alphaValue,
                )
                ax2.scatter(
                    [cam2_undistort[-1][i, 0, 0]],
                    [cam2_undistort[-1][i, 0, 1]],
                    marker=markerType,
                    s=markerSize,
                    color=markerColor,
                    alpha=alphaValue,
                )
            plt.savefig(str(Path(path_undistort) / "undistorted_points.png"))

            # Triangulate
            triangulate = auxiliaryfunctions_3d.compute_triangulation_calibration_images(
                stereo_params[pair[0] + "-" + pair[1]],
                cam1_undistort,
                cam2_undistort,
                path_undistort,
                cfg_3d,
                plot=True,
            )
            auxiliaryfunctions.write_pickle("triangulate.pickle", triangulate)

create_labeled_video_3d

create_labeled_video_3d(
    config: str | Path,
    path: str | Path,
    videofolder=None,
    start=0,
    end=None,
    trailpoints=0,
    videotype="",
    view=(-113, -270),
    xlim=None,
    ylim=None,
    zlim=None,
    draw_skeleton=True,
    color_by="bodypart",
    figsize=(20, 8),
    fps=30,
    dpi=300,
)

Create a video with two camera views and 3D reconstruction for selected frames.

Parameters:

Name Type Description Default

config

string

Full path of the config.yaml file as a string.

required

path

list

Full paths to triangulated files for analysis, or a directory containing them.

required

videofolder

string

Full path of the folder where videos are stored. Use when videos are not co-located with triangulation files. Defaults to None (videos searched next to the triangulation file).

None

start

int

Start frame index to select. Defaults to 0.

0

end

int

End frame index to select. Defaults to None (all frames used).

None

trailpoints

int

Number of previous frames whose body parts are plotted (history). Defaults to 0.

0

videotype

string

When path is a directory, only videos with this extension are analyzed. If unspecified, common extensions ('avi', 'mp4', 'mov', 'mpeg', 'mkv') are kept.

''

view

list

Elevation (z plane) and azimuth (x,y plane) angles for the 3D view.

(-113, -270)

xlim

list

Limits for the 3D x-axis. Defaults to [None, None] (min/max over all bodyparts).

None

ylim

list

Limits for the 3D y-axis. Defaults to [None, None] (min/max over all bodyparts).

None

zlim

list

Limits for the 3D z-axis. Defaults to [None, None] (min/max over all bodyparts).

None

draw_skeleton

bool

If True, draw skeleton lines on each frame (from config). Defaults to True.

True

color_by

string

Coloring rule. Each bodypart colored differently by default. Use 'individual' to color all points of one individual the same. Defaults to 'bodypart'.

'bodypart'

figsize

tuple

Figure size for the matplotlib plot. Defaults to (20, 8).

(20, 8)

fps

int

Output video frame rate. Defaults to 30.

30

dpi

int

Output video DPI. Defaults to 300.

300

Examples:

Linux/MacOs deeplabcut.create_labeled_video_3d(config, ["/data/project1/videos/3d.h5"], start=100, end=500)

To create labeled videos for all the triangulated files in the folder deeplabcut.create_labeled_video_3d(config, ["/data/project1/videos"], start=100, end=500)

To set the xlim, ylim, zlim and rotate the view of the 3d axis:

deeplabcut.create_labeled_video_3d(
    config,
    ["/data/project1/videos"],
    start=100,
    end=500,
    view=[30, 90],
    xlim=[-12, 12],
    ylim=[15, 25],
    zlim=[20, 30],
)
Source code in deeplabcut/pose_estimation_3d/plotting3D.py
def create_labeled_video_3d(
    config: str | Path,
    path: str | Path,
    videofolder=None,
    start=0,
    end=None,
    trailpoints=0,
    videotype="",
    view=(-113, -270),
    xlim=None,
    ylim=None,
    zlim=None,
    draw_skeleton=True,
    color_by="bodypart",
    figsize=(20, 8),
    fps=30,
    dpi=300,
):
    """Create a video with two camera views and 3D reconstruction for selected frames.

    Args:
        config (string): Full path of the config.yaml file as a string.
        path (list): Full paths to triangulated files for analysis, or a directory containing them.
        videofolder (string): Full path of the folder where videos are stored.
            Use when videos are not co-located with triangulation files.
            Defaults to None (videos searched next to the triangulation file).
        start (int): Start frame index to select. Defaults to 0.
        end (int): End frame index to select.
            Defaults to None (all frames used).
        trailpoints (int): Number of previous frames whose body parts are plotted (history).
            Defaults to 0.
        videotype (string, optional): When ``path`` is a directory, only videos with this extension are
            analyzed. If unspecified, common extensions ('avi', 'mp4', 'mov', 'mpeg', 'mkv') are kept.
        view (list): Elevation (z plane) and azimuth (x,y plane) angles for the 3D view.
        xlim (list): Limits for the 3D x-axis.
            Defaults to [None, None] (min/max over all bodyparts).
        ylim (list): Limits for the 3D y-axis.
            Defaults to [None, None] (min/max over all bodyparts).
        zlim (list): Limits for the 3D z-axis.
            Defaults to [None, None] (min/max over all bodyparts).
        draw_skeleton (bool): If True, draw skeleton lines on each frame (from config).
            Defaults to True.
        color_by (string, optional): Coloring rule. Each bodypart colored differently by default.
            Use 'individual' to color all points of one individual the same. Defaults to 'bodypart'.
        figsize (tuple, optional): Figure size for the matplotlib plot. Defaults to (20, 8).
        fps (int, optional): Output video frame rate. Defaults to 30.
        dpi (int, optional): Output video DPI. Defaults to 300.

    Examples:
        Linux/MacOs
            deeplabcut.create_labeled_video_3d(config, ["/data/project1/videos/3d.h5"], start=100, end=500)

        To create labeled videos for all the triangulated files in the folder
            deeplabcut.create_labeled_video_3d(config, ["/data/project1/videos"], start=100, end=500)

        To set the xlim, ylim, zlim and rotate the view of the 3d axis:

            deeplabcut.create_labeled_video_3d(
                config,
                ["/data/project1/videos"],
                start=100,
                end=500,
                view=[30, 90],
                xlim=[-12, 12],
                ylim=[15, 25],
                zlim=[20, 30],
            )
    """
    # Read the config file and related variables
    cfg_3d = auxiliaryfunctions.read_config(config)
    cam_names = cfg_3d["camera_names"]
    pcutoff = cfg_3d["pcutoff"]
    markerSize = cfg_3d["dotsize"]
    alphaValue = cfg_3d["alphaValue"]
    cmap = cfg_3d["colormap"]
    bodyparts2connect = cfg_3d["skeleton"]
    skeleton_color = cfg_3d["skeleton_color"]
    scorer_3d = cfg_3d["scorername_3d"]

    if color_by not in ("bodypart", "individual"):
        raise ValueError(f"Invalid color_by={color_by}")

    file_list = auxiliaryfunctions_3d.Get_list_of_triangulated_and_videoFiles(
        path, videotype, scorer_3d, cam_names, videofolder
    )
    print(file_list)
    if file_list == []:
        raise Exception(
            "No corresponding video file(s) found for the specified triangulated file or folder. "
            "Did you specify the video file type? If videos are stored in a different location, "
            "please use the ``videofolder`` argument to specify their path."
        )

    for file in file_list:
        path_h5_file = Path(file[0]).parents[0]
        triangulate_file = file[0]
        # triangulated file is a list which is always sorted as [triangulated.h5,camera-1.videotype,camera-2.videotype]
        # name for output video
        file_name = str(Path(triangulate_file).stem)
        videooutname = path_h5_file / (file_name + ".mp4")
        if videooutname.is_file():
            print("Video already created...")
        else:
            string_to_remove = str(Path(triangulate_file).suffix)
            pickle_file = triangulate_file.replace(string_to_remove, "_meta.pickle")
            metadata_ = auxiliaryfunctions_3d.LoadMetadata3d(pickle_file)

            base_filename_cam1 = str(Path(file[1]).stem).split(videotype)[0]  # required for searching the filtered file
            base_filename_cam2 = str(Path(file[2]).stem).split(videotype)[0]  # required for searching the filtered file
            cam1_view_video = file[1]
            cam2_view_video = file[2]
            cam1_scorer = metadata_["scorer_name"][cam_names[0]]
            cam2_scorer = metadata_["scorer_name"][cam_names[1]]
            print(
                f"Creating 3D video from {Path(cam1_view_video).name} "
                f"and {Path(cam2_view_video).name} using {Path(triangulate_file).name}"
            )

            # Read the video files and corresponfing h5 files
            vid_cam1 = VideoReader(cam1_view_video)
            vid_cam2 = VideoReader(cam2_view_video)

            # Look for the filtered predictions file
            try:
                print("Looking for filtered predictions...")
                df_cam1 = pd.read_hdf(
                    list(path_h5_file.glob("*" + base_filename_cam1 + cam1_scorer + "*filtered.h5"))[0]
                )
                df_cam2 = pd.read_hdf(
                    list(path_h5_file.glob("*" + base_filename_cam2 + cam2_scorer + "*filtered.h5"))[0]
                )
                # print("Found filtered predictions, will be use these for triangulation.")
                print(
                    "Found the following filtered data: ",
                    path_h5_file / ("*" + base_filename_cam1 + cam1_scorer + "*filtered.h5"),
                    path_h5_file / ("*" + base_filename_cam2 + cam2_scorer + "*filtered.h5"),
                )
            except IndexError:
                print("No filtered predictions found, the unfiltered predictions will be used instead.")
                df_cam1 = pd.read_hdf(list(path_h5_file.glob(base_filename_cam1 + cam1_scorer + "*.h5"))[0])
                df_cam2 = pd.read_hdf(list(path_h5_file.glob(base_filename_cam2 + cam2_scorer + "*.h5"))[0])

            df_3d = pd.read_hdf(triangulate_file)
            try:
                num_animals = df_3d.columns.get_level_values("individuals").unique().size
            except KeyError:
                num_animals = 1

            if end is None:
                end = len(df_3d)  # All the frames
            end = min(end, min(len(vid_cam1), len(vid_cam2)))
            frames = list(range(start, end))

            output_folder = path_h5_file / ("temp_" + file_name)
            output_folder.mkdir(parents=True, exist_ok=True)

            # Flatten the list of bodyparts to connect
            bodyparts2plot = list(np.unique([val for sublist in bodyparts2connect for val in sublist]))

            # Format data
            mask2d = df_cam1.columns.get_level_values("bodyparts").isin(bodyparts2plot)
            xy1 = df_cam1.iloc[: len(df_3d)].loc[:, mask2d].to_numpy().reshape((len(df_3d), -1, 3))
            visible1 = xy1[..., 2] >= pcutoff
            xy1[~visible1] = np.nan
            xy2 = df_cam2.iloc[: len(df_3d)].loc[:, mask2d].to_numpy().reshape((len(df_3d), -1, 3))
            visible2 = xy2[..., 2] >= pcutoff
            xy2[~visible2] = np.nan
            mask = df_3d.columns.get_level_values("bodyparts").isin(bodyparts2plot)
            xyz = df_3d.loc[:, mask].to_numpy().reshape((len(df_3d), -1, 3))
            xyz[~(visible1 & visible2)] = np.nan

            bpts = df_3d.columns.get_level_values("bodyparts")[mask][::3]
            links = make_labeled_video.get_segment_indices(
                bodyparts2connect,
                bpts,
            )
            ind_links = tuple(zip(*links, strict=False))

            if color_by == "bodypart":
                color = plt.cm.get_cmap(cmap, len(bodyparts2plot))
                colors_ = color(range(len(bodyparts2plot)))
                colors = np.tile(colors_, (num_animals, 1))
            elif color_by == "individual":
                color = plt.cm.get_cmap(cmap, num_animals)
                colors_ = color(range(num_animals))
                colors = np.repeat(colors_, len(bodyparts2plot), axis=0)

            # Trick to force equal aspect ratio of 3D plots
            minmax = np.nanpercentile(xyz[frames], q=[25, 75], axis=(0, 1)).T
            minmax *= 1.1
            minmax_range = (minmax[:, 1] - minmax[:, 0]).max() / 2
            if xlim is None:
                mid_x = np.mean(minmax[0])
                xlim = mid_x - minmax_range, mid_x + minmax_range
            if ylim is None:
                mid_y = np.mean(minmax[1])
                ylim = mid_y - minmax_range, mid_y + minmax_range
            if zlim is None:
                mid_z = np.mean(minmax[2])
                zlim = mid_z - minmax_range, mid_z + minmax_range

            # Set up the matplotlib figure beforehand
            fig, axes1, axes2, axes3 = set_up_grid(figsize, xlim, ylim, zlim, view)
            points_2d1 = axes1.scatter(
                *np.zeros((2, len(bodyparts2plot))),
                s=markerSize,
                alpha=alphaValue,
            )
            im1 = axes1.imshow(np.zeros((vid_cam1.height, vid_cam1.width)))
            points_2d2 = axes2.scatter(
                *np.zeros((2, len(bodyparts2plot))),
                s=markerSize,
                alpha=alphaValue,
            )
            im2 = axes2.imshow(np.zeros((vid_cam2.height, vid_cam2.width)))
            points_3d = axes3.scatter(
                *np.zeros((3, len(bodyparts2plot))),
                s=markerSize,
                alpha=alphaValue,
            )
            if draw_skeleton:
                # Set up skeleton LineCollections
                segs = np.zeros((2, len(ind_links), 2))
                coll1 = LineCollection(segs, colors=skeleton_color)
                coll2 = LineCollection(segs, colors=skeleton_color)
                axes1.add_collection(coll1)
                axes2.add_collection(coll2)
                segs = np.zeros((2, len(ind_links), 3))
                coll_3d = Line3DCollection(segs, colors=skeleton_color)
                axes3.add_collection(coll_3d)

            writer = FFMpegWriter(fps=fps)
            with writer.saving(fig, str(videooutname), dpi=dpi):
                for k in tqdm(frames):
                    vid_cam1.set_to_frame(k)
                    vid_cam2.set_to_frame(k)
                    frame_cam1 = vid_cam1.read_frame()
                    frame_cam2 = vid_cam2.read_frame()
                    if frame_cam1 is None or frame_cam2 is None:
                        raise OSError("A video frame is empty.")

                    im1.set_data(frame_cam1)
                    im2.set_data(frame_cam2)

                    sl = slice(max(0, k - trailpoints), k + 1)
                    coords3d = xyz[sl]
                    coords1 = xy1[sl, :, :2]
                    coords2 = xy2[sl, :, :2]
                    points_3d._offsets3d = coords3d.reshape((-1, 3)).T
                    points_3d.set_color(colors)
                    points_2d1.set_offsets(coords1.reshape((-1, 2)))
                    points_2d1.set_color(colors)
                    points_2d2.set_offsets(coords2.reshape((-1, 2)))
                    points_2d2.set_color(colors)
                    if draw_skeleton:
                        segs3d = xyz[k][tuple([ind_links])].swapaxes(0, 1)
                        coll_3d.set_segments(segs3d)
                        segs1 = xy1[k, :, :2][tuple([ind_links])].swapaxes(0, 1)
                        coll1.set_segments(segs1)
                        segs2 = xy2[k, :, :2][tuple([ind_links])].swapaxes(0, 1)
                        coll2.set_segments(segs2)

                    writer.grab_frame()

triangulate

triangulate(
    config: str | Path,
    video_path: str | Path | list[str | Path] | list[list[str | Path]],
    videotype="",
    filterpredictions=True,
    filtertype="median",
    gputouse=None,
    destfolder=None,
    save_as_csv=False,
    track_method="",
)

Triangulate DLC keypoints from two camera views into 3D predictions.

Uses camera matrices from calibration.

Parameters:

Name Type Description Default

config

string

Full path of the config.yaml file as a string.

required

video_path

string/list of list

Directory where videos are saved, or a list of video pairs, e.g. [['video1-camera-1.avi', 'video1-camera-2.avi']].

required

videotype

string

When video_path is a directory, only videos with this extension are analyzed. If unspecified, common extensions ('avi', 'mp4', 'mov', 'mpeg', 'mkv') are kept.

''

filterpredictions

bool

Filter predictions with filtertype. Defaults to True.

True

filtertype

string

Filter to use: 'arima' or 'median' (currently supported).

'median'

gputouse

int None

destfolder

string

Destination folder for analysis data. Defaults to the video path.

None

save_as_csv

bool

Save predictions as .csv. Defaults to False.

False

track_method

str

Tracking method suffix for multi-animal projects. Defaults to "".

''

Examples:

Linux/MacOS — analyze all videos in the directory: deeplabcut.triangulate(config, "/data/project1/videos/")

To analyze only a few pairs of videos: deeplabcut.triangulate( config, [ [ "/data/project1/videos/video1-camera-1.avi", "/data/project1/videos/video1-camera-2.avi", ], [ "/data/project1/videos/video2-camera-1.avi", "/data/project1/videos/video2-camera-2.avi", ], ], )

Windows — analyze all videos in the directory: deeplabcut.triangulate(config, "C:\yourusername\rig-95\Videos")

To analyze only a few pairs of videos: deeplabcut.triangulate( config, [ [ "C:\yourusername\rig-95\Videos\video1-camera-1.avi", "C:\yourusername\rig-95\Videos\video1-camera-2.avi", ], [ "C:\yourusername\rig-95\Videos\video2-camera-1.avi", "C:\yourusername\rig-95\Videos\video2-camera-2.avi", ], ], )

Source code in deeplabcut/pose_estimation_3d/triangulation.py
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
def triangulate(
    config: str | Path,
    video_path: str | Path | list[str | Path] | list[list[str | Path]],
    videotype="",
    filterpredictions=True,
    filtertype="median",
    gputouse=None,
    destfolder=None,
    save_as_csv=False,
    track_method="",
):
    """Triangulate DLC keypoints from two camera views into 3D predictions.

    Uses camera matrices from calibration.

    Args:
        config (string): Full path of the config.yaml file as a string.
        video_path (string/list of list): Directory where videos are saved, or a list of video pairs,
            e.g. [['video1-camera-1.avi', 'video1-camera-2.avi']].
        videotype (string, optional): When ``video_path`` is a directory, only videos with this extension
            are analyzed. If unspecified, common extensions ('avi', 'mp4', 'mov', 'mpeg', 'mkv') are kept.
        filterpredictions (bool, optional): Filter predictions with ``filtertype``.
            Defaults to True.
        filtertype (string): Filter to use: 'arima' or 'median' (currently supported).
        gputouse (int, optional): GPU index (see nvidia-smi). Use None if no GPU.
            See: https://nvidia.custhelp.com/app/answers/detail/a_id/3751/~/useful-nvidia-smi-queries
        destfolder (string, optional): Destination folder for analysis data.
            Defaults to the video path.
        save_as_csv (bool, optional): Save predictions as .csv. Defaults to False.
        track_method (str, optional): Tracking method suffix for multi-animal projects.
            Defaults to "".

    Examples:
        Linux/MacOS — analyze all videos in the directory:
            deeplabcut.triangulate(config, "/data/project1/videos/")

        To analyze only a few pairs of videos:
            deeplabcut.triangulate(
                config,
                [
                    [
                        "/data/project1/videos/video1-camera-1.avi",
                        "/data/project1/videos/video1-camera-2.avi",
                    ],
                    [
                        "/data/project1/videos/video2-camera-1.avi",
                        "/data/project1/videos/video2-camera-2.avi",
                    ],
                ],
            )

        Windows — analyze all videos in the directory:
            deeplabcut.triangulate(config, "C:\\yourusername\\rig-95\\Videos")

        To analyze only a few pairs of videos:
            deeplabcut.triangulate(
                config,
                [
                    [
                        "C:\\yourusername\\rig-95\\Videos\\video1-camera-1.avi",
                        "C:\\yourusername\\rig-95\\Videos\\video1-camera-2.avi",
                    ],
                    [
                        "C:\\yourusername\\rig-95\\Videos\\video2-camera-1.avi",
                        "C:\\yourusername\\rig-95\\Videos\\video2-camera-2.avi",
                    ],
                ],
            )
    """
    from deeplabcut.compat import analyze_videos
    from deeplabcut.post_processing import filtering

    cfg_3d = auxiliaryfunctions.read_config(config)
    cam_names = cfg_3d["camera_names"]
    pcutoff = cfg_3d["pcutoff"]
    scorer_3d = cfg_3d["scorername_3d"]

    snapshots = {}
    for cam in cam_names:
        snapshots[cam] = cfg_3d[str("config_file_" + cam)]
        # Check if the config file exists
        if not Path(snapshots[cam]).exists():
            raise Exception(
                str("It seems the file specified in the variable config_file_" + str(cam))
                + " does not exist. Please edit the config file with correct file path and retry."
            )

    # flag to check if the video_path variable is a string or a list of list
    flag = False  # assumes that video path is a list
    if isinstance(video_path, str):
        flag = True
        video_list = auxiliaryfunctions_3d.get_camerawise_videos(video_path, cam_names, videotype=videotype)
    else:
        video_list = video_path

    if video_list == []:
        print("No videos found in the specified video path.", video_path)
        print(
            "Please make sure that the video names are specified with"
            " correct camera names as entered in the config file or"
        )
        print(
            "perhaps the videotype is distinct from the videos in the path, I was looking for:",
            videotype,
        )

    print("List of pairs:", video_list)
    scorer_name = {}
    run_triangulate = False
    for i in range(len(video_list)):
        dataname = []
        for j in range(len(video_list[i])):  # looping over cameras
            if cam_names[j] not in video_list[i][j]:
                raise ValueError(f"Camera name '{cam_names[j]}' not found in video list '{video_list[i][j]}'.")
            else:
                print("Analyzing video {} using {}".format(video_list[i][j], str("config_file_" + cam_names[j])))

                config_2d = snapshots[cam_names[j]]
                cfg = auxiliaryfunctions.read_config(config_2d)

                # Get track_method and do related checks
                track_method = auxfun_multianimal.get_track_method(cfg, track_method=track_method)
                if len(cfg.get("multianimalbodyparts", [])) == 1 and track_method != "box":
                    warnings.warn("Switching to `box` tracker for single point tracking...", stacklevel=2)
                    track_method = "box"

                # Get track method suffix
                tr_method_suffix = TRACK_METHODS.get(track_method, "")

                shuffle = cfg_3d[str("shuffle_" + cam_names[j])]
                trainingsetindex = cfg_3d[str("trainingsetindex_" + cam_names[j])]
                trainFraction = cfg["TrainingFraction"][trainingsetindex]
                if flag:
                    video = str(Path(video_path) / video_list[i][j])
                else:
                    video_path = str(Path(video_list[i][j]).parents[0])
                    video = str(Path(video_path) / video_list[i][j])

                if destfolder is None:
                    destfolder = str(Path(video).parents[0])

                vname = Path(video).stem
                prefix = str(vname).split(cam_names[j])[0]
                suffix = str(vname).split(cam_names[j])[-1]
                if prefix == "":
                    pass
                elif prefix[-1] == "_" or prefix[-1] == "-":
                    prefix = prefix[:-1]

                if suffix == "":
                    pass
                elif suffix[0] == "_" or suffix[0] == "-":
                    suffix = suffix[1:]

                if prefix == "":
                    output_file = str(Path(destfolder) / suffix)
                else:
                    if suffix == "":
                        output_file = str(Path(destfolder) / prefix)
                    else:
                        output_file = str(Path(destfolder) / (prefix + "_" + suffix))

                output_filename = output_file + "_" + scorer_3d  # Check if the videos are already analyzed for 3d
                if Path(output_filename + ".h5").is_file():
                    if save_as_csv is True and not Path(output_filename + ".csv").exists():
                        # In case user adds save_as_csv is True after triangulating
                        pd.read_hdf(output_filename + ".h5").to_csv(str(output_filename + ".csv"))

                    print(
                        "Already analyzed..."
                        "Checking the meta data for any change in the camera matrices and/or scorer names",
                        vname,
                    )
                    pickle_file = str(output_filename + "_meta.pickle")
                    metadata_ = auxiliaryfunctions_3d.LoadMetadata3d(pickle_file)
                    (
                        img_path,
                        path_corners,
                        path_camera_matrix,
                        path_undistort,
                        _,
                    ) = auxiliaryfunctions_3d.Foldernames3Dproject(cfg_3d)
                    path_stereo_file = str(Path(path_camera_matrix) / "stereo_params.pickle")
                    stereo_file = auxiliaryfunctions.read_pickle(path_stereo_file)
                    cam_pair = str(cam_names[0] + "-" + cam_names[1])
                    is_video_analyzed = False  # variable to keep track if the video was already analyzed
                    # Check for the camera matrix
                    for k in metadata_["stereo_matrix"].keys():
                        if np.all(metadata_["stereo_matrix"][k] == stereo_file[cam_pair][k]):
                            pass
                        else:
                            run_triangulate = True

                    # Check for scorer names in the pickle file of 3d output
                    DLCscorer, DLCscorerlegacy = auxiliaryfunctions.get_scorer_name(
                        cfg, shuffle, trainFraction, trainingsiterations="unknown"
                    )

                    if metadata_["scorer_name"][cam_names[j]] == DLCscorer:  # TODO: CHECK FOR BOTH?
                        is_video_analyzed = True
                    elif metadata_["scorer_name"][cam_names[j]] == DLCscorerlegacy:
                        is_video_analyzed = True
                    else:
                        is_video_analyzed = False
                        run_triangulate = True

                    if is_video_analyzed:
                        print("This file is already analyzed!")
                        dataname.append(str(Path(destfolder) / (vname + DLCscorer + tr_method_suffix + ".h5")))
                        scorer_name[cam_names[j]] = DLCscorer
                    else:
                        # Analyze video if score name is different
                        DLCscorer = analyze_videos(
                            config_2d,
                            [video],
                            video_extensions=videotype,
                            shuffle=shuffle,
                            trainingsetindex=trainingsetindex,
                            gputouse=gputouse,
                            destfolder=destfolder,
                        )
                        scorer_name[cam_names[j]] = DLCscorer
                        is_video_analyzed = False
                        run_triangulate = True
                        suffix = tr_method_suffix
                        if filterpredictions:
                            filtering.filterpredictions(
                                config_2d,
                                [video],
                                video_extensions=videotype,
                                shuffle=shuffle,
                                trainingsetindex=trainingsetindex,
                                filtertype=filtertype,
                                destfolder=destfolder,
                            )
                            suffix += "_filtered"

                        dataname.append(str(Path(destfolder) / (vname + DLCscorer + suffix + ".h5")))

                else:  # need to do the whole jam.
                    DLCscorer = analyze_videos(
                        config_2d,
                        [video],
                        video_extensions=videotype,
                        shuffle=shuffle,
                        trainingsetindex=trainingsetindex,
                        gputouse=gputouse,
                        destfolder=destfolder,
                    )
                    scorer_name[cam_names[j]] = DLCscorer
                    run_triangulate = True
                    print(destfolder, vname, DLCscorer)
                    suffix = tr_method_suffix
                    if filterpredictions:
                        filtering.filterpredictions(
                            config_2d,
                            [video],
                            video_extensions=videotype,
                            shuffle=shuffle,
                            trainingsetindex=trainingsetindex,
                            filtertype=filtertype,
                            destfolder=destfolder,
                        )
                        suffix += "_filtered"
                    dataname.append(str(Path(destfolder) / (vname + DLCscorer + suffix + ".h5")))

        if run_triangulate:
            #        if len(dataname)>0:
            # undistort points for this pair
            print("Undistorting...")
            (
                dataFrame_camera1_undistort,
                dataFrame_camera2_undistort,
                stereomatrix,
                path_stereo_file,
            ) = undistort_points(config, dataname, str(cam_names[0] + "-" + cam_names[1]))
            if len(dataFrame_camera1_undistort) != len(dataFrame_camera2_undistort):
                warnings.warn(
                    "The number of frames do not match in the two videos. "
                    "Please make sure that your videos have same number of frames and then retry! "
                    "Excluding the extra frames from the longer video.",
                    stacklevel=2,
                )
                if len(dataFrame_camera1_undistort) > len(dataFrame_camera2_undistort):
                    dataFrame_camera1_undistort = dataFrame_camera1_undistort[: len(dataFrame_camera2_undistort)]
                if len(dataFrame_camera2_undistort) > len(dataFrame_camera1_undistort):
                    dataFrame_camera2_undistort = dataFrame_camera2_undistort[: len(dataFrame_camera1_undistort)]
                    # raise Exception("The number of frames do not match in the two videos.
                    # Please make sure that your videos have same number of frames and then retry!")
            dataFrame_camera1_undistort.columns.get_level_values(0)[0]
            dataFrame_camera2_undistort.columns.get_level_values(0)[0]

            dataFrame_camera1_undistort.columns.get_level_values("bodyparts").unique()

            P1 = stereomatrix["P1"]
            P2 = stereomatrix["P2"]
            F = stereomatrix["F"]

            print("Computing the triangulation...")

            num_frames = dataFrame_camera1_undistort.shape[0]
            ### Assign nan to [X,Y] of low likelihood predictions ###
            # Convert the data to a np array to easily mask out the low likelihood predictions
            data_cam1_tmp = dataFrame_camera1_undistort.to_numpy().reshape((num_frames, -1, 3))
            data_cam2_tmp = dataFrame_camera2_undistort.to_numpy().reshape((num_frames, -1, 3))
            # Assign [X,Y] = nan to low likelihood predictions
            data_cam1_tmp[data_cam1_tmp[..., 2] < pcutoff, :2] = np.nan
            data_cam2_tmp[data_cam2_tmp[..., 2] < pcutoff, :2] = np.nan

            # Reshape data back to original shape
            data_cam1_tmp = data_cam1_tmp.reshape(num_frames, -1)
            data_cam2_tmp = data_cam2_tmp.reshape(num_frames, -1)

            # put data back to the dataframes
            dataFrame_camera1_undistort[:] = data_cam1_tmp
            dataFrame_camera2_undistort[:] = data_cam2_tmp

            if cfg.get("multianimalproject"):
                # Check individuals are the same in both views
                individuals_view1 = (
                    dataFrame_camera1_undistort.columns.get_level_values("individuals").unique().to_list()
                )
                individuals_view2 = (
                    dataFrame_camera2_undistort.columns.get_level_values("individuals").unique().to_list()
                )
                if individuals_view1 != individuals_view2:
                    raise ValueError("The individuals do not match between the two DataFrames")

                # Cross-view match individuals
                _, voting = auxiliaryfunctions_3d.cross_view_match_dataframes(
                    dataFrame_camera1_undistort, dataFrame_camera2_undistort, F
                )
            else:
                # Create a dummy variables for single-animal
                individuals_view1 = ["indie"]
                voting = {0: 0}

            # Cleaner variable (since inds view1 == inds view2)
            individuals = individuals_view1

            # Reshape: (num_framex, num_individuals, num_bodyparts , 2)
            all_points_cam1 = dataFrame_camera1_undistort.to_numpy().reshape((num_frames, len(individuals), -1, 3))[
                ..., :2
            ]
            all_points_cam2 = dataFrame_camera2_undistort.to_numpy().reshape((num_frames, len(individuals), -1, 3))[
                ..., :2
            ]

            # Triangulate data
            triangulate = []
            for i, _ in enumerate(individuals):
                # i is individual in view 1
                # voting[i] is the matched individual in view 2

                pts_indv_cam1 = all_points_cam1[:, i].reshape((-1, 2)).T
                pts_indv_cam2 = all_points_cam2[:, voting[i]].reshape((-1, 2)).T

                indv_points_3d = auxiliaryfunctions_3d.triangulatePoints(P1, P2, pts_indv_cam1, pts_indv_cam2)

                indv_points_3d = indv_points_3d[:3].T.reshape((num_frames, -1, 3))

                triangulate.append(indv_points_3d)

            triangulate = np.asanyarray(triangulate)
            metadata = {}
            metadata["stereo_matrix"] = stereomatrix
            metadata["stereo_matrix_file"] = path_stereo_file
            metadata["scorer_name"] = {
                cam_names[0]: scorer_name[cam_names[0]],
                cam_names[1]: scorer_name[cam_names[1]],
            }

            # Create 3D DataFrame column and row indices
            cols = [
                [scorer_3d],
                list(auxiliaryfunctions.get_bodyparts(cfg)),
                ["x", "y", "z"],
            ]
            cols_names = ["scorer", "bodyparts", "coords"]
            flag_indiv_single = False
            if cfg.get("multianimalproject"):
                cols_names.insert(1, "individuals")
                if "single" == individuals[-1]:
                    individuals = individuals[:-1]
                    columns_unique = pd.MultiIndex.from_product(
                        [
                            [scorer_3d],
                            ["single"],
                            auxiliaryfunctions.get_unique_bodyparts(cfg),
                            ["x", "y", "z"],
                        ],
                        names=cols_names,
                    )
                    flag_indiv_single = True
                cols.insert(1, individuals)
            columns = pd.MultiIndex.from_product(cols, names=cols_names)
            if flag_indiv_single:
                columns = columns.append(columns_unique)
                individuals.append("single")

            inds = range(num_frames)

            # Swap num_animals with num_frames axes to ensure well-behaving reshape
            triangulate = triangulate.swapaxes(0, 1).reshape((num_frames, -1))

            # Fill up 3D dataframe
            df_3d = pd.DataFrame(triangulate, columns=columns, index=inds)

            df_3d.to_hdf(
                str(output_filename) + ".h5",
                key="df_with_missing",
                mode="w",
                format="table",
            )

            # Reorder 2D dataframe in view 2 to match order of view 1
            if cfg.get("multianimalproject"):
                df_2d_view2 = pd.read_hdf(dataname[1])
                individuals_order = [individuals[i] for i in list(voting.values())]
                df_2d_view2 = auxfun_multianimal.reorder_individuals_in_df(df_2d_view2, individuals_order)
                df_2d_view2.to_hdf(
                    dataname[1],
                    key="tracks",
                    format="table",
                    mode="w",
                )

            auxiliaryfunctions_3d.SaveMetadata3d(str(output_filename) + "_meta.pickle", metadata)

            if save_as_csv:
                df_3d.to_csv(str(output_filename) + ".csv")

            print("Triangulated data for video", video)
            print("Results are saved under: ", destfolder)
            # have to make the dest folder none so that it can be updated for a new pair of videos
            if destfolder == str(Path(video).parents[0]):
                destfolder = None

    if len(video_list) > 0:
        print("All videos were analyzed...")
        print("Now you can create 3D video(s) using deeplabcut.create_labeled_video_3d")