Arguments parser in hpe_json.py

This commit is contained in:
Arkadiy Strelnikov
2021-11-29 14:28:49 +07:00
parent 5459e0f0ce
commit bee2dfa944
3 changed files with 540 additions and 20 deletions

89
hpe_json.py Normal file
View File

@@ -0,0 +1,89 @@
import cv2
import time
from hpe_mp_class import hpe_mp_class
import json
import argparse
from pythonosc import udp_client
# Arguments
parser = argparse.ArgumentParser()
parser.add_argument('--address_input', type=str, default="0", help='input video path or webcam index')
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_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')
parser.add_argument('--show_image', type=bool, default=False, help='show image in opencv interface')
args = parser.parse_args()
address_input = args.address_input
for i in range(0, 100):
if address_input == str(i):
address_input = i
scale_pose = args.scale_pose
osc_address = args.osc_address
osc_port = args.osc_port
osc_message_address = args.osc_message_address
output_method = args.output_method
crop = args.crop_image
mirror_image = args.mirror_image
show_image = args.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