Upload core/sensel_lib/frame_data.py with huggingface_hub
Browse files- core/sensel_lib/frame_data.py +112 -0
core/sensel_lib/frame_data.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
class ContactData():
|
| 4 |
+
|
| 5 |
+
''' Contains all info in one keystroke
|
| 6 |
+
|
| 7 |
+
Attributes:
|
| 8 |
+
id: a unique integer to number a keystroke with upper limit 16
|
| 9 |
+
state: 1 => the start of a keystroke, 2 => middle, 3 => the end
|
| 10 |
+
number of states that equal 1 or 3 can only be 1
|
| 11 |
+
label: -1 => unsure, 0 => negative, 1 => positive
|
| 12 |
+
'''
|
| 13 |
+
|
| 14 |
+
def __init__(self, id = 0, state = 0, x = 0, y = 0, area = 0, force = 0, major = 0, minor = 0,
|
| 15 |
+
delta_x = 0, delta_y = 0, delta_force = 0, delta_area = 0, label = 0, frame_id = 0):
|
| 16 |
+
self.id: int = id
|
| 17 |
+
self.state: int = state
|
| 18 |
+
self.x: float = x
|
| 19 |
+
self.y: float = y
|
| 20 |
+
self.area: float = area
|
| 21 |
+
self.force: float = force
|
| 22 |
+
self.major: float = major
|
| 23 |
+
self.minor: float = minor
|
| 24 |
+
self.delta_x: float = delta_x
|
| 25 |
+
self.delta_y: float = delta_y
|
| 26 |
+
self.delta_force: float = delta_force
|
| 27 |
+
self.delta_area: float = delta_area
|
| 28 |
+
self.label: int = label
|
| 29 |
+
self.frame_id: int = frame_id
|
| 30 |
+
|
| 31 |
+
def __str__(self):
|
| 32 |
+
contact_info = 'id: ' + str(self.id) + '\n' + 'state: ' + str(self.state) + '\n' \
|
| 33 |
+
+ 'x: ' + str(self.x) + '\n' + 'y: ' + str(self.y) + '\n' \
|
| 34 |
+
+'area: ' + str(self.area) + '\n' + 'force: ' + str(self.force) + '\n' \
|
| 35 |
+
+ 'major: ' + str(self.major) + '\n' + 'minor: ' + str(self.minor) + '\n' \
|
| 36 |
+
+ 'delta_x: ' + str(self.delta_x) + '\n' + 'delta_y: ' + str(self.delta_y) + '\n' \
|
| 37 |
+
+ 'delta_force: ' + str(self.delta_force) + '\n' + 'delta_area: ' + str(self.delta_area) + '\n' \
|
| 38 |
+
+ 'label: ' + str(self.label) + '\n' + 'frame_id: ' + str(self.frame_id)
|
| 39 |
+
return contact_info
|
| 40 |
+
|
| 41 |
+
def to_numpy(self):
|
| 42 |
+
return np.array([self.id, self.state, self.x, self.y, self.area, self.force, self.major, self.minor,
|
| 43 |
+
self.delta_x, self.delta_y, self.delta_force, self.delta_area, self.label, self.frame_id], dtype=np.float64)
|
| 44 |
+
|
| 45 |
+
def type_str(self):
|
| 46 |
+
type_info = 'id: ' + str(type(self.id)) + '\n' + 'state: ' + str(type(self.state)) + '\n' \
|
| 47 |
+
+ 'x: ' + str(type(self.x)) + '\n' + 'y: ' + str(type(self.y)) + '\n' \
|
| 48 |
+
+'area: ' + str(type(self.area)) + '\n' + 'force: ' + str(type(self.force)) + '\n' \
|
| 49 |
+
+ 'major: ' + str(type(self.major)) + '\n' + 'minor: ' + str(type(self.minor)) + '\n' \
|
| 50 |
+
+ 'delta_x: ' + str(type(self.delta_x)) + '\n' + 'delta_y: ' + str(type(self.delta_y)) + '\n' \
|
| 51 |
+
+ 'delta_force: ' + str(type(self.delta_force)) + '\n' + 'delta_area: ' + str(type(self.delta_area)) + '\n' \
|
| 52 |
+
+ 'label: ' + str(type(self.label)) + '\n' + 'frame_id: ' + str(type(self.frame_id))
|
| 53 |
+
return type_info
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class FrameData():
|
| 57 |
+
''' info in one frame of morph board
|
| 58 |
+
attrs:
|
| 59 |
+
force_array: TODO
|
| 60 |
+
contacts: list of keystroke contacts on the board
|
| 61 |
+
timestamp: time acquired from time.perf_counter for each frame
|
| 62 |
+
'''
|
| 63 |
+
|
| 64 |
+
# MAGNI = 4
|
| 65 |
+
# layout = utils.Layout()
|
| 66 |
+
|
| 67 |
+
def __init__(self, force_array, timestamp):
|
| 68 |
+
self.force_array = force_array
|
| 69 |
+
self.timestamp = timestamp
|
| 70 |
+
self.contacts = []
|
| 71 |
+
|
| 72 |
+
def append_contact(self, contact):
|
| 73 |
+
self.contacts.append(contact)
|
| 74 |
+
|
| 75 |
+
def to_numpy(self):
|
| 76 |
+
''' convert frame data to numpy array
|
| 77 |
+
return:
|
| 78 |
+
return a numpy array of force_array
|
| 79 |
+
'''
|
| 80 |
+
return self.force_array
|
| 81 |
+
|
| 82 |
+
# def output(self) -> np.array:
|
| 83 |
+
# ''' add contacts to force_map and show image
|
| 84 |
+
# return:
|
| 85 |
+
# return force_map image with marked contacts
|
| 86 |
+
# '''
|
| 87 |
+
# MAGNI = FrameData.MAGNI
|
| 88 |
+
# layout = FrameData.layout
|
| 89 |
+
# H, W = self.force_array.shape
|
| 90 |
+
# force_map = cv2.resize(self.force_array, (W * MAGNI, H * MAGNI)).astype(float)
|
| 91 |
+
# force_map = cv2.cvtColor(force_map, cv2.COLOR_GRAY2BGR)
|
| 92 |
+
|
| 93 |
+
# color = (150, 150, 150)
|
| 94 |
+
# for key, rec in layout.key_pos.items():
|
| 95 |
+
# pt1, pt2 = rec
|
| 96 |
+
# cv2.rectangle(force_map, (int(pt1[0] * W * MAGNI), int(pt1[1] * H * MAGNI)),
|
| 97 |
+
# (int(pt2[0] * W * MAGNI), int(pt2[1] * H * MAGNI)), color, 1)
|
| 98 |
+
# # use len(key) // 2 to modify key offset
|
| 99 |
+
# cv2.putText(force_map, key, org=(int((pt1[0] + 0.03 - (len(key) // 2) * 0.01) * W * MAGNI),
|
| 100 |
+
# int((pt1[1] + 0.05) * H * MAGNI)), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
|
| 101 |
+
# fontScale=0.15*MAGNI, color=color, thickness=1)
|
| 102 |
+
|
| 103 |
+
# for contact in self.contacts:
|
| 104 |
+
# color = (0, 0, 0)
|
| 105 |
+
# if contact.label == -1: # Unsure
|
| 106 |
+
# color = (255, 0, 0)
|
| 107 |
+
# if contact.label == 1: # Positive sample
|
| 108 |
+
# color = (0, 255, 0)
|
| 109 |
+
# if contact.label == 0: # Negative sample
|
| 110 |
+
# color = (0, 0, 255)
|
| 111 |
+
# cv2.circle(force_map, (int((contact.x * W + 0.5) * MAGNI), int((contact.y * H + 0.5) * MAGNI)), 8, color, -1)
|
| 112 |
+
# return force_map
|