Files
JustTwirk/utils.py
2025-11-28 08:31:35 +00:00

64 lines
1.7 KiB
Python

import math
import cv2
import numpy as np
def recvall(sock, n):
data = b''
while len(data) < n:
packet = sock.recv(n - len(data))
if not packet:
return None
data += packet
return data
def distance(p1, p2):
return math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)
import numpy as np
def normalize(move):
left_hip = move[11] # Left Hip
right_hip = move[12] # Right Hip
nose = move[0] # Nose (głowa)
# Środek bioder
center = (left_hip + right_hip) / 2
# Przesunięcie względem środka
normalized_keypoints = move - center
# Zamiast max_dist używamy stałej miary "rozmiaru ciała"
body_height = np.linalg.norm(nose[:2] - center[:2]) # np. odległość biodra-głowa
if body_height > 0:
normalized_keypoints[:, :2] /= body_height
draw = normalized_keypoints[:, :2]
return draw
def find_closest(moves, target):
return min(moves, key=lambda t: abs(t[0] - target))
def resize_with_padding(image, target_size=(640, 640)):
h, w = image.shape[:2]
target_w, target_h = target_size
# Oblicz współczynnik skalowania, zachowując proporcje
scale = min(target_w / w, target_h / h)
new_w, new_h = int(w * scale), int(h * scale)
# Resize obrazu
resized_image = cv2.resize(image, (new_w, new_h), interpolation=cv2.INTER_AREA)
# Stwórz tło (czarne) o wymiarach docelowych
output_image = np.zeros((target_h, target_w, 3), dtype=np.uint8)
# Oblicz offsety do wyśrodkowania obrazu
x_offset = (target_w - new_w) // 2
y_offset = (target_h - new_h) // 2
# Wklej resized image na tło
output_image[y_offset:y_offset+new_h, x_offset:x_offset+new_w] = resized_image
return output_image