human/render/video_render.py

60 lines
1.9 KiB
Python
Raw Normal View History

2024-10-12 11:57:24 +00:00
#encoding = utf8
2024-10-22 11:57:30 +00:00
import copy
2024-11-24 16:13:04 +00:00
import logging
2024-10-23 09:44:33 +00:00
import time
2024-10-22 11:57:30 +00:00
import cv2
import numpy as np
2024-10-23 09:44:33 +00:00
from .base_render import BaseRender
2024-10-12 11:57:24 +00:00
2024-11-24 07:19:40 +00:00
def img_warp_back_inv_m(img, img_to, inv_m):
h_up, w_up, c = img_to.shape
2024-11-24 16:13:04 +00:00
t = time.perf_counter()
2024-11-24 07:19:40 +00:00
mask = np.ones_like(img).astype(np.float32)
inv_mask = cv2.warpAffine(mask, inv_m, (w_up, h_up))
inv_img = cv2.warpAffine(img, inv_m, (w_up, h_up))
2024-11-24 16:13:04 +00:00
mask_indices = inv_mask == 1
print(f'time1: {time.perf_counter() - t}')
if 4 == c:
t = time.perf_counter()
img_to[:, :, :3][mask_indices] = inv_img[mask_indices]
print(f'time2: {time.perf_counter() - t}')
else:
img_to[inv_mask == 1] = inv_img[inv_mask == 1]
2024-11-24 07:19:40 +00:00
return img_to
2024-10-22 11:57:30 +00:00
class VideoRender(BaseRender):
def __init__(self, play_clock, context, human_render):
2024-11-05 11:40:03 +00:00
super().__init__(play_clock, context, 'Video')
2024-10-22 11:57:30 +00:00
self._human_render = human_render
2024-11-19 15:18:09 +00:00
self.index = 0
2024-10-22 11:57:30 +00:00
2024-11-04 05:40:05 +00:00
def render(self, frame, ps):
res_frame, idx, type_ = frame
if type_ == 0:
combine_frame = self._context.frame_list_cycle[idx]
else:
bbox = self._context.coord_list_cycle[idx]
combine_frame = copy.deepcopy(self._context.frame_list_cycle[idx])
2024-11-24 16:13:04 +00:00
af = self._context.align_frames[idx]
2024-11-24 07:19:40 +00:00
inv_m = self._context.inv_m_frames[idx]
2024-11-04 05:40:05 +00:00
y1, y2, x1, x2 = bbox
try:
2024-11-24 16:13:04 +00:00
t = time.perf_counter()
2024-11-04 05:40:05 +00:00
res_frame = cv2.resize(res_frame.astype(np.uint8), (x2 - x1, y2 - y1))
2024-11-24 07:19:40 +00:00
af[y1:y2, x1:x2] = res_frame
combine_frame = img_warp_back_inv_m(af, combine_frame, inv_m)
2024-11-24 16:13:04 +00:00
print(time.perf_counter() - t)
2024-11-24 07:19:40 +00:00
except Exception as e:
2024-11-24 16:13:04 +00:00
logging.error(f'resize error:{e}')
2024-11-04 05:40:05 +00:00
return
image = combine_frame
2024-11-24 16:13:04 +00:00
# image = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA)
2024-11-04 05:40:05 +00:00
if self._human_render is not None:
self._human_render.put_image(image)