<reponame>vijay4313/proxemo
#!/usr/bin/env python
# Title           :loader.py
# Author          :<NAME>, <NAME>, <NAME>, <NAME>, <NAME>
# Copyright       :"Copyright 2020, Proxemo project"
# Version         :1.0
# License         :"MIT"
# Maintainer      :<NAME>, <NAME>
# Email           :<EMAIL>, <EMAIL>
# ==============================================================================
import argparse
import os
import platform
from pprint import pprint
import cv2

keypoint_ids = [
    (1, 2),
    (1, 5),
    (2, 3),
    (3, 4),
    (5, 6),
    (6, 7),
    (1, 8),
    (8, 9),
    (9, 10),
    (1, 11),
    (11, 12),
    (12, 13),
    (1, 0),
    (0, 14),
    (14, 16),
    (0, 15),
    (15, 17),
]


def default_log_dir():
    """Get default cubemos log dir.

    Raises:
        Exception: Works only on Windows/Linux

    Returns:
        [str]: Default log directory
    """
    if platform.system() == "Windows":
        return os.path.join(os.environ["LOCALAPPDATA"], "Cubemos", "SkeletonTracking", "logs")
    elif platform.system() == "Linux":
        return os.path.join(os.environ["HOME"], ".cubemos", "skeleton_tracking", "logs")
    else:
        raise Exception("{} is not supported".format(platform.system()))


def default_license_dir():
    """Get license file location.

    Raises:
        Exception: Works only on Windows/Linux

    Returns:
        [str]: License file directory
    """
    if platform.system() == "Windows":
        return os.path.join(os.environ["LOCALAPPDATA"], "Cubemos", "SkeletonTracking", "license")
    elif platform.system() == "Linux":
        return os.path.join(os.environ["HOME"], ".cubemos", "skeleton_tracking", "license")
    else:
        raise Exception("{} is not supported".format(platform.system()))


def check_license_and_variables_exist():
    """Check license file."""
    license_path = os.path.join(default_license_dir(), "cubemos_license.json")
    if not os.path.isfile(license_path):
        raise Exception(
            "The license file has not been found at location \"" +
            default_license_dir() + "\". "
            "Please have a look at the Getting Started Guide on how to "
            "use the post-installation script to generate the license file")
    if "CUBEMOS_SKEL_SDK" not in os.environ:
        raise Exception(
            "The environment Variable \"CUBEMOS_SKEL_SDK\" is not set. "
            "Please check the troubleshooting section in the Getting "
            "Started Guide to resolve this issue."
        )


def get_valid_limbs(keypoint_ids, skeleton, confidence_threshold):
    """Extract limbs based on confidence

    Args:
        keypoint_ids (list(tuple)): Joint ID pair (limbs)
        skeleton (obj): Skeleton joint points
        confidence_threshold (float): Threshold

    Returns:
        [lsit]: valid limbs (connections)
    """
    limbs = [
        (tuple(map(int, skeleton.joints[i])),
         tuple(map(int, skeleton.joints[v])))
        for (i, v) in keypoint_ids
        if skeleton.confidences[i] >= confidence_threshold
        and skeleton.confidences[v] >= confidence_threshold
    ]
    valid_limbs = [
        limb
        for limb in limbs
        if limb[0][0] >= 0 and limb[0][1] >= 0 and limb[1][0] >= 0 and limb[1][1] >= 0
    ]
    return valid_limbs


def render_result(skeletons, img, confidence_threshold):
    """Generate skeleton overlay on image (inplace).

    Args:
        skeletons (dict): skeleton joint co-ordinates
        img (np.array): source image
        confidence_threshold (float): Threshold
    """
    skeleton_color = (100, 254, 213)
    for index, skeleton in enumerate(skeletons):
        limbs = get_valid_limbs(keypoint_ids, skeleton, confidence_threshold)
        for limb in limbs:
            cv2.line(
                img, limb[0], limb[1], skeleton_color, thickness=2, lineType=cv2.LINE_AA
            )
#!/usr/bin/env python
# Title           :loader.py
# Author          :<NAME>, <NAME>, <NAME>, <NAME>, <NAME>
# Copyright       :"Copyright 2020, Proxemo project"
# Version         :1.0
# License         :"MIT"
# Maintainer      :<NAME>, <NAME>
# Email           :<EMAIL>, <EMAIL>
# ==============================================================================
import argparse
import os
import platform
from pprint import pprint
import cv2

keypoint_ids = [
    (1, 2),
    (1, 5),
    (2, 3),
    (3, 4),
    (5, 6),
    (6, 7),
    (1, 8),
    (8, 9),
    (9, 10),
    (1, 11),
    (11, 12),
    (12, 13),
    (1, 0),
    (0, 14),
    (14, 16),
    (0, 15),
    (15, 17),
]


def default_log_dir():
    """Get default cubemos log dir.

    Raises:
        Exception: Works only on Windows/Linux

    Returns:
        [str]: Default log directory
    """
    if platform.system() == "Windows":
        return os.path.join(os.environ["LOCALAPPDATA"], "Cubemos", "SkeletonTracking", "logs")
    elif platform.system() == "Linux":
        return os.path.join(os.environ["HOME"], ".cubemos", "skeleton_tracking", "logs")
    else:
        raise Exception("{} is not supported".format(platform.system()))


def default_license_dir():
    """Get license file location.

    Raises:
        Exception: Works only on Windows/Linux

    Returns:
        [str]: License file directory
    """
    if platform.system() == "Windows":
        return os.path.join(os.environ["LOCALAPPDATA"], "Cubemos", "SkeletonTracking", "license")
    elif platform.system() == "Linux":
        return os.path.join(os.environ["HOME"], ".cubemos", "skeleton_tracking", "license")
    else:
        raise Exception("{} is not supported".format(platform.system()))


def check_license_and_variables_exist():
    """Check license file."""
    license_path = os.path.join(default_license_dir(), "cubemos_license.json")
    if not os.path.isfile(license_path):
        raise Exception(
            "The license file has not been found at location \"" +
            default_license_dir() + "\". "
            "Please have a look at the Getting Started Guide on how to "
            "use the post-installation script to generate the license file")
    if "CUBEMOS_SKEL_SDK" not in os.environ:
        raise Exception(
            "The environment Variable \"CUBEMOS_SKEL_SDK\" is not set. "
            "Please check the troubleshooting section in the Getting "
            "Started Guide to resolve this issue."
        )


def get_valid_limbs(keypoint_ids, skeleton, confidence_threshold):
    """Extract limbs based on confidence

    Args:
        keypoint_ids (list(tuple)): Joint ID pair (limbs)
        skeleton (obj): Skeleton joint points
        confidence_threshold (float): Threshold

    Returns:
        [lsit]: valid limbs (connections)
    """
    limbs = [
        (tuple(map(int, skeleton.joints[i])),
         tuple(map(int, skeleton.joints[v])))
        for (i, v) in keypoint_ids
        if skeleton.confidences[i] >= confidence_threshold
        and skeleton.confidences[v] >= confidence_threshold
    ]
    valid_limbs = [
        limb
        for limb in limbs
        if limb[0][0] >= 0 and limb[0][1] >= 0 and limb[1][0] >= 0 and limb[1][1] >= 0
    ]
    return valid_limbs


def render_result(skeletons, img, confidence_threshold):
    """Generate skeleton overlay on image (inplace).

    Args:
        skeletons (dict): skeleton joint co-ordinates
        img (np.array): source image
        confidence_threshold (float): Threshold
    """
    skeleton_color = (100, 254, 213)
    for index, skeleton in enumerate(skeletons):
        limbs = get_valid_limbs(keypoint_ids, skeleton, confidence_threshold)
        for limb in limbs:
            cv2.line(
                img, limb[0], limb[1], skeleton_color, thickness=2, lineType=cv2.LINE_AA
            )
#!/usr/bin/env python # Title           :loader.py # Author          :<NAME>, <NAME>, <NAME>, <NAME>, <NAME> # Copyright       :"Copyright 2020, Proxemo project" # Version         :1.0 # License         :"MIT" # Maintainer      :<NAME>, <NAME> # Email           :<EMAIL>, <EMAIL> # ============================================================================== Get default cubemos log dir.      Raises:         Exception: Works only on Windows/Linux      Returns:         [str]: Default log directory Get license file location.      Raises:         Exception: Works only on Windows/Linux      Returns:         [str]: License file directory Check license file. Extract limbs based on confidence      Args:         keypoint_ids (list(tuple)): Joint ID pair (limbs)         skeleton (obj): Skeleton joint points         confidence_threshold (float): Threshold      Returns:         [lsit]: valid limbs (connections) Generate skeleton overlay on image (inplace).      Args:         skeletons (dict): skeleton joint co-ordinates         img (np.array): source image         confidence_threshold (float): Threshold