Files
HPE_mediapipe/hpe_win.py
2021-11-29 22:51:18 +07:00

88 lines
2.2 KiB
Python

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