GSL-video2text / utils /keypoints.py
tinh2312's picture
init
157fb39
Raw
History Blame Contribute Delete
4.35 kB
import time
import cv2 as cv
import numpy as np
total_body_idx = 23
total_face_idx = 68
total_hand = 42
body_idx = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
lefthand_idx = [91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111]
righthand_idx = [112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131,
132]
face_idx = [23, 26, 29, 31, 33, 36, 39, 40, 42, 44, 45, 47, 49, 53, 56, 59, 62, 65, 68, 71, 72, 73, 74, 75, 76, 77, 79,
80, 81, 82]
total_idx = body_idx + face_idx + lefthand_idx + righthand_idx
body_count = len(body_idx)
hand_count = len(lefthand_idx)
face_count = len(face_idx)
body_face_offset = total_body_idx - body_count - 1
face_hand_offset = face_count + body_count
body_connect = [(0, 1), (0, 2), (2, 4), (1, 3), (6, 5), (6, 8), (8, 10), (5, 7), (7, 9)]
lefthand_connect = [(91, 92), (92, 93), (93, 94), (94, 95), (91, 96), (96, 97), (97, 98), (98, 99),
(91, 100), (100, 101), (101, 102), (102, 103), (91, 104), (104, 105), (105, 106), (106, 107),
(91, 108), (108, 109), (109, 110), (110, 111), (93, 96), (96, 100), (104, 108)]
righthand_connect = [(112, 113), (113, 114), (114, 115), (115, 116), (112, 117), (117, 118), (118, 119), (119, 120),
(112, 121), (121, 122), (122, 123), (123, 124), (112, 125), (125, 126), (126, 127), (127, 128),
(112, 129), (129, 130), (130, 131), (131, 132), (114, 117), (117, 121), (125, 129)]
face_connect = [(23, 26), (26, 29), (29, 31), (31, 33), (33, 36), (36, 39), (40, 42), (42, 44),
(45, 47), (47, 49), (53, 56), (59, 62), (65, 68), (71, 72), (71, 82), (72, 73),
(73, 74), (74, 75), (75, 76), (76, 77), (77, 79), (79, 80), (80, 81)]
face_connect = [(body_face_offset + face_idx.index(x), body_face_offset + face_idx.index(y)) for (x, y) in face_connect]
lefthand_connect = [(face_hand_offset + lefthand_idx.index(x), face_hand_offset + lefthand_idx.index(y)) for (x, y) in
lefthand_connect]
righthand_connect = [(face_hand_offset + righthand_idx.index(x) + 21, face_hand_offset + righthand_idx.index(y) + 21)
for (x, y) in righthand_connect]
connections = body_connect + face_connect + lefthand_connect + righthand_connect + [(10, righthand_connect[0][0]),
(9, lefthand_connect[0][0])]
def get_bbox(keypoint):
x_coords = keypoint[:, 0]
y_coords = keypoint[:, 1]
w = max(x_coords) - min(x_coords)
h = max(y_coords) - min(y_coords)
if w > h:
delta_x = 0.05 * w
delta_y = delta_x + ((w - h) / 2)
else:
delta_y = 0.05 * h
delta_x = delta_y + ((h - w) / 2)
starting_point = ((min(x_coords) - delta_x), (min(y_coords) - delta_y))
ending_point = ((max(x_coords) + delta_x), (max(y_coords) + delta_y))
temp = [int(x) for x in starting_point + ending_point]
return (temp[0], temp[1]), (temp[2], temp[3])
def draw_from_keypoints(keypoint, w, h, image=None):
if image is None:
image = np.zeros((h, w, 3), dtype=np.uint8)
keypoint = keypoint[total_idx, :]
for (x, y, c) in keypoint:
if c < 0.6:
continue
cv.circle(image, (int(x), int(y)), radius=3, color=(0, 255, 0), thickness=1)
for (start_idx, end_idx) in connections:
start_point = keypoint[start_idx]
end_point = keypoint[end_idx]
if start_point[-1] < 0.6 or end_point[-1] < 0.6:
continue
x_start, y_start = int(start_point[0]), int(start_point[1])
x_end, y_end = int(end_point[0]), int(end_point[1])
cv.line(image, (x_start, y_start), (x_end, y_end), color=(0, 255, 255), thickness=1)
return image
def create_video_keypoints(keypoints, w, h, save_to):
fourcc = cv.VideoWriter_fourcc(*'mp4v') # Định dạng video, có thể dùng 'XVID' hoặc 'mp4v'
video_writer = cv.VideoWriter(save_to, fourcc, 5.0, (w, h)) # 20 FPS
for keypoint in keypoints:
image = draw_from_keypoints(keypoint, w, h)
image = cv.cvtColor(image, cv.COLOR_RGB2BGR)
video_writer.write(image)
time.sleep(0.1)
video_writer.release()
print(f"Video saved to {save_to}")