Advertisement
alseambusher

Untitled

Sep 22nd, 2024
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. import cv2
  2. from comms import Message
  3.  
  4. class TrackData:
  5.     @classmethod
  6.     def from_handLms(cls, handLms, img) -> None:
  7.         self = cls()
  8.         self.c_x = self.c_y = 0  # hand center
  9.         self.palm_height = 0
  10.        
  11.         avg_x = avg_y = avg_f_x = avg_f_y = count = 0
  12.         for idx, lm in enumerate(handLms.landmark):
  13.             h, w, _ = img.shape
  14.             x, y = int(lm.x *w), int(lm.y*h)
  15.             avg_x += x
  16.             avg_y += y
  17.             count += 1
  18.  
  19.             if idx == 0: # center
  20.                 self.c_x, self.c_y = x, y
  21.             if idx == 5:
  22.                 self.palm_height = self.c_y - y
  23.             if idx > 4 and idx % 4 == 0:  # tip of 4 fingers
  24.                 avg_f_x += x
  25.                 avg_f_y += y
  26.            
  27.             cv2.circle(img, (x,y), 3, (255,0,255), cv2.FILLED)
  28.                
  29.  
  30.         self.avg_x = avg_x // count
  31.         self.avg_y = avg_y // count
  32.         self.avg_f_x = avg_f_x // 4
  33.         self.avg_f_y = avg_f_y // 4
  34.  
  35.         return self
  36.  
  37. class TrackingAlgo:
  38.     @staticmethod
  39.     def hand_finger_control(td0: TrackData, td: TrackData):
  40.         if td.palm_height > 0 :
  41.             dist_x = (td0.c_x - td.c_x) / td.palm_height # -3 - +3
  42.             scaled_dist_x = round(((dist_x + 3) / (3 + 3)) * 180)
  43.             print(f"Sides {dist_x} {scaled_dist_x}")
  44.             Message.send(Message.pnt(Message.PAN, 0, scaled_dist_x))
  45.             Message.send(Message.pnt(Message.PAN, 1, scaled_dist_x))
  46.        
  47.             dist_y = (td.c_y - td.avg_f_y) / td.palm_height
  48.  
  49.             # TODO add part of this to the degrees
  50.             scaled_dist_x = round(((dist_x + 3) / (3 + 3)) * 180)
  51.             print(f"dist_y {dist_y}")
  52.  
  53.             inverse_dist_y = 1 / dist_y # 0.5 - 1.5
  54.             scaled_dist_y = round(((inverse_dist_y - 0.5) / (1.5 - 0.5)) * 180)
  55.  
  56.             if 0 < scaled_dist_y < 180:
  57.                 print(f"Fingers {scaled_dist_y} {td.avg_f_y - td0.avg_f_y}")
  58.                 Message.send(Message.pnt(Message.TILT, 0, scaled_dist_y))
  59.                 Message.send(Message.pnt(Message.TILT, 1, scaled_dist_y))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement