|
a |
|
b/app/LeapRecord.py |
|
|
1 |
import sys |
|
|
2 |
import os |
|
|
3 |
from collections import deque |
|
|
4 |
from threading import Thread |
|
|
5 |
|
|
|
6 |
from config.Configuration import env |
|
|
7 |
from resources.LeapSDK.v53_python39 import Leap |
|
|
8 |
from LeapData import LeapData |
|
|
9 |
from resources.pymo.pymo.writers import BVHWriter as Pymo_BVHWriter |
|
|
10 |
# from resources.b3d.bvh_reader import BVH as B3D_BVHReader |
|
|
11 |
# from resources.b3d.c3d_convertor import Convertor as B3D_C3DWriter |
|
|
12 |
from AnyWriter import AnyWriter |
|
|
13 |
from BVHAnimation import bvh_animation |
|
|
14 |
|
|
|
15 |
# from resources.virb import Virb |
|
|
16 |
|
|
|
17 |
_LEAP_QUEUE = deque() |
|
|
18 |
|
|
|
19 |
|
|
|
20 |
class LeapRecord(Leap.Listener): |
|
|
21 |
def __init__(self): |
|
|
22 |
super(LeapRecord, self).__init__() |
|
|
23 |
# Initialize Leap2DataFrame parser |
|
|
24 |
self.fps = int(env.config.frames_per_second) |
|
|
25 |
basis_setting = True if env.args('anybody_basis') else False |
|
|
26 |
self.leap2bvh = LeapData(channel_setting=env.config.channels, |
|
|
27 |
frame_rate=1 / self.fps, |
|
|
28 |
anybody_basis=basis_setting) |
|
|
29 |
|
|
|
30 |
self.bvh_write = env.config.bvh |
|
|
31 |
if self.bvh_write: |
|
|
32 |
self.bvh_filename = os.path.normpath( |
|
|
33 |
os.path.join(os.path.split(env.config.bvh_path)[0], |
|
|
34 |
os.path.split(env.config.bvh_path)[1].replace(".bvh", "") + '.bvh')) |
|
|
35 |
|
|
|
36 |
# self.c3d_write = env.config.c3d |
|
|
37 |
# if self.c3d_write: |
|
|
38 |
# self.c3d_filename = env.config.c3d_path + '\\' + env.config.c3d_filename + '.c3d' |
|
|
39 |
|
|
|
40 |
self.anybody_write = env.config.anybody |
|
|
41 |
if self.anybody_write: |
|
|
42 |
self.anybody_template_path = env.config.anybody_template_path + '\\' |
|
|
43 |
self.anybody_output_path = env.config.anybody_output_path + '\\' |
|
|
44 |
|
|
|
45 |
self.processing = True |
|
|
46 |
self.t = None |
|
|
47 |
self.last_time = 0 |
|
|
48 |
|
|
|
49 |
# self.garmin = Virb(host=('192.168.137.34', 80)) |
|
|
50 |
|
|
|
51 |
def on_init(self, controller): |
|
|
52 |
self.t = Thread(target=self.process_frame, args=(self,)) |
|
|
53 |
# self.garmin.start_recording() |
|
|
54 |
self.t.start() |
|
|
55 |
print("Initialized") |
|
|
56 |
|
|
|
57 |
def on_connect(self, controller): |
|
|
58 |
print("Connected") |
|
|
59 |
print("=====================") |
|
|
60 |
|
|
|
61 |
def on_disconnect(self, controller): |
|
|
62 |
# Note: not dispatched when running in a debugger. |
|
|
63 |
print("Disconnected") |
|
|
64 |
|
|
|
65 |
def on_exit(self, controller): |
|
|
66 |
print("=====================") |
|
|
67 |
print("Exited") |
|
|
68 |
# self.garmin.stop_recording() |
|
|
69 |
|
|
|
70 |
def on_frame(self, controller): |
|
|
71 |
# Get the most recent frame |
|
|
72 |
frame = controller.frame() |
|
|
73 |
_LEAP_QUEUE.append(frame) |
|
|
74 |
# self.leap2bvh.add_frame(controller.frame()) |
|
|
75 |
|
|
|
76 |
@staticmethod |
|
|
77 |
def process_frame(listener): |
|
|
78 |
while listener.processing: |
|
|
79 |
try: |
|
|
80 |
while True: |
|
|
81 |
frame = _LEAP_QUEUE.popleft() |
|
|
82 |
if frame.timestamp - listener.last_time > int(1000000 / listener.fps): |
|
|
83 |
added_frame = listener.leap2bvh.add_frame(frame) |
|
|
84 |
if added_frame: |
|
|
85 |
# print(added_frame.timestamp - listener.last_time) |
|
|
86 |
listener.last_time = added_frame.timestamp |
|
|
87 |
except IndexError: |
|
|
88 |
pass |
|
|
89 |
|
|
|
90 |
def exit(self): |
|
|
91 |
self.processing = False |
|
|
92 |
self.t.join() |
|
|
93 |
self.exit_actions() |
|
|
94 |
|
|
|
95 |
def exit_actions(self): |
|
|
96 |
bvh_data = self.leap2bvh.parse() |
|
|
97 |
|
|
|
98 |
if self.bvh_write: |
|
|
99 |
bvh_writer = Pymo_BVHWriter() |
|
|
100 |
bvh_file = open(self.bvh_filename, 'w') |
|
|
101 |
bvh_writer.write(bvh_data, bvh_file) |
|
|
102 |
bvh_file.close() |
|
|
103 |
print('"{}" written'.format(bvh_file.name)) |
|
|
104 |
|
|
|
105 |
# if self.c3d_write: |
|
|
106 |
# # workaround, need bvh |
|
|
107 |
# bvh_writer = Pymo_BVHWriter() |
|
|
108 |
# bvh_file = open(self.c3d_filename.strip('.c3d') + '-tmp.bvh', 'w') |
|
|
109 |
# bvh_writer.write(bvh_data, bvh_file) |
|
|
110 |
# bvh_file.close() |
|
|
111 |
# print('"{}" written'.format(bvh_file.name)) |
|
|
112 |
# |
|
|
113 |
# bvh_reader = B3D_BVHReader() |
|
|
114 |
# if not bvh_reader.load_from_file(bvh_file.name): |
|
|
115 |
# raise Exception('error: can not read "{}"'.format(bvh_file.name)) |
|
|
116 |
# |
|
|
117 |
# c3d_writer = B3D_C3DWriter() |
|
|
118 |
# c3d_writer.convert(bvh_reader, self.c3d_filename) |
|
|
119 |
# print('"{}" written from "{}"'.format(self.c3d_filename, bvh_file.name)) |
|
|
120 |
# |
|
|
121 |
# os.remove(bvh_file.name) |
|
|
122 |
# print('"{}" deleted'.format(bvh_file.name)) |
|
|
123 |
|
|
|
124 |
if self.anybody_write: |
|
|
125 |
AnyWriter(template_directory=self.anybody_template_path, |
|
|
126 |
output_directory=self.anybody_output_path |
|
|
127 |
).write(bvh_data) |
|
|
128 |
print('Anybody files written to "{}"'.format(self.anybody_output_path)) |
|
|
129 |
|
|
|
130 |
bvh_animation.bvh_data = bvh_data |
|
|
131 |
|
|
|
132 |
|
|
|
133 |
def start_recording(): |
|
|
134 |
# Create a listener and controller |
|
|
135 |
listener = LeapRecord() |
|
|
136 |
controller = Leap.Controller() |
|
|
137 |
|
|
|
138 |
# Have the listener receive events from the controller |
|
|
139 |
controller.add_listener(listener) |
|
|
140 |
|
|
|
141 |
# Keep this process running until Enter is pressed |
|
|
142 |
print("Listener added") |
|
|
143 |
try: |
|
|
144 |
sys.stdin.readline() |
|
|
145 |
except KeyboardInterrupt: |
|
|
146 |
pass |
|
|
147 |
finally: |
|
|
148 |
# Remove the listener when done |
|
|
149 |
controller.remove_listener(listener) |
|
|
150 |
print("Listener removed") |
|
|
151 |
listener.exit() |