[to #42322933] video detecor support output with timestamp

适配demoservice,增加视频时间戳输出,每一个结果对应一个时间戳
        Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/10496879
This commit is contained in:
leyuan.hjy
2022-10-24 14:54:42 +08:00
committed by yingda.chen
parent 35644fa0a7
commit 8b28b725ee
3 changed files with 19 additions and 4 deletions

View File

@@ -16,6 +16,7 @@ from modelscope.models.builder import MODELS
from modelscope.preprocessors import LoadImage
from modelscope.utils.config import Config
from modelscope.utils.constant import ModelFile, Tasks
from .utils import timestamp_format
from .yolox.data.data_augment import ValTransform
from .yolox.exp import get_exp_by_name
from .yolox.utils import postprocess
@@ -99,14 +100,17 @@ class RealtimeVideoDetector(TorchModel):
def inference_video(self, v_path):
outputs = []
desc = 'Detecting video: {}'.format(v_path)
for frame, result in tqdm(
self.inference_video_iter(v_path), desc=desc):
for frame_idx, (frame, result) in enumerate(
tqdm(self.inference_video_iter(v_path), desc=desc)):
result = result + (timestamp_format(seconds=frame_idx
/ self.fps), )
outputs.append(result)
return outputs
def inference_video_iter(self, v_path):
capture = cv2.VideoCapture(v_path)
self.fps = capture.get(cv2.CAP_PROP_FPS)
while capture.isOpened():
ret, frame = capture.read()
if not ret:

View File

@@ -0,0 +1,9 @@
# Copyright (c) Alibaba, Inc. and its affiliates.
import math
def timestamp_format(seconds):
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
time = '%02d:%02d:%06.3f' % (h, m, s)
return time

View File

@@ -45,15 +45,17 @@ class RealtimeVideoObjectDetectionPipeline(Pipeline):
**kwargs) -> str:
forward_output = input['forward_output']
scores, boxes, labels = [], [], []
scores, boxes, labels, timestamps = [], [], [], []
for result in forward_output:
box, score, label = result
box, score, label, timestamp = result
scores.append(score)
boxes.append(box)
labels.append(label)
timestamps.append(timestamp)
return {
OutputKeys.BOXES: boxes,
OutputKeys.SCORES: scores,
OutputKeys.LABELS: labels,
OutputKeys.TIMESTAMPS: timestamps,
}