diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..0920c0b --- /dev/null +++ b/config.yml @@ -0,0 +1,16 @@ +# Input +address_input: "0" # input video path or webcam index + +# Image processing +scale_pose: 0.42 # shoulder width +crop_image: 1.0 # coefficient of image cropping + +# OSC output +osc_address: "0.0.0.0" # osc ip address +osc_port: 5005 # port +osc_message_address: "/pose/0" # osc address for message in send_message function + +# Output +output_method: 'file' # type os output data method +mirror_image: True # horizontal image mirroring just for show +show_image: True # show image in opencv interface diff --git a/hierarchy_data.json b/hierarchy_data.json index d715af4..a9b34c0 100644 --- a/hierarchy_data.json +++ b/hierarchy_data.json @@ -1,10 +1,10 @@ { "hip_r": { - "visible": 3.283990008640103e-05, + "visible": 3.3100583095801994e-05, "translation": { - "x": 0.23185212236810918, - "y": 0.00371596784027843, - "z": -1.3490804656954876 + "x": 0.19120925371787711, + "y": 0.012880325989142518, + "z": -1.2929239842510376 }, "rotation": { "rotx": 0, @@ -18,11 +18,11 @@ } }, "hip_l": { - "visible": 3.066848512389697e-05, + "visible": 3.3651816920610145e-05, "translation": { - "x": 0.49386722361764196, - "y": -0.004551878615287206, - "z": -1.3717949211023943 + "x": 0.44584404101564734, + "y": -0.01352180735545764, + "z": -1.3006948606338735 }, "rotation": { "rotx": 0, @@ -38,9 +38,9 @@ "root": { "visible": 1.0, "translation": { - "x": 0.32104200438618696, - "y": -2.4274882656989587, - "z": 1.2036544053764062 + "x": 0.2515628279636967, + "y": -2.3222343162082186, + "z": 1.024181330938888 }, "rotation": { "rotx": 0, @@ -54,11 +54,11 @@ } }, "pelvis": { - "visible": 3.1754192605149e-05, + "visible": 3.337620000820607e-05, "translation": { - "x": 0.32104200438618696, - "y": 0.00036978822761340243, - "z": 1.2036544053764062 + "x": 0.2515628279636967, + "y": 0.0002533114072720546, + "z": 1.024181330938888 }, "rotation": { "rotx": 0, @@ -74,9 +74,9 @@ "head": { "visible": 1.0, "translation": { - "x": 0.4018470055921379, - "y": 1.4863796147074047, - "z": -0.49572140460617947 + "x": 0.36108411469132357, + "y": 1.4519316551019659, + "z": -0.5288823311319394 }, "rotation": { "rotx": 0, @@ -90,11 +90,11 @@ } }, "spine_01": { - "visible": 0.9558884007726984, + "visible": 0.955888472324311, "translation": { - "x": 0.3523824967200987, - "y": 1.2570843949458836, - "z": -0.3722668270030201 + "x": 0.2813578177045325, + "y": 1.096117025560525, + "z": -0.36358807558897704 }, "rotation": { "rotx": 0, @@ -108,11 +108,11 @@ } }, "spine_02": { - "visible": 0.8642053121876015, + "visible": 0.8642055324545849, "translation": { - "x": 0.3458276216245312, - "y": 1.136539767039317, - "z": -0.23441402075809645 + "x": 0.2734272868087156, + "y": 0.9909997438024116, + "z": -0.25112939542523893 }, "rotation": { "rotx": 0, @@ -126,11 +126,11 @@ } }, "spine_03": { - "visible": 0.7177959614459576, + "visible": 0.7177964191975329, "translation": { - "x": 0.335360096787125, - "y": 0.9440412269098308, - "z": -0.014275932352623016 + "x": 0.26076296782239117, + "y": 0.8231372381708821, + "z": -0.07154336050721924 }, "rotation": { "rotx": 0, @@ -144,11 +144,11 @@ } }, "spine_04": { - "visible": 0.5484203400028553, + "visible": 0.5484210724916665, "translation": { - "x": 0.323250600260258, - "y": 0.7213467084507186, - "z": 0.24039376830583514 + "x": 0.24611208051501207, + "y": 0.6289432467057616, + "z": 0.13621315308061457 }, "rotation": { "rotx": 0, @@ -162,11 +162,11 @@ } }, "spine_05": { - "visible": 0.2289994832763534, + "visible": 0.22900073388461134, "translation": { - "x": 0.3004136316210739, - "y": 0.30137320064339257, - "z": 0.7206683985833275 + "x": 0.21848237243982405, + "y": 0.2627181040528383, + "z": 0.5280154856437433 }, "rotation": { "rotx": 0, @@ -180,11 +180,11 @@ } }, "knee_r": { - "visible": 0.021311474964022636, + "visible": 0.013898183591663837, "translation": { - "x": 0.224715555879875, - "y": 0.04331640486588048, - "z": -1.848822215632786 + "x": 0.18465588665818877, + "y": 0.09801578488803149, + "z": -1.768014233852209 }, "rotation": { "rotx": 0, @@ -198,11 +198,11 @@ } }, "knee_l": { - "visible": 0.01792996935546398, + "visible": 0.014928212389349937, "translation": { - "x": 0.479653365369021, - "y": 0.04956380758879055, - "z": -1.8599454030851834 + "x": 0.4274460282068141, + "y": 0.05224144739339493, + "z": -1.7756454888478737 }, "rotation": { "rotx": 0, @@ -216,11 +216,11 @@ } }, "ankle_r": { - "visible": 0.004003077745437622, + "visible": 0.0026775619480758905, "translation": { - "x": 0.22040579799855411, - "y": -0.6035946591415875, - "z": -2.2528606986185435 + "x": 0.17896696239800255, + "y": -0.5335542811699052, + "z": -2.1543303245629586 }, "rotation": { "rotx": 0, @@ -234,11 +234,11 @@ } }, "ankle_l": { - "visible": 0.005025549326092005, + "visible": 0.0034075980074703693, "translation": { - "x": 0.46486163125941043, - "y": -0.49356652566551906, - "z": -2.266784775168302 + "x": 0.41408530649657976, + "y": -0.49391145737651565, + "z": -2.169021733182351 }, "rotation": { "rotx": 0, @@ -252,11 +252,11 @@ } }, "ball_r": { - "visible": 0.0033030910417437553, + "visible": 0.0020394118037074804, "translation": { - "x": 0.2533501274660685, - "y": -0.2277205314512301, - "z": -2.4257652906916354 + "x": 0.20595402067926516, + "y": -0.17559397141519834, + "z": -2.3155040569477063 }, "rotation": { "rotx": 0, @@ -270,11 +270,11 @@ } }, "ball_l": { - "visible": 0.007918210700154305, + "visible": 0.004800162278115749, "translation": { - "x": 0.4167605323921076, - "y": -0.07112429439358949, - "z": -2.4274882656989587 + "x": 0.3851209297536031, + "y": -0.11210682978053091, + "z": -2.3222343162082186 }, "rotation": { "rotx": 0, @@ -288,11 +288,11 @@ } }, "heel_r": { - "visible": 0.003357355482876301, + "visible": 0.002590247429907322, "translation": { - "x": 0.214153237082301, - "y": -0.6405563543831717, - "z": -2.3297296844741737 + "x": 0.1730598856046068, + "y": -0.5702056955598608, + "z": -2.2201981026431357 }, "rotation": { "rotx": 0, @@ -306,11 +306,11 @@ } }, "heel_l": { - "visible": 0.009946711361408234, + "visible": 0.008067294023931026, "translation": { - "x": 0.4684025052154504, - "y": -0.5260255564826632, - "z": -2.3472397765876565 + "x": 0.41600418262782407, + "y": -0.5288466490646851, + "z": -2.241292700597866 }, "rotation": { "rotx": 0, @@ -324,11 +324,11 @@ } }, "shoulder_r": { - "visible": 0.9999551773071289, + "visible": 0.9998279809951782, "translation": { - "x": 0.18561357625361946, - "y": 0.7721517346988932, - "z": -0.69730114825041 + "x": 0.13330336641156587, + "y": 0.740182636431514, + "z": -0.6801516537114732 }, "rotation": { "rotx": 0, @@ -342,11 +342,11 @@ } }, "shoulder_l": { - "visible": 0.9999798536300659, + "visible": 0.9999477863311768, "translation": { - "x": 0.60345881243435, - "y": 0.7539580847363371, - "z": -0.7356984098989899 + "x": 0.5445893135230234, + "y": 0.655885117688507, + "z": -0.6918903948025639 }, "rotation": { "rotx": 0, @@ -360,11 +360,11 @@ } }, "clavicle_r": { - "visible": 0.3903829998635262, + "visible": 0.33058090412246055, "translation": { - "x": 0.29915971923785645, - "y": 0.6344740294859538, - "z": 0.30770636512969557 + "x": 0.2238388086062232, + "y": 0.5294572856816074, + "z": 0.24276561849161685 }, "rotation": { "rotx": 0, @@ -378,11 +378,11 @@ } }, "clavicle_l": { - "visible": 0.45094530122838333, + "visible": 0.39446994282734144, "translation": { - "x": 0.3989762273134593, - "y": 0.6323009442726204, - "z": 0.3072717480870289 + "x": 0.31154057340157937, + "y": 0.5204695713996657, + "z": 0.24096807563522846 }, "rotation": { "rotx": 0, @@ -396,11 +396,11 @@ } }, "elbow_r": { - "visible": 0.3276873528957367, + "visible": 0.36677366495132446, "translation": { - "x": 0.09727160822155982, - "y": 0.6303470208087711, - "z": -1.0638687217845344 + "x": 0.06503809352132009, + "y": 0.6750874833775152, + "z": -1.0384893789994394 }, "rotation": { "rotx": 0, @@ -414,11 +414,11 @@ } }, "elbow_l": { - "visible": 0.3048570454120636, + "visible": 0.30559301376342773, "translation": { - "x": 0.6504693670538582, - "y": 0.5472289809491672, - "z": -1.1147813886926048 + "x": 0.5904584697564025, + "y": 0.4368189729206031, + "z": -1.0304522522572501 }, "rotation": { "rotx": 0, diff --git a/hpe_json.py b/hpe_json.py index 0e859a4..a443918 100644 --- a/hpe_json.py +++ b/hpe_json.py @@ -11,7 +11,7 @@ parser.add_argument('--address_input', type=str, default="0", help='input video parser.add_argument('--scale_pose', type=float, default=0.42, help='shoulder width') parser.add_argument('--crop_image', type=float, default=1.0, help='coefficient of image cropping') parser.add_argument('--osc_address', type=str, default="0.0.0.0", help='osc_address') -parser.add_argument('--osc_port', type=int, default="5005", help='port') +parser.add_argument('--osc_port', type=int, default=5005, help='port') parser.add_argument('--osc_message_address', type=str, default="/pose/0", help='osc address for message in send_message function') parser.add_argument('--output_method', type=str, default='file', choices=['file', 'osc'], help='type os output data method') parser.add_argument('--mirror_image', type=bool, default=True, help='horizontal image mirroring just for show') diff --git a/hpe_win.py b/hpe_win.py new file mode 100644 index 0000000..b11f21f --- /dev/null +++ b/hpe_win.py @@ -0,0 +1,87 @@ +import cv2 +import time +from hpe_mp_class import hpe_mp_class +import json +import argparse +from pythonosc import udp_client +import yaml + +# Arguments +parser = argparse.ArgumentParser() +parser.add_argument('--config_path', type=str, default="config.yml", help='path to config file yaml') +args = parser.parse_args() +config_path = args.config_path + +# Config parameters +data_yaml = yaml.safe_load(open(config_path)) + +address_input = data_yaml["address_input"] +for i in range(0, 100): + if address_input == str(i): + address_input = i +scale_pose = data_yaml["scale_pose"] +osc_address = data_yaml["osc_address"] +osc_port = data_yaml["osc_port"] +osc_message_address = data_yaml["osc_message_address"] +output_method = data_yaml["output_method"] +crop = data_yaml["crop_image"] +mirror_image = data_yaml["mirror_image"] +show_image = data_yaml["show_image"] + +# OSC client +client = udp_client.SimpleUDPClient(osc_address, osc_port) + +# Videocapture +cap = cv2.VideoCapture(address_input) + +# Preprocessing parameters +frame_width = int(crop*cap.get(cv2.CAP_PROP_FRAME_WIDTH)) +frame_height = int(crop*cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + +# FPS variables +pTime = 0 +cTime = 0 + +# Mediapie class +mp_cl = hpe_mp_class() + +while True: + # Reading frame + success, img = cap.read() + + # Image preprocessing + if crop != 1.0: + img = cv2.resize(img, (frame_width, frame_height)) + + # Mediapipe + mp_cl.process(img, scale_pose=scale_pose) + mp_cl.show(img) + + # FPS + cTime = time.time() + fps = 1. / (cTime - pTime) + pTime = cTime + + # Showing + if show_image: + if mirror_image: + img = cv2.flip(img, 1) # mirror + cv2.putText(img, str(int(fps)), (22, 32), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2) + cv2.putText(img, str(int(fps)), (20, 30), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2) + cv2.imshow("Main", img) + + # Output + if output_method == 'file': + # JSON + res = mp_cl.getJSON() + with open('hierarchy_data.json', 'w', encoding='utf-8') as f: + json.dump(res, f, ensure_ascii=False, indent=4) + else: + # OSC + res = mp_cl.getJSON() + client.send_message(osc_message_address, res) + + # Interface + key = cv2.waitKey(1) + if key == 27: + break