38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
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 find_closest(target):
|
|
global moves
|
|
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 |