Spaces:
Sleeping
Sleeping
File size: 20,785 Bytes
95db528 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
from scipy.spatial import distance
from collections import deque
from .enums import Event, HandPosition, targets
from .hand import Hand
class Deque:
def __init__(self, maxlen=30, min_frames=20):
self.maxlen = maxlen
self._deque = []
self.action = None
self.min_absolute_distance = 1.5
self.min_frames = min_frames
self.action_deque = deque(maxlen=5)
def __len__(self):
return len(self._deque)
def index_position(self, x):
for i in range(len(self._deque)):
if self._deque[i].position == x:
return i
def index_gesture(self, x):
for i in range(len(self._deque)):
if self._deque[i].gesture == x:
return i
def __getitem__(self, index):
return self._deque[index]
def __setitem__(self, index, value):
self._deque[index] = value
def __delitem__(self, index):
del self._deque[index]
def __iter__(self):
return iter(self._deque)
def __reversed__(self):
return reversed(self._deque)
def append(self, x):
if self.maxlen is not None and len(self) >= self.maxlen:
self._deque.pop(0)
self.set_hand_position(x)
self._deque.append(x)
self.check_is_action(x)
def check_duration(self, start_index, min_frames=None):
"""
Check duration of swipe.
Parameters
----------
start_index : int
Index of start position of swipe.
Returns
-------
bool
True if duration of swipe is more than min_frames.
"""
if min_frames == None:
min_frames = self.min_frames
if len(self) - start_index >= min_frames:
return True
else:
return False
def check_duration_max(self, start_index, max_frames=10):
"""
Check duration of swipe.
Parameters
----------
start_index : int
Index of start position of swipe.
Returns
-------
bool
True if duration of swipe is more than min_frames.
"""
if len(self) - start_index <= max_frames:
return True
else:
return False
def check_is_action(self, x):
"""
Check if gesture is action.
Parameters
----------
x : Hand
Hand object.
Returns
-------
bool
True if gesture is action.
"""
if x.position == HandPosition.LEFT_END and HandPosition.RIGHT_START in self:
start_index = self.index_position(HandPosition.RIGHT_START)
if (
self.swipe_distance(self._deque[start_index], x)
and self.check_duration(start_index)
and self.check_horizontal_swipe(self._deque[start_index], x)
):
self.action = Event.SWIPE_LEFT
self.clear()
return True
elif x.position == HandPosition.RIGHT_END and HandPosition.LEFT_START in self:
start_index = self.index_position(HandPosition.LEFT_START)
if (
self.swipe_distance(self._deque[start_index], x)
and self.check_duration(start_index)
and self.check_horizontal_swipe(self._deque[start_index], x)
):
self.action = Event.SWIPE_RIGHT
self.clear()
return True
else:
self.clear()
elif x.position == HandPosition.UP_END and HandPosition.DOWN_START in self:
start_index = self.index_position(HandPosition.DOWN_START)
if (
self.swipe_distance(self._deque[start_index], x)
and self.check_duration(start_index)
and self.check_vertical_swipe(self._deque[start_index], x)
):
self.action = Event.SWIPE_UP
self.clear()
return True
else:
self.clear()
elif x.position == HandPosition.DOWN_END and HandPosition.UP_START in self:
start_index = self.index_position(HandPosition.UP_START)
if (
self.swipe_distance(self._deque[start_index], x)
and self.check_duration(start_index)
and self.check_vertical_swipe(self._deque[start_index], x)
):
self.action = Event.SWIPE_DOWN
self.clear()
return True
else:
self.clear()
elif x.gesture == 18: # grip
if self.action is None:
start_index = self.index_gesture(18)
if self.check_duration(start_index):
self.action = Event.DRAG2
return True
elif self.action == Event.DRAG2 and x.gesture in [11, 12]: # hand heart
self.action = Event.DROP2
self.clear()
return True
elif x.gesture == 29: # ok
if self.action is None:
start_index = self.index_gesture(29)
if self.check_duration(start_index):
self.action = Event.DRAG3
return True
elif self.action == Event.DRAG3 and x.gesture in [11, 12]: # hand heart
self.action = Event.DROP3
self.clear()
return True
elif x.position == HandPosition.FAST_SWIPE_UP_END and HandPosition.FAST_SWIPE_UP_START in self:
start_index = self.index_position(HandPosition.FAST_SWIPE_UP_START)
if (
self.check_duration(start_index, min_frames=20)
and self.check_vertical_swipe(self._deque[start_index], x)
):
self.action = Event.FAST_SWIPE_UP
self.clear()
return True
else:
self.clear()
elif x.position == HandPosition.FAST_SWIPE_DOWN_END and HandPosition.FAST_SWIPE_DOWN_START in self:
start_index = self.index_position(HandPosition.FAST_SWIPE_DOWN_START)
if (
self.check_duration(start_index, min_frames=20)
and self.check_vertical_swipe(self._deque[start_index], x)
):
self.action = Event.FAST_SWIPE_DOWN
self.clear()
return True
elif x.position == HandPosition.ZOOM_IN_END and HandPosition.ZOOM_IN_START in self:
start_index = self.index_position(HandPosition.ZOOM_IN_START)
if (
self.check_duration(start_index, min_frames=20)
and self.check_vertical_swipe(self._deque[start_index], x)
and self.check_horizontal_swipe(self._deque[start_index], x)
):
self.action = Event.ZOOM_IN
self.clear()
return True
elif x.position == HandPosition.ZOOM_OUT_END and HandPosition.ZOOM_OUT_START in self:
start_index = self.index_position(HandPosition.ZOOM_OUT_START)
if (
self.check_duration(start_index, min_frames=20)
and self.check_vertical_swipe(self._deque[start_index], x)
and self.check_horizontal_swipe(self._deque[start_index], x)
):
self.action = Event.ZOOM_OUT
self.clear()
return True
else:
self.clear()
elif x.position == HandPosition.LEFT_END2 and HandPosition.RIGHT_START2 in self:
start_index = self.index_position(HandPosition.RIGHT_START2)
if (
self.swipe_distance(self._deque[start_index], x)
and self.check_duration(start_index)
and self.check_horizontal_swipe(self._deque[start_index], x)
):
self.action = Event.SWIPE_LEFT2
self.clear()
return True
else:
self.clear()
elif x.position == HandPosition.RIGHT_END2 and HandPosition.LEFT_START2 in self:
start_index = self.index_position(HandPosition.LEFT_START2)
if (
self.swipe_distance(self._deque[start_index], x)
and self.check_duration(start_index)
and self.check_horizontal_swipe(self._deque[start_index], x)
):
self.action = Event.SWIPE_RIGHT2
self.clear()
return True
else:
self.clear()
elif x.position == HandPosition.UP_END2 and HandPosition.DOWN_START2 in self:
start_index = self.index_position(HandPosition.DOWN_START2)
if (
self.swipe_distance(self._deque[start_index], x)
and self.check_duration(start_index)
and self.check_vertical_swipe(self._deque[start_index], x)
):
self.action = Event.SWIPE_UP2
self.clear()
return True
else:
self.clear()
elif x.position == HandPosition.LEFT_END3 and HandPosition.RIGHT_START3 in self:
start_index = self.index_position(HandPosition.RIGHT_START3)
if (
self.swipe_distance(self._deque[start_index], x)
and self.check_duration(start_index)
and self.check_horizontal_swipe(self._deque[start_index], x)
):
self.action = Event.SWIPE_LEFT3 # two
self.clear()
return True
else:
self.clear()
elif x.position == HandPosition.RIGHT_END3 and HandPosition.LEFT_START3 in self:
start_index = self.index_position(HandPosition.LEFT_START3)
if (
self.swipe_distance(self._deque[start_index], x)
and self.check_duration(start_index)
and self.check_horizontal_swipe(self._deque[start_index], x)
):
self.action = Event.SWIPE_RIGHT3
self.clear()
return True
else:
self.clear()
elif x.position == HandPosition.UP_END3 and HandPosition.DOWN_START3 in self:
start_index = self.index_position(HandPosition.DOWN_START3)
if (
self.check_duration(start_index, min_frames=15)
and self.check_vertical_swipe(self._deque[start_index], x)
):
self.action = Event.SWIPE_UP3
self.clear()
return True
else:
self.clear()
elif x.position == HandPosition.DOWN_END3 and HandPosition.UP_START3 in self:
start_index = self.index_position(HandPosition.UP_START3)
if (
self.check_duration(start_index, min_frames=15)
and self.check_vertical_swipe(self._deque[start_index], x)
):
self.action = Event.SWIPE_DOWN3
self.clear()
return True
else:
self.clear()
elif HandPosition.DRAG_START in self and x.gesture == 25: # fist
if self.action is None:
start_index = self.index_gesture(17) # grabbing
if self.check_duration(start_index, min_frames=3):
self.action = Event.DRAG
return True
else:
self.clear()
elif HandPosition.ZOOM_IN_START in self and x.gesture == 19: # point
start_index = self.index_position(HandPosition.ZOOM_IN_START)
if (
self.check_duration(start_index, min_frames=8)
and self.check_vertical_swipe(self._deque[start_index], x)
and self.check_horizontal_swipe(self._deque[start_index], x)
):
self.action = Event.TAP
self.clear()
return True
elif (
self.check_duration(start_index, min_frames=2)
and self.check_duration_max(start_index, max_frames=8)
and self.check_vertical_swipe(self._deque[start_index], x)
and self.check_horizontal_swipe(self._deque[start_index], x)
):
self.action_deque.append(Event.TAP)
if len(self.action_deque) >= 2 and self.action_deque[-1] == Event.TAP and self.action_deque[-2] == Event.TAP:
self.action_deque.pop()
self.action_deque.pop()
self.action = Event.DOUBLE_TAP
self.clear()
return True
else:
self.clear()
elif x.position == HandPosition.DOWN_END2 and HandPosition.ZOOM_OUT_START in self:
start_index = self.index_position(HandPosition.ZOOM_OUT_START)
if (
self.swipe_distance(self._deque[start_index], x)
and self.check_vertical_swipe(self._deque[start_index], x)
):
self.action = Event.SWIPE_DOWN2
self.clear()
return True
else:
self.clear()
elif x.position == HandPosition.ZOOM_OUT_START and HandPosition.UP_START2 in self:
start_index = self.index_position(HandPosition.UP_START2)
if (
self.swipe_distance(self._deque[start_index], x)
and self.check_vertical_swipe(self._deque[start_index], x)
):
self.action = Event.SWIPE_UP2
self.clear()
return True
else:
self.clear()
elif self.action == Event.DRAG and x.gesture in [35, 31, 36, 17]: # [stop, palm, stop_inverted, grabbing]
self.action = Event.DROP
self.clear()
return True
return False
@staticmethod
def check_horizontal_swipe(start_hand, x):
"""
Check if swipe is horizontal.
Parameters
----------
start_hand : Hand
Hand object of start position of swipe.
x : Hand
Hand object of end position of swipe.
Returns
-------
bool
True if swipe is horizontal.
"""
boundary = [start_hand.bbox[1], start_hand.bbox[3]]
if boundary[0] < x.center[1] < boundary[1]:
return True
else:
return False
@staticmethod
def check_vertical_swipe(start_hand, x):
"""
Check if swipe is vertical.
Parameters
----------
start_hand : Hand
Hand object of start position of swipe.
x : Hand
Hand object of end position of swipe.
Returns
-------
bool
True if swipe is vertical.
"""
boundary = [start_hand.bbox[0], start_hand.bbox[2]]
if boundary[0] < x.center[0] < boundary[1]:
return True
else:
return False
def __contains__(self, item):
for x in self._deque:
if x.position == item:
return True
def set_hand_position(self, hand: Hand):
"""
Set hand position.
Parameters
----------
hand : Hand
Hand object.
"""
if hand.gesture in [31, 35, 36]: # [palm, stop, stop_inv]
if HandPosition.DOWN_START in self:
hand.position = HandPosition.UP_END
else:
hand.position = HandPosition.UP_START
elif hand.gesture == 0: # hand_down
if HandPosition.UP_START in self:
hand.position = HandPosition.DOWN_END
else:
hand.position = HandPosition.DOWN_START
elif hand.gesture == 1: # hand_right
if HandPosition.LEFT_START in self:
hand.position = HandPosition.RIGHT_END
else:
hand.position = HandPosition.RIGHT_START
elif hand.gesture == 2: # hand_left
if HandPosition.RIGHT_START in self:
hand.position = HandPosition.LEFT_END
else:
hand.position = HandPosition.LEFT_START
elif hand.gesture == 30: # one
if HandPosition.FAST_SWIPE_UP_START in self:
hand.position = HandPosition.FAST_SWIPE_UP_END
else:
hand.position = HandPosition.FAST_SWIPE_DOWN_START
elif hand.gesture == 19: # point
if HandPosition.FAST_SWIPE_DOWN_START in self:
hand.position = HandPosition.FAST_SWIPE_DOWN_END
else:
hand.position = HandPosition.FAST_SWIPE_UP_START
elif hand.gesture == 17: # grabbing
hand.position = HandPosition.DRAG_START
elif hand.gesture == 25: # fist
if HandPosition.ZOOM_OUT_START in self:
hand.position = HandPosition.ZOOM_OUT_END
else:
hand.position = HandPosition.ZOOM_IN_START
elif hand.gesture == 3: # thumb_index
if HandPosition.ZOOM_IN_START in self:
hand.position = HandPosition.ZOOM_IN_END
else:
hand.position = HandPosition.ZOOM_OUT_START
elif hand.gesture == 38: # three2
if HandPosition.ZOOM_IN_START in self:
hand.position = HandPosition.ZOOM_IN_END
else:
hand.position = HandPosition.ZOOM_OUT_START
elif hand.gesture == 5: # thumb_right
if HandPosition.LEFT_START2 in self:
hand.position = HandPosition.RIGHT_END2
else:
hand.position = HandPosition.RIGHT_START2
elif hand.gesture == 4: # thumb_left
if HandPosition.RIGHT_START2 in self:
hand.position = HandPosition.LEFT_END2
else:
hand.position = HandPosition.LEFT_START2
elif hand.gesture == 15: # two_right
if HandPosition.LEFT_START3 in self:
hand.position = HandPosition.RIGHT_END3
else:
hand.position = HandPosition.RIGHT_START3
elif hand.gesture == 14: # two_left
if HandPosition.RIGHT_START3 in self:
hand.position = HandPosition.LEFT_END3
else:
hand.position = HandPosition.LEFT_START3
elif hand.gesture == 39: # two_up
if HandPosition.DOWN_START3 in self:
hand.position = HandPosition.UP_END3
else:
hand.position = HandPosition.UP_START3
elif hand.gesture == 16: # two_down
if HandPosition.UP_START3 in self:
hand.position = HandPosition.DOWN_END3
else:
hand.position = HandPosition.DOWN_START3
elif hand.gesture == 6: # thumb_down
if HandPosition.ZOOM_OUT_START in self:
hand.position = HandPosition.DOWN_END2
else:
hand.position = HandPosition.UP_START2
else:
hand.position = HandPosition.UNKNOWN
def swipe_distance(
self,
first_hand: Hand,
last_hand: Hand,
):
"""
Check if swipe distance is more than min_distance.
Parameters
----------
first_hand : Hand
Hand object of start position of swipe.
last_hand : Hand
Hand object of end position of swipe.
Returns
-------
bool
True if swipe distance is more than min_distance.
"""
hand_dist = distance.euclidean(first_hand.center, last_hand.center)
hand_size = (first_hand.size + last_hand.size) / 2
return hand_dist / hand_size > self.min_absolute_distance
def clear(self):
self._deque.clear()
def copy(self):
return self._deque.copy()
def count(self, x):
return self._deque.count(x)
def extend(self, iterable):
self._deque.extend(iterable)
def insert(self, i, x):
self._deque.insert(i, x)
def pop(self):
return self._deque.pop()
def remove(self, value):
self._deque.remove(value)
def reverse(self):
self._deque.reverse()
def __str__(self):
return f"Deque({[hand.gesture for hand in self._deque]})"
|