2024-10-12 11:57:24 +00:00
|
|
|
#encoding = utf8
|
2024-10-22 11:57:30 +00:00
|
|
|
import copy
|
2024-10-23 09:44:33 +00:00
|
|
|
import time
|
2024-10-22 11:57:30 +00:00
|
|
|
from queue import Empty
|
2024-10-25 00:23:55 +00:00
|
|
|
from enum import Enum
|
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-25 00:23:55 +00:00
|
|
|
from human.message_type import MessageType
|
2024-10-12 11:57:24 +00:00
|
|
|
|
|
|
|
|
2024-10-22 11:57:30 +00:00
|
|
|
class VideoRender(BaseRender):
|
|
|
|
def __init__(self, play_clock, context, human_render):
|
2024-10-25 00:23:55 +00:00
|
|
|
super().__init__(play_clock, context, 'Video')
|
2024-10-22 11:57:30 +00:00
|
|
|
self._human_render = human_render
|
|
|
|
|
2024-10-23 09:44:33 +00:00
|
|
|
def _run_step(self):
|
2024-10-25 00:23:55 +00:00
|
|
|
while self._exit_event.is_set():
|
2024-10-22 11:57:30 +00:00
|
|
|
try:
|
2024-10-25 00:23:55 +00:00
|
|
|
frame, ps = self._queue.get(block=True, timeout=0.01)
|
|
|
|
res_frame, idx, type_ = frame
|
|
|
|
except Empty:
|
2024-10-22 11:57:30 +00:00
|
|
|
return
|
|
|
|
|
2024-10-25 00:23:55 +00:00
|
|
|
clock_time = self._play_clock.clock_time()
|
|
|
|
time_difference = clock_time - ps
|
2024-10-23 09:44:33 +00:00
|
|
|
|
2024-10-25 00:23:55 +00:00
|
|
|
print('video render:', ps, ' ', clock_time, ' ', time_difference)
|
|
|
|
if time_difference < -self._play_clock.audio_diff_threshold:
|
|
|
|
sleep_time = abs(time_difference + self._play_clock.audio_diff_threshold)
|
|
|
|
print("Video frame waiting to catch up with audio", sleep_time)
|
|
|
|
if sleep_time > 0:
|
|
|
|
time.sleep(sleep_time)
|
2024-10-23 09:44:33 +00:00
|
|
|
|
2024-10-25 00:23:55 +00:00
|
|
|
elif time_difference > self._play_clock.audio_diff_threshold: # 视频比音频快超过10ms
|
|
|
|
print("Video frame dropped to catch up with audio")
|
|
|
|
continue
|
2024-10-22 11:57:30 +00:00
|
|
|
|
2024-10-25 00:23:55 +00:00
|
|
|
print('get face', self._queue.qsize())
|
2024-10-22 11:57:30 +00:00
|
|
|
|
2024-10-25 00:23:55 +00:00
|
|
|
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])
|
|
|
|
y1, y2, x1, x2 = bbox
|
|
|
|
try:
|
|
|
|
res_frame = cv2.resize(res_frame.astype(np.uint8), (x2 - x1, y2 - y1))
|
|
|
|
except:
|
|
|
|
print('resize error')
|
|
|
|
return
|
|
|
|
combine_frame[y1:y2, x1:x2] = res_frame
|
|
|
|
|
|
|
|
image = combine_frame
|
|
|
|
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
|
|
|
if self._human_render is not None:
|
|
|
|
self._human_render.put_image(image)
|
|
|
|
return
|