python怎么处理帧数不等的视频

发布时间:2021-12-27 13:54:46 作者:iii
来源:亿速云 阅读:274

这篇文章主要介绍“python怎么处理帧数不等的视频”,在日常操作中,相信很多人在python怎么处理帧数不等的视频问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”python怎么处理帧数不等的视频”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

训练和测试一个有效的机器学习模型最重要的一步是收集大量数据并使用这些数据对其进行有效训练。小批量(Mini-batches)有助于解决这个问题,在每次迭代中使用一小部分数据进行训练。

但是,随着大量的机器学习任务在视频数据集上执行,存在着对不等长视频进行有效批处理的问题。大多数方法依赖于将视频裁剪成相等的长度,以便在迭代期间提取相同数量的帧。但在我们需要从每一帧获取信息来有效地预测某些事情的场景中,这并不是特别有用,特别是在自动驾驶汽车和动作识别的情况下。

我们可以创建一个可以处理不同长度视频的处理方法。

在Glenn Jocher的Yolov3中,我用LoadStreams作为基础,创建了LoadStreamsBatch类。

类初始化
def __init__(self, sources='streams.txt', img_size=416, batch_size=2, subdir_search=False):
        self.mode = 'images'
        self.img_size = img_size
        self.def_img_size = None

        videos = []
        if os.path.isdir(sources):
            if subdir_search:
                for subdir, dirs, files in os.walk(sources):
                    for file in files:
                        if 'video' in magic.from_file(subdir + os.sep + file, mime=True):
                            videos.append(subdir + os.sep + file)
            else:
                for elements in os.listdir(sources):
                    if not os.path.isdir(elements) and 'video' in magic.from_file(sources + os.sep + elements, mime=True):
                        videos.append(sources + os.sep + elements)
        else:
            with open(sources, 'r') as f:
                videos = [x.strip() for x in f.read().splitlines() if len(x.strip())]

        n = len(videos)
        curr_batch = 0
        self.data = [None] * batch_size
        self.cap = [None] * batch_size
        self.sources = videos
        self.n = n
        self.cur_pos = 0

        # 启动线程从视频流中读取帧
        for i, s in enumerate(videos):
            if curr_batch == batch_size:
                break
            print('%g/%g: %s... ' % (self.cur_pos+1, n, s), end='')
            self.cap[curr_batch] = cv2.VideoCapture(s)
            try:
                assert self.cap[curr_batch].isOpened()
            except AssertionError:
                print('Failed to open %s' % s)
                self.cur_pos+=1
                continue
            w = int(self.cap[curr_batch].get(cv2.CAP_PROP_FRAME_WIDTH))
            h = int(self.cap[curr_batch].get(cv2.CAP_PROP_FRAME_HEIGHT))
            fps = self.cap[curr_batch].get(cv2.CAP_PROP_FPS) % 100
            frames = int(self.cap[curr_batch].get(cv2.CAP_PROP_FRAME_COUNT))
            _, self.data[i] = self.cap[curr_batch].read()  # guarantee first frame
            thread = Thread(target=self.update, args=([i, self.cap[curr_batch], self.cur_pos+1]), daemon=True)
            print(' success (%gx%g at %.2f FPS having %g frames).' % (w, h, fps, frames))
            curr_batch+=1
            self.cur_pos+=1
            thread.start()
            print('')  # 新的一行

        if all( v is None for v in self.data ):
            return
        # 检查常见形状
        s = np.stack([letterbox(x, new_shape=self.img_size)[0].shape for x in self.data], 0)  # 推理的形状
        self.rect = np.unique(s, axis=0).shape[0] == 1
        if not self.rect:
            print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.')

在*__init__*函数中,接受四个参数。虽然img_size与原始版本相同,但其他三个参数定义如下:

我首先检查sources参数是目录还是文本文件。如果是一个目录,我会读取目录中的所有内容(如果subdir_search参数为True,子目录也会包括在内),否则我会读取文本文件中视频的路径。视频的路径存储在列表中。使用cur_pos以跟踪列表中的当前位置。

该列表以batch_size为最大值进行迭代,并检查以跳过错误视频或不存在的视频。它们被发送到letterbox函数,以调整图像大小。这与原始版本相比没有任何变化,除非所有视频都有故障/不可用。

def letterbox(img, new_shape=(416, 416), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True):
    # 将图像调整为32个像素倍数的矩形 https://github.com/ultralytics/yolov3/issues/232
    shape = img.shape[:2]  # 当前形状 [height, width]
    if isinstance(new_shape, int):
        new_shape = (new_shape, new_shape)

    # 比例
    r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
    if not scaleup:  # 只按比例缩小,不按比例放大(用于更好的测试图)
        r = min(r, 1.0)

    # 计算填充
    ratio = r, r  # 宽高比
    new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
    dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]  #填充
    if auto:  # 最小矩形
        dw, dh = np.mod(dw, 64), np.mod(dh, 64)  # 填充
    elif scaleFill:  # 伸展
        dw, dh = 0.0, 0.0
        new_unpad = new_shape
        ratio = new_shape[0] / shape[1], new_shape[1] / shape[0]  # 宽高比

    dw /= 2  # 将填充分成两侧
    dh /= 2

    if shape[::-1] != new_unpad:  # 改变大小
        img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
    top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
    left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
    img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  # 添加边界
    return img, ratio, (dw, dh)
固定间隔检索帧函数

update函数有一个小的变化,我们另外存储了默认的图像大小,以便在所有视频都被提取进行处理,但由于长度不相等,一个视频比另一个视频提前完成。当我解释代码的下一部分时,它会更清楚,那就是*__next__* 函数。

def update(self, index, cap, cur_pos):
        # 读取守护进程线程中的下一个帧
        n = 0
        while cap.isOpened():
            n += 1
            # _, self.imgs[index] = cap.read()
            cap.grab()
            if n == 4:  # 每4帧读取一次
                _, self.data[index] = cap.retrieve()
                if self.def_img_size is None:
                    self.def_img_size = self.data[index].shape
                n = 0
            time.sleep(0.01)  # 等待
迭代器

如果帧存在,它会像往常一样传递给letterbox函数。在frame为None的情况下,这意味着视频已被完全处理,我们检查列表中的所有视频是否都已被处理。如果有更多的视频要处理,cur_pos指针用于获取下一个可用视频的位置。

如果不再从列表中提取视频,但仍在处理某些视频,则向其他处理组件发送一个空白帧,即,它根据其他批次中的剩余帧动态调整视频大小。

def __next__(self):
        self.count += 1
        img0 = self.data.copy()
        img = []

        for i, x in enumerate(img0):
            if x is not None:
                img.append(letterbox(x, new_shape=self.img_size, auto=self.rect)[0])
            else:
                if self.cur_pos == self.n:
                    if all( v is None for v in img0 ):
                        cv2.destroyAllWindows()
                        raise StopIteration
                    else:
                        img0[i] = np.zeros(self.def_img_size)
                        img.append(letterbox(img0[i], new_shape=self.img_size, auto=self.rect)[0])
                else:
                    print('%g/%g: %s... ' % (self.cur_pos+1, self.n, self.sources[self.cur_pos]), end='')
                    self.cap[i] = cv2.VideoCapture(self.sources[self.cur_pos])
                    fldr_end_flg = 0
                    while not self.cap[i].isOpened():
                        print('Failed to open %s' % self.sources[self.cur_pos])
                        self.cur_pos+=1
                        if self.cur_pos == self.n:
                            img0[i] = np.zeros(self.def_img_size)
                            img.append(letterbox(img0[i], new_shape=self.img_size, auto=self.rect)[0])
                            fldr_end_flg = 1
                            break
                        self.cap[i] = cv2.VideoCapture(self.sources[self.cur_pos])
                    if fldr_end_flg:
                        continue
                    w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
                    h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
                    fps = cap.get(cv2.CAP_PROP_FPS) % 100
                    frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
                    _, self.data[i] = self.cap[i].read()  # 保证第一帧
                    img0[i] = self.data[i]
                    img.append(letterbox(self.data[i], new_shape=self.img_size, auto=self.rect)[0])
                    thread = Thread(target=self.update, args=([i, self.cap[i], self.cur_pos+1]), daemon=True)
                    print(' success (%gx%g at %.2f FPS having %g frames).' % (w, h, fps, frames))
                    self.cur_pos+=1
                    thread.start()
                    print('')  # 新的一行

        # 堆叠
        img = np.stack(img, 0)

        # 转换
        img = img[:, :, :, ::-1].transpose(0, 3, 1, 2)  # BGR 到 RGB, bsx3x416x416
        img = np.ascontiguousarray(img)

        return self.sources, img, img0, None

到此,关于“python怎么处理帧数不等的视频”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. python表示不等于的方法
  2. python如何处理RSTP视频流

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:liferay与portlet之间如何通信

下一篇:Android如何自定View实现滑动验证效果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》