initial commit

This commit is contained in:
Tulis
2025-08-23 17:37:43 +02:00
commit ee25c170be
15 changed files with 1482 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
/mmdetection/
/mmpose/
/.ipynb_checkpoints/
/.gpu/
/.gpu-3d/
/.venv/

14
.idea/JustTwerk.iml generated Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
<excludeFolder url="file://$MODULE_DIR$/.venv1" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings">
<option name="format" value="GOOGLE" />
<option name="myDocStringFormat" value="Google" />
</component>
</module>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.13 (JustTwerk)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 virtualenv at C:\Users\Kajetan\PycharmProjects\JustTwerk\.gpu-3d" project-jdk-type="Python SDK" />
</project>

8
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/mmdetection" vcs="Git" />
<mapping directory="$PROJECT_DIR$/mmpose" vcs="Git" />
</component>
</project>

148
.idea/workspace.xml generated Normal file
View File

@ -0,0 +1,148 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="441a4e7b-d6ce-44cb-92c5-2f22f1b1874f" name="Changes" comment="initial commit">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/JustTwerk.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/inspectionProfiles/profiles_settings.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/02_whole_body_from_image.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/body3d.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/hrnet_w48_coco_256x192-b9e0b3ab_20200708.pth" afterDir="false" />
<change afterPath="$PROJECT_DIR$/humanPoseDetection.ipynb" afterDir="false" />
<change afterPath="$PROJECT_DIR$/is_torch.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/rtmdet_m_8xb32-100e_coco-obj365-person-235e8209.pth" afterDir="false" />
<change afterPath="$PROJECT_DIR$/rtmpose-m_simcc-body7_pt-body7_420e-256x192-e48f03d0_20230504.pth" afterDir="false" />
<change afterPath="$PROJECT_DIR$/test.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/videopose_h36m_243frames_fullconv_supervised_cpn_ft-88f5abbb_20210527.pth" afterDir="false" />
<change beforePath="$PROJECT_DIR$/mmpose/demo/body3d_pose_lifter_demo.py" beforeDir="false" afterPath="$PROJECT_DIR$/mmpose/demo/body3d_pose_lifter_demo.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Python Script" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectColorInfo">{
&quot;associatedIndex&quot;: 6
}</component>
<component name="ProjectId" id="31eO8f3HjfPWWrp80QbmhTb8V2s" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"ModuleVcsDetector.initialDetectionPerformed": "true",
"Python.02_whole_body_from_image.executor": "Run",
"Python.body3d.executor": "Run",
"Python.body3d_pose_lifter_demo.executor": "Run",
"Python.checkpoint.executor": "Run",
"Python.is_torch.executor": "Run",
"Python.local_visualizer_3d.executor": "Run",
"Python.openpose.executor": "Run",
"Python.test.executor": "Run",
"RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager.252": "true",
"RunOnceActivity.git.unshallow": "true",
"git-widget-placeholder": "main",
"last_opened_file_path": "C:/Users/Kajetan/PycharmProjects/JustTwerk",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm",
"settings.editor.selected.configurable": "editor.preferences.fonts.default",
"vue.rearranger.settings.migration": "true"
}
}]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="C:\Users\Kajetan\PycharmProjects\JustTwerk" />
</key>
</component>
<component name="RunManager">
<configuration name="test" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="JustTwerk" />
<option name="ENV_FILES" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/test.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Python.test" />
</list>
</recent_temporary>
</component>
<component name="SharedIndexes">
<attachedChunks>
<set>
<option value="bundled-js-predefined-d6986cc7102b-e03c56caf84a-JavaScript-PY-252.23892.515" />
<option value="bundled-python-sdk-7e47963ff851-f0eec537fc84-com.jetbrains.pycharm.pro.sharedIndexes.bundled-PY-252.23892.515" />
</set>
</attachedChunks>
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="441a4e7b-d6ce-44cb-92c5-2f22f1b1874f" name="Changes" comment="" />
<created>1755880914713</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1755880914713</updated>
<workItem from="1755881007482" duration="1783000" />
<workItem from="1755884695519" duration="705000" />
<workItem from="1755885461444" duration="2686000" />
<workItem from="1755888180570" duration="3107000" />
<workItem from="1755891319108" duration="23374000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="initial commit" />
<option name="LAST_COMMIT_MESSAGE" value="initial commit" />
</component>
<component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/JustTwerk$openpose.coverage" NAME="openpose Coverage Results" MODIFIED="1755886110615" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/human-pose-estimation-opencv" />
<SUITE FILE_PATH="coverage/JustTwerk$body3d_pose_lifter_demo.coverage" NAME="body3d_pose_lifter_demo Coverage Results" MODIFIED="1755937235510" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/mmpose/demo" />
<SUITE FILE_PATH="coverage/JustTwerk$body3d.coverage" NAME="body3d Coverage Results" MODIFIED="1755944498141" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
<SUITE FILE_PATH="coverage/JustTwerk$02_whole_body_from_image.coverage" NAME="02_whole_body_from_image Coverage Results" MODIFIED="1755885569302" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
<SUITE FILE_PATH="coverage/JustTwerk$local_visualizer_3d.coverage" NAME="local_visualizer_3d Coverage Results" MODIFIED="1755937454029" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/.gpu/Lib/site-packages/mmpose/visualization" />
<SUITE FILE_PATH="coverage/JustTwerk$checkpoint.coverage" NAME="checkpoint Coverage Results" MODIFIED="1755936916130" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/.gpu/Lib/site-packages/mmengine/runner" />
<SUITE FILE_PATH="coverage/JustTwerk$is_torch.coverage" NAME="is_torch Coverage Results" MODIFIED="1755943611769" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
<SUITE FILE_PATH="coverage/JustTwerk$test.coverage" NAME="test Coverage Results" MODIFIED="1755962675907" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
</component>
</project>

View File

@ -0,0 +1,70 @@
# From Python
# It requires OpenCV installed for Python
import sys
import cv2
import os
from sys import platform
import argparse
try:
# Import Openpose (Windows/Ubuntu/OSX)
dir_path = r"C:\Users\Kajetan\Documents\openpose/python"
try:
# Change these variables to point to the correct folder (Release/x64 etc.)
sys.path.append(dir_path + '/../bin/python/openpose/Release');
os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../x64/Release;' + dir_path + '/../bin;'
print(os.environ["PATH"])
import pyopenpose as op
except ImportError as e:
print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?')
raise e
# Flags
parser = argparse.ArgumentParser()
parser.add_argument("--image_path", default="../examples/media/COCO_val2014_000000000241.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).")
args = parser.parse_known_args()
# Custom Params (refer to include/openpose/flags.hpp for more parameters)
params = dict()
params["model_folder"] = "../models/"
params["face"] = True
params["hand"] = True
# Add others in path?
for i in range(0, len(args[1])):
curr_item = args[1][i]
if i != len(args[1])-1: next_item = args[1][i+1]
else: next_item = "1"
if "--" in curr_item and "--" in next_item:
key = curr_item.replace('-','')
if key not in params: params[key] = "1"
elif "--" in curr_item and "--" not in next_item:
key = curr_item.replace('-','')
if key not in params: params[key] = next_item
# Construct it from system arguments
# op.init_argv(args[1])
# oppython = op.OpenposePython()
# Starting OpenPose
opWrapper = op.WrapperPython()
opWrapper.configure(params)
opWrapper.start()
# Process Image
datum = op.Datum()
imageToProcess = cv2.imread(args[0].image_path)
datum.cvInputData = imageToProcess
opWrapper.emplaceAndPop(op.VectorDatum([datum]))
# Display Image
print("Body keypoints: \n" + str(datum.poseKeypoints))
print("Face keypoints: \n" + str(datum.faceKeypoints))
print("Left hand keypoints: \n" + str(datum.handKeypoints[0]))
print("Right hand keypoints: \n" + str(datum.handKeypoints[1]))
cv2.imshow("OpenPose 1.7.0 - Tutorial Python API", datum.cvOutputData)
cv2.waitKey(0)
except Exception as e:
print(e)
sys.exit(-1)

555
body3d.py Normal file
View File

@ -0,0 +1,555 @@
# Copyright (c) OpenMMLab. All rights reserved.
import logging
import mimetypes
import os
import time
from argparse import ArgumentParser
from functools import partial
import cv2
import json_tricks as json
import mmcv
import mmengine
import numpy as np
from mmengine.logging import print_log
from mmpose.apis import (_track_by_iou, _track_by_oks,
convert_keypoint_definition, extract_pose_sequence,
inference_pose_lifter_model, inference_topdown,
init_model)
from mmpose.models.pose_estimators import PoseLifter
from mmpose.models.pose_estimators.topdown import TopdownPoseEstimator
from mmpose.registry import VISUALIZERS
from mmpose.structures import (PoseDataSample, merge_data_samples,
split_instances)
from mmpose.utils import adapt_mmdet_pipeline
try:
from mmdet.apis import inference_detector, init_detector
has_mmdet = True
except (ImportError, ModuleNotFoundError):
has_mmdet = False
def parse_args():
parser = ArgumentParser()
parser.add_argument('--det_config', default="mmpose/demo/mmdetection_cfg/rtmdet_m_640-8xb32_coco-person.py", help='Config file for detection')
parser.add_argument('--det_checkpoint', default="rtmdet_m_8xb32-100e_coco-obj365-person-235e8209.pth", help='Checkpoint file for detection')
parser.add_argument(
'--pose_estimator_config',
type=str,
default="mmpose/configs/body_2d_keypoint/rtmpose/body8/rtmpose-m_8xb256-420e_body8-256x192.py",
help='Config file for the 1st stage 2D pose estimator')
parser.add_argument(
'--pose_estimator_checkpoint',
type=str,
default="rtmpose-m_simcc-body7_pt-body7_420e-256x192-e48f03d0_20230504.pth",
help='Checkpoint file for the 1st stage 2D pose estimator')
parser.add_argument(
'--pose_lifter_config',
default="mmpose/configs/body_3d_keypoint/video_pose_lift/h36m/video-pose-lift_tcn-243frm-supv-cpn-ft_8xb128-200e_h36m.py",
help='Config file for the 2nd stage pose lifter model')
parser.add_argument(
'--pose_lifter_checkpoint',
default="videopose_h36m_243frames_fullconv_supervised_cpn_ft-88f5abbb_20210527.pth",
help='Checkpoint file for the 2nd stage pose lifter model')
parser.add_argument('--input', type=str, default='webcam', help='Video path')
parser.add_argument(
'--show',
action='store_true',
default=True,
help='Whether to show visualizations')
parser.add_argument(
'--disable-rebase-keypoint',
action='store_true',
default=False,
help='Whether to disable rebasing the predicted 3D pose so its '
'lowest keypoint has a height of 0 (landing on the ground). Rebase '
'is useful for visualization when the model do not predict the '
'global position of the 3D pose.')
parser.add_argument(
'--disable-norm-pose-2d',
action='store_true',
default=False,
help='Whether to scale the bbox (along with the 2D pose) to the '
'average bbox scale of the dataset, and move the bbox (along with the '
'2D pose) to the average bbox center of the dataset. This is useful '
'when bbox is small, especially in multi-person scenarios.')
parser.add_argument(
'--num-instances',
type=int,
default=1,
help='The number of 3D poses to be visualized in every frame. If '
'less than 0, it will be set to the number of pose results in the '
'first frame.')
parser.add_argument(
'--output-root',
type=str,
default='',
help='Root of the output video file. '
'Default not saving the visualization video.')
parser.add_argument(
'--save-predictions',
action='store_true',
default=False,
help='Whether to save predicted results')
parser.add_argument(
'--device', default='cuda:0', help='Device used for inference')
parser.add_argument(
'--det-cat-id',
type=int,
default=0,
help='Category id for bounding box detection model')
parser.add_argument(
'--bbox-thr',
type=float,
default=0.3,
help='Bounding box score threshold')
parser.add_argument('--kpt-thr', type=float, default=0.3)
parser.add_argument(
'--use-oks-tracking', action='store_true', help='Using OKS tracking')
parser.add_argument(
'--tracking-thr', type=float, default=0.3, help='Tracking threshold')
parser.add_argument(
'--show-interval', type=int, default=0, help='Sleep seconds per frame')
parser.add_argument(
'--thickness',
type=int,
default=1,
help='Link thickness for visualization')
parser.add_argument(
'--radius',
type=int,
default=3,
help='Keypoint radius for visualization')
parser.add_argument(
'--online',
action='store_true',
default=False,
help='Inference mode. If set to True, can not use future frame'
'information when using multi frames for inference in the 2D pose'
'detection stage. Default: False.')
args = parser.parse_args()
return args
def process_one_image(args, detector, frame, frame_idx, pose_estimator,
pose_est_results_last, pose_est_results_list, next_id,
pose_lifter, visualize_frame, visualizer):
"""Visualize detected and predicted keypoints of one image.
Pipeline of this function:
frame
|
V
+-----------------+
| detector |
+-----------------+
| det_result
V
+-----------------+
| pose_estimator |
+-----------------+
| pose_est_results
V
+--------------------------------------------+
| convert 2d kpts into pose-lifting format |
+--------------------------------------------+
| pose_est_results_list
V
+-----------------------+
| extract_pose_sequence |
+-----------------------+
| pose_seq_2d
V
+-------------+
| pose_lifter |
+-------------+
| pose_lift_results
V
+-----------------+
| post-processing |
+-----------------+
| pred_3d_data_samples
V
+------------+
| visualizer |
+------------+
Args:
args (Argument): Custom command-line arguments.
detector (mmdet.BaseDetector): The mmdet detector.
frame (np.ndarray): The image frame read from input image or video.
frame_idx (int): The index of current frame.
pose_estimator (TopdownPoseEstimator): The pose estimator for 2d pose.
pose_est_results_last (list(PoseDataSample)): The results of pose
estimation from the last frame for tracking instances.
pose_est_results_list (list(list(PoseDataSample))): The list of all
pose estimation results converted by
``convert_keypoint_definition`` from previous frames. In
pose-lifting stage it is used to obtain the 2d estimation sequence.
next_id (int): The next track id to be used.
pose_lifter (PoseLifter): The pose-lifter for estimating 3d pose.
visualize_frame (np.ndarray): The image for drawing the results on.
visualizer (Visualizer): The visualizer for visualizing the 2d and 3d
pose estimation results.
Returns:
pose_est_results (list(PoseDataSample)): The pose estimation result of
the current frame.
pose_est_results_list (list(list(PoseDataSample))): The list of all
converted pose estimation results until the current frame.
pred_3d_instances (InstanceData): The result of pose-lifting.
Specifically, the predicted keypoints and scores are saved at
``pred_3d_instances.keypoints`` and
``pred_3d_instances.keypoint_scores``.
next_id (int): The next track id to be used.
"""
pose_lift_dataset = pose_lifter.cfg.test_dataloader.dataset
pose_lift_dataset_name = pose_lifter.dataset_meta['dataset_name']
# First stage: conduct 2D pose detection in a Topdown manner
# use detector to obtain person bounding boxes
det_result = inference_detector(detector, frame)
pred_instance = det_result.pred_instances.cpu().numpy()
# filter out the person instances with category and bbox threshold
# e.g. 0 for person in COCO
bboxes = pred_instance.bboxes
bboxes = bboxes[np.logical_and(pred_instance.labels == args.det_cat_id,
pred_instance.scores > args.bbox_thr)]
# estimate pose results for current image
pose_est_results = inference_topdown(pose_estimator, frame, bboxes)
if args.use_oks_tracking:
_track = partial(_track_by_oks)
else:
_track = _track_by_iou
pose_det_dataset_name = pose_estimator.dataset_meta['dataset_name']
pose_est_results_converted = []
# convert 2d pose estimation results into the format for pose-lifting
# such as changing the keypoint order, flipping the keypoint, etc.
for i, data_sample in enumerate(pose_est_results):
pred_instances = data_sample.pred_instances.cpu().numpy()
keypoints = pred_instances.keypoints
# calculate area and bbox
if 'bboxes' in pred_instances:
areas = np.array([(bbox[2] - bbox[0]) * (bbox[3] - bbox[1])
for bbox in pred_instances.bboxes])
pose_est_results[i].pred_instances.set_field(areas, 'areas')
else:
areas, bboxes = [], []
for keypoint in keypoints:
xmin = np.min(keypoint[:, 0][keypoint[:, 0] > 0], initial=1e10)
xmax = np.max(keypoint[:, 0])
ymin = np.min(keypoint[:, 1][keypoint[:, 1] > 0], initial=1e10)
ymax = np.max(keypoint[:, 1])
areas.append((xmax - xmin) * (ymax - ymin))
bboxes.append([xmin, ymin, xmax, ymax])
pose_est_results[i].pred_instances.areas = np.array(areas)
pose_est_results[i].pred_instances.bboxes = np.array(bboxes)
# track id
track_id, pose_est_results_last, _ = _track(data_sample,
pose_est_results_last,
args.tracking_thr)
if track_id == -1:
if np.count_nonzero(keypoints[:, :, 1]) >= 3:
track_id = next_id
next_id += 1
else:
# If the number of keypoints detected is small,
# delete that person instance.
keypoints[:, :, 1] = -10
pose_est_results[i].pred_instances.set_field(
keypoints, 'keypoints')
pose_est_results[i].pred_instances.set_field(
pred_instances.bboxes * 0, 'bboxes')
pose_est_results[i].set_field(pred_instances, 'pred_instances')
track_id = -1
pose_est_results[i].set_field(track_id, 'track_id')
# convert keypoints for pose-lifting
pose_est_result_converted = PoseDataSample()
pose_est_result_converted.set_field(
pose_est_results[i].pred_instances.clone(), 'pred_instances')
pose_est_result_converted.set_field(
pose_est_results[i].gt_instances.clone(), 'gt_instances')
keypoints = convert_keypoint_definition(keypoints,
pose_det_dataset_name,
pose_lift_dataset_name)
pose_est_result_converted.pred_instances.set_field(
keypoints, 'keypoints')
pose_est_result_converted.set_field(pose_est_results[i].track_id,
'track_id')
pose_est_results_converted.append(pose_est_result_converted)
pose_est_results_list.append(pose_est_results_converted.copy())
# Second stage: Pose lifting
# extract and pad input pose2d sequence
pose_seq_2d = extract_pose_sequence(
pose_est_results_list,
frame_idx=frame_idx,
causal=pose_lift_dataset.get('causal', False),
seq_len=pose_lift_dataset.get('seq_len', 1),
step=pose_lift_dataset.get('seq_step', 1))
# conduct 2D-to-3D pose lifting
norm_pose_2d = not args.disable_norm_pose_2d
pose_lift_results = inference_pose_lifter_model(
pose_lifter,
pose_seq_2d,
image_size=visualize_frame.shape[:2],
norm_pose_2d=norm_pose_2d)
# post-processing
for idx, pose_lift_result in enumerate(pose_lift_results):
pose_lift_result.track_id = pose_est_results[idx].get('track_id', 1e4)
pred_instances = pose_lift_result.pred_instances
keypoints = pred_instances.keypoints
keypoint_scores = pred_instances.keypoint_scores
if keypoint_scores.ndim == 3:
keypoint_scores = np.squeeze(keypoint_scores, axis=1)
pose_lift_results[
idx].pred_instances.keypoint_scores = keypoint_scores
if keypoints.ndim == 4:
keypoints = np.squeeze(keypoints, axis=1)
keypoints = keypoints[..., [0, 2, 1]]
keypoints[..., 0] = -keypoints[..., 0]
keypoints[..., 2] = -keypoints[..., 2]
# rebase height (z-axis)
if not args.disable_rebase_keypoint:
keypoints[..., 2] -= np.min(
keypoints[..., 2], axis=-1, keepdims=True)
pose_lift_results[idx].pred_instances.keypoints = keypoints
pose_lift_results = sorted(
pose_lift_results, key=lambda x: x.get('track_id', 1e4))
pred_3d_data_samples = merge_data_samples(pose_lift_results)
det_data_sample = merge_data_samples(pose_est_results)
pred_3d_instances = pred_3d_data_samples.get('pred_instances', None)
if args.num_instances < 0:
args.num_instances = len(pose_lift_results)
# Visualization
if visualizer is not None:
visualizer.add_datasample(
'result',
visualize_frame,
data_sample=pred_3d_data_samples,
det_data_sample=det_data_sample,
draw_gt=False,
dataset_2d=pose_det_dataset_name,
dataset_3d=pose_lift_dataset_name,
show=args.show,
draw_bbox=True,
kpt_thr=args.kpt_thr,
num_instances=args.num_instances,
wait_time=args.show_interval)
return pose_est_results, pose_est_results_list, pred_3d_instances, next_id
def main():
assert has_mmdet, 'Please install mmdet to run the demo.'
args = parse_args()
assert args.show or (args.output_root != '')
assert args.input != ''
assert args.det_config is not None
assert args.det_checkpoint is not None
detector = init_detector(
args.det_config, args.det_checkpoint, device=args.device.lower())
detector.cfg = adapt_mmdet_pipeline(detector.cfg)
pose_estimator = init_model(
args.pose_estimator_config,
args.pose_estimator_checkpoint,
device=args.device.lower())
assert isinstance(pose_estimator, TopdownPoseEstimator), 'Only "TopDown"' \
'model is supported for the 1st stage (2D pose detection)'
det_kpt_color = pose_estimator.dataset_meta.get('keypoint_colors', None)
det_dataset_skeleton = pose_estimator.dataset_meta.get(
'skeleton_links', None)
det_dataset_link_color = pose_estimator.dataset_meta.get(
'skeleton_link_colors', None)
pose_lifter = init_model(
args.pose_lifter_config,
args.pose_lifter_checkpoint,
device=args.device.lower())
assert isinstance(pose_lifter, PoseLifter), \
'Only "PoseLifter" model is supported for the 2nd stage ' \
'(2D-to-3D lifting)'
pose_lifter.cfg.visualizer.radius = args.radius
pose_lifter.cfg.visualizer.line_width = args.thickness
pose_lifter.cfg.visualizer.det_kpt_color = det_kpt_color
pose_lifter.cfg.visualizer.det_dataset_skeleton = det_dataset_skeleton
pose_lifter.cfg.visualizer.det_dataset_link_color = det_dataset_link_color
visualizer = VISUALIZERS.build(pose_lifter.cfg.visualizer)
# the dataset_meta is loaded from the checkpoint
visualizer.set_dataset_meta(pose_lifter.dataset_meta)
if args.input == 'webcam':
input_type = 'webcam'
else:
input_type = mimetypes.guess_type(args.input)[0].split('/')[0]
if args.output_root == '':
save_output = False
else:
mmengine.mkdir_or_exist(args.output_root)
output_file = os.path.join(args.output_root,
os.path.basename(args.input))
if args.input == 'webcam':
output_file += '.mp4'
save_output = True
if args.save_predictions:
assert args.output_root != ''
args.pred_save_path = f'{args.output_root}/results_' \
f'{os.path.splitext(os.path.basename(args.input))[0]}.json'
if save_output:
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
pose_est_results_list = []
pred_instances_list = []
if input_type == 'image':
frame = mmcv.imread(args.input, channel_order='rgb')
_, _, pred_3d_instances, _ = process_one_image(
args=args,
detector=detector,
frame=frame,
frame_idx=0,
pose_estimator=pose_estimator,
pose_est_results_last=[],
pose_est_results_list=pose_est_results_list,
next_id=0,
pose_lifter=pose_lifter,
visualize_frame=frame,
visualizer=visualizer)
if args.save_predictions:
# save prediction results
pred_instances_list = split_instances(pred_3d_instances)
if save_output:
frame_vis = visualizer.get_image()
mmcv.imwrite(mmcv.rgb2bgr(frame_vis), output_file)
elif input_type in ['webcam', 'video']:
next_id = 0
pose_est_results = []
if args.input == 'webcam':
video = cv2.VideoCapture(0)
else:
video = cv2.VideoCapture(args.input)
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
if int(major_ver) < 3:
fps = video.get(cv2.cv.CV_CAP_PROP_FPS)
else:
fps = video.get(cv2.CAP_PROP_FPS)
video_writer = None
frame_idx = 0
while video.isOpened():
success, frame = video.read()
frame_idx += 1
if not success:
break
pose_est_results_last = pose_est_results
# First stage: 2D pose detection
# make person results for current image
(pose_est_results, pose_est_results_list, pred_3d_instances,
next_id) = process_one_image(
args=args,
detector=detector,
frame=frame,
frame_idx=frame_idx,
pose_estimator=pose_estimator,
pose_est_results_last=pose_est_results_last,
pose_est_results_list=pose_est_results_list,
next_id=next_id,
pose_lifter=pose_lifter,
visualize_frame=mmcv.bgr2rgb(frame),
visualizer=visualizer)
if args.save_predictions:
# save prediction results
pred_instances_list.append(
dict(
frame_id=frame_idx,
instances=split_instances(pred_3d_instances)))
if save_output:
frame_vis = visualizer.get_image()
if video_writer is None:
# the size of the image with visualization may vary
# depending on the presence of heatmaps
video_writer = cv2.VideoWriter(output_file, fourcc, fps,
(frame_vis.shape[1],
frame_vis.shape[0]))
video_writer.write(mmcv.rgb2bgr(frame_vis))
if args.show:
# press ESC to exit
if cv2.waitKey(5) & 0xFF == 27:
break
time.sleep(args.show_interval)
video.release()
if video_writer:
video_writer.release()
else:
args.save_predictions = False
raise ValueError(
f'file {os.path.basename(args.input)} has invalid format.')
if args.save_predictions:
with open(args.pred_save_path, 'w') as f:
json.dump(
dict(
meta_info=pose_lifter.dataset_meta,
instance_info=pred_instances_list),
f,
indent='\t')
print(f'predictions have been saved at {args.pred_save_path}')
if save_output:
input_type = input_type.replace('webcam', 'video')
print_log(
f'the output {input_type} has been saved at {output_file}',
logger='current',
level=logging.INFO)
if __name__ == '__main__':
main()

BIN
hrnet_w48_coco_256x192-b9e0b3ab_20200708.pth (Stored with Git LFS) Normal file

Binary file not shown.

300
humanPoseDetection.ipynb Normal file

File diff suppressed because one or more lines are too long

3
is_torch.py Normal file
View File

@ -0,0 +1,3 @@
import torch
print(torch.cuda.is_available())

BIN
rtmdet_m_8xb32-100e_coco-obj365-person-235e8209.pth (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

353
test.py Normal file
View File

@ -0,0 +1,353 @@
# Copyright (c) OpenMMLab. All rights reserved.
import logging
import time
import cv2
from argparse import ArgumentParser
from mmengine.logging import print_log
from mmpose.apis import inference_topdown, init_model
from mmpose.registry import VISUALIZERS
from mmpose.structures import merge_data_samples
import numpy as np
class Position:
def __init__(self, person_kpts):
if len(person_kpts) == 8:
self.left_elbow_angle = person_kpts[0]
self.right_elbow_angle = person_kpts[1]
self.left_shoulder_angle = person_kpts[2]
self.right_shoulder_angle = person_kpts[3]
self.left_leg_angle = person_kpts[4]
self.right_leg_angle = person_kpts[5]
self.left_hip_angle = person_kpts[6]
self.right_hip_angle = person_kpts[7]
else:
self.left_hip = person_kpts[11]
self.left_knee = person_kpts[13]
self.left_ankle = person_kpts[15]
self.right_hip = person_kpts[12]
self.right_knee = person_kpts[14]
self.right_ankle = person_kpts[16]
self.left_shoulder = person_kpts[5]
self.left_elbow = person_kpts[7]
self.left_wrist = person_kpts[9]
self.right_shoulder = person_kpts[6]
self.right_elbow = person_kpts[8]
self.right_wrist = person_kpts[10]
self.left_elbow_angle = angle_between(self.left_shoulder - self.left_elbow, self.left_wrist - self.left_elbow)
self.right_elbow_angle = angle_between(self.right_shoulder - self.right_elbow, self.right_wrist - self.right_elbow)
self.left_shoulder_angle = angle_between(self.right_shoulder - self.left_shoulder, self.left_elbow - self.left_shoulder)
self.right_shoulder_angle = angle_between(self.left_shoulder - self.right_shoulder, self.right_elbow - self.right_shoulder)
self.left_leg_angle = angle_between(self.left_hip - self.left_knee, self.left_ankle - self.left_knee)
self.right_leg_angle = angle_between(self.right_hip - self.right_knee, self.right_ankle - self.right_knee)
self.left_hip_angle = angle_between(self.left_shoulder - self.left_hip, self.left_knee - self.left_hip)
self.right_hip_angle = angle_between(self.right_shoulder - self.right_hip, self.right_knee - self.right_hip)
def to_array(self):
return [
self.left_elbow_angle,
self.right_elbow_angle,
self.left_shoulder_angle,
self.right_shoulder_angle,
self.left_leg_angle,
self.right_leg_angle,
self.left_hip_angle,
self.right_hip_angle
]
def distance_to(self, position):
error = 0
success = True
for i, x in enumerate(self.to_array()):
if 3 < i < 6:
continue
y = position.to_array()[i]
if abs(y) > 165:
x = abs(x)
y = abs(y)
dist = abs(y - x)
if dist > 20:
success = False
# print(f"{i} nie jest ok: moje: {x}, cel: {y}")
error += abs(y - x)
else:
pass
# print(f"{i} jest ok: moje: {x}, cel: {position.to_array()[i]}")
return (success, error)
def parse_args():
parser = ArgumentParser()
# parser.add_argument('checkpoint', help='Checkpoint file')
parser.add_argument(
'--device', default='cuda:0', help='Device used for inference')
parser.add_argument(
'--draw-heatmap',
action='store_true',
help='Visualize the predicted heatmap')
parser.add_argument(
'--show-kpt-idx',
action='store_true',
default=False,
help='Whether to show the index of keypoints')
parser.add_argument(
'--skeleton-style',
default='mmpose',
type=str,
choices=['mmpose', 'openpose'],
help='Skeleton style selection')
parser.add_argument(
'--kpt-thr',
type=float,
default=0.3,
help='Visualizing keypoint thresholds')
parser.add_argument(
'--radius',
type=int,
default=3,
help='Keypoint radius for visualization')
parser.add_argument(
'--thickness',
type=int,
default=1,
help='Link thickness for visualization')
parser.add_argument(
'--alpha', type=float, default=0.8, help='The transparency of bboxes')
parser.add_argument(
'--camera-id', type=int, default=0, help='Camera device ID')
args = parser.parse_args()
return args
def angle_between(v1, v2):
"""
Liczy kąt między wektorami v1 i v2 w stopniach.
Znak kąta zależy od kierunku (przeciwnie do ruchu wskazówek zegara jest dodatni).
"""
# kąt w radianach
angle = np.arccos(np.clip(np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)), -1.0, 1.0))
# obliczamy znak kąta w 2D: + jeśli v2 jest "po lewej" od v1
sign = np.sign(v1[0] * v2[1] - v1[1] * v2[0])
return np.degrees(angle) * sign
def is_visible(kpt, threshold=0.3):
# kpt = [x, y, score]
return kpt[2] > threshold
def main():
args = parse_args()
# build the model from a config file and a checkpoint file
if args.draw_heatmap:
cfg_options = dict(model=dict(test_cfg=dict(output_heatmaps=True)))
else:
cfg_options = None
model = init_model(
"mmpose/configs/body_2d_keypoint/topdown_heatmap/coco/td-hm_hrnet-w48_8xb32-210e_coco-256x192.py",
"hrnet_w48_coco_256x192-b9e0b3ab_20200708.pth",
device=args.device,
cfg_options=cfg_options)
# init visualizer
model.cfg.visualizer.radius = args.radius
model.cfg.visualizer.alpha = args.alpha
model.cfg.visualizer.line_width = args.thickness
visualizer = VISUALIZERS.build(model.cfg.visualizer)
visualizer.set_dataset_meta(
model.dataset_meta, skeleton_style=args.skeleton_style)
# start capturing video from camera
cap = cv2.VideoCapture(args.camera_id)
if not cap.isOpened():
print("Error: Cannot open camera")
return
lastDegs = []
lastMove = 0
visible = 0
maha = 0
start = False
oldpos = None
waiting = 100
positions = []
numPositions = 0
tPose = Position(
[
167.5568,
-161.67027,
-166.49443,
168.22028,
110.21745,
166.41733,
167.57822,
-176.08066
]
)
krakowiak = Position(
[np.float32(-98.226715), np.float32(106.39389), np.float32(-135.05656), np.float32(139.48904), np.float32(-149.9036), np.float32(8.216028), np.float32(174.70923), np.float32(-176.893)]
)
krakowiakRight = Position(
[np.float32(-110.61421), np.float32(-174.2424), np.float32(-127.05916), np.float32(174.9463),
np.float32(175.62007), np.float32(166.89127), np.float32(168.8219), np.float32(-178.02744)]
)
while True:
ret, frame = cap.read()
if not ret:
print("Error: Cannot read frame from camera")
break
# convert BGR to RGB
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# inference
batch_results = inference_topdown(model, img)
results = merge_data_samples(batch_results)
person_kpts = results.pred_instances.keypoints[0]
person_visible = results.pred_instances.keypoints_visible[0]
left_hand_visible = person_visible[5] > 0.75 and person_visible[5] > 0.75 and person_visible[9] > 0.75
right_hand_visible = person_visible[6] > 0.75 and person_visible[8] > 0.75 and person_visible[10] > 0.75
left_leg_visible = person_visible[11] > 0.75 and person_visible[13] > 0.75 and person_visible[15] > 0.75
right_leg_visible = person_visible[12] > 0.75 and person_visible[14] > 0.75 and person_visible[16] > 0.75
position = Position(person_kpts)
#
if position.distance_to(tPose)[0]:
print("\rT POSE!", end="")
elif position.distance_to(krakowiak)[0]:
print("\rKrakowiak", end="")
elif position.distance_to(krakowiakRight)[0]:
print("\rKrakowiak right", end="")
else:
print("\rNIC!", end="")
# print(position.left_elbow_angle)
# if oldpos is None:
# oldpos = position
#
# if oldpos.distance_to(position) < 7.5:
# print(f"\r{goalPosition.distance_to(position)}", end="")
#
# oldpos = position
# if error > 200:
# if start:
# if numPositions > 100:
# avgPosition = [0, 0, 0, 0, 0, 0, 0, 0]
#
# for element in positions:
# for i, data in enumerate(element):
# avgPosition[i] += data
#
# for i, element in enumerate(avgPosition):
# avgPosition[i] = avgPosition[i] / numPositions
#
# print(avgPosition)
#
# break
# else:
# print(f"\r{numPositions}", end="")
# if oldpos is None:
# oldpos = position
#
# if oldpos.distance_to(position)[0]:
# positions.insert(0, position.to_array())
# numPositions += 1
#
# oldpos = position
#
# # else:
# # print(f"\rOK!", end="")
#
# if waiting != 0 and not start:
# print(f"\r{waiting}", end="")
# waiting -= 1
# else:
# start = True
# lastDegs.insert(0, (left_elbow_angle, right_elbow_angle))
#
# last = 0
# lastCount = 0
#
# for element in lastDegs:
# last += element[1]
# lastCount += 1
#
# last = last / lastCount
# dist = right_elbow_angle - last
#
# if not right_visible:
# visible = 0
# print("\rNie widać prawej ręki!!!!!!!!", end="")
# else:
# if maha == 0:
# print("\rWidać rękę, nie maha!", end="")
# else:
# maha -= 1
#
# visible += 1
#
# if 15 < abs(dist) < 60 and visible > 5:
# if lastMove != dist > 0:
# maha = 10
# print("\rmaha!", end="")
#
# lastMove = dist > 0
#
# if len(lastDegs) > 5:
# lastDegs.pop()
# visualize
vis_img = visualizer.add_datasample(
'result',
img,
data_sample=results,
draw_gt=False,
draw_bbox=True,
kpt_thr=args.kpt_thr,
draw_heatmap=args.draw_heatmap,
show_kpt_idx=args.show_kpt_idx,
skeleton_style=args.skeleton_style,
show=False,
out_file=None)
# convert RGB back to BGR for OpenCV
vis_img = cv2.cvtColor(vis_img, cv2.COLOR_RGB2BGR)
cv2.imshow('Live Pose Estimation', vis_img)
# press 'q' to quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()

Binary file not shown.