Browse Source

Added Landmark Export as XML File

Dev
Erik Hildebrandt 3 years ago
parent
commit
34dee8263c
  1. 24
      HPEModule.py

24
HPEModule.py

@ -1,9 +1,11 @@
import os import os
from xml.dom import minidom
import cv2 import cv2
import mediapipe as mp import mediapipe as mp
from DataStreamModule import DataStreamModule from DataStreamModule import DataStreamModule
from IHPEModule import IHPEModule from IHPEModule import IHPEModule
import xml.etree.ElementTree as ET
# initialize mediapipe drawing utilities and pose models # initialize mediapipe drawing utilities and pose models
mp_drawing = mp.solutions.drawing_utils mp_drawing = mp.solutions.drawing_utils
@ -38,8 +40,18 @@ class HPEModule(IHPEModule):
# set the window name using the camera name # set the window name using the camera name
window_name = f"Pose Estimation on Camera {camera_name}" window_name = f"Pose Estimation on Camera {camera_name}"
# create the root element
root = ET.Element("Landmarks")
# loop through each landmark
for i in range(33):
ET.SubElement(root, f'landmark{i}')
framecounter = 0
# start pose detection # start pose detection
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose: with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
while True: while True:
# get the next frame from the camera # get the next frame from the camera
if out is not None: if out is not None:
@ -63,6 +75,12 @@ class HPEModule(IHPEModule):
# Extract landmarks # Extract landmarks
try: try:
landmarks = results.pose_landmarks.landmark landmarks = results.pose_landmarks.landmark
for j, landmark in enumerate(landmarks):
frame_element = ET.SubElement(root.find(f'landmark{j}'), f'frame{framecounter}')
frame_element.set("X", str(landmark.x))
frame_element.set("Y", str(landmark.y))
frame_element.set("Z", str(landmark.z))
framecounter += 1
# print(landmarks) # print(landmarks)
except: except:
pass pass
@ -81,3 +99,9 @@ class HPEModule(IHPEModule):
keyCode = cv2.waitKey(1) keyCode = cv2.waitKey(1)
if cv2.getWindowProperty(window_name, cv2.WND_PROP_VISIBLE) < 1: if cv2.getWindowProperty(window_name, cv2.WND_PROP_VISIBLE) < 1:
break break
xml_string = ET.tostring(root, encoding="UTF-8")
parsed_xml = minidom.parseString(xml_string)
pretty_xml_string = parsed_xml.toprettyxml(indent=" ")
with open(f"{camera_name}_output.xml", "w") as xml_file:
xml_file.write(pretty_xml_string)

Loading…
Cancel
Save