def run(self):
        while not self.video_metas.empty():
            video_meta = None
            try:
                print 'undeal %d' % (self.video_metas.qsize(), )
                video_meta = self.video_metas.get()
                if not os.path.exists(video_meta.frames_file_dir):
                    os.makedirs(video_meta.frames_file_dir)

                if os.path.exists(video_meta.meta_path) and os.path.isfile(
                        video_meta.meta_path):
                    print('video %s had deal' % (video_meta.video_path, ))
                    # selvideo_metas.remove(video_meta)
                    continue

                video_shape = get_video_shape(video_meta.video_path)
                frames_dirs = self.extract_frame(
                    video_meta,
                    video_shape,
                    video_meta.frames_file_pattern,
                    frame_rate=self.frame_rate,
                    distinct=self.distinct)
                with open(video_meta.meta_path, 'wb') as meta_file:
                    meta_file.writelines(frames_dirs)

                self.save_to_database(video_meta.meta_path)
            except Exception, e:
                print 'threading error'
                print os.path.exists(video_meta.meta_path)
                if os.path.exists(video_meta.meta_path):

                    os.remove(video_meta.meta_path)
                raise RuntimeError()
Exemple #2
0
def extract_frame(args, video_path):
    video_path = os.path.abspath(video_path)
    video_file = os.path.basename(video_path)
    video_name = os.path.splitext(video_file)[0]
    frame_rate = args.frame_rate if args.frame_rate is not None else None
    frame_nums = args.frame_nums if args.frame_nums is not None and len(
        args.frame_nums) > 0 else None
    distinct = args.distinct if args.distinct is not None else False
    video_shape = get_video_shape(video_path)

    if frame_nums is None:

        frame_generator = VideoFrameGenerator(video_path, video_shape,
                                              frame_rate)

        with frame_generator as frames:
            i = 1
            prev_frame = None
            for frame in frames:
                should_write = True
                if prev_frame is not None and args.distinct:
                    should_write = not numpy.array_equal(prev_frame, frame)

                if should_write:
                    process(args, video_path, frame, i)
                prev_frame = frame
                i += 1

    else:
        frame_nums = sorted(frame_nums)
        max_frame_num = max(frame_nums)
        frame_generator = VideoFrameGenerator(video_path, video_shape,
                                              frame_rate)

        with frame_generator as frames:
            frame_num_index = 0
            frame_num = frame_nums[frame_num_index]
            i = 1

            for frame in frames:
                should_write = False
                if frame_nums is None:
                    # no frame_nums, write all
                    should_write = True
                else:
                    if i > max_frame_num:
                        break

                    if i == frame_num:
                        should_write = True
                        frame_num_index += 1
                        if frame_num_index < len(frame_nums):
                            frame_num = frame_nums[frame_num_index]

                if should_write:
                    process(args, frame, i)
                i += 1
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        default=False,
                        help='verbose')
    parser.add_argument('-r', '--frame-rate', type=float, help='frame rate')
    parser.add_argument('-d',
                        '--distinct',
                        action='store_true',
                        default=False,
                        help='distinct')
    parser.add_argument('-n',
                        '--frame-nums',
                        nargs='*',
                        type=int,
                        help='frame nums')
    parser.add_argument('-t', '--frame-type', help='frame type')
    parser.add_argument('--output-dir', help='output dir')
    parser.add_argument('video_paths', nargs='+')
    args = parser.parse_args()

    frame_rate = args.frame_rate
    distinct = args.distinct
    frame_nums = args.frame_nums if args.frame_nums is not None and len(
        args.frame_nums) > 0 else None
    frame_type = args.frame_type if args.frame_type is not None else 'jpg'

    for video_path in args.video_paths:
        video_path = os.path.abspath(video_path)
        video_file = os.path.basename(video_path)
        video_name = os.path.splitext(video_file)[0]

        frames_dir = args.output_dir
        if frames_dir is None:
            frames_basedir = os.path.dirname(video_path)
            frames_dir = os.path.join(frames_basedir, video_name)

            if not os.path.exists(frames_dir):
                os.mkdir(frames_dir)

        video_shape = get_video_shape(video_path)
        extract_frame(video_path,
                      video_shape,
                      frames_dir,
                      frame_rate=frame_rate,
                      distinct=distinct,
                      frame_nums=frame_nums,
                      frame_type=frame_type)
Exemple #4
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        default=False,
                        help='verbose')
    parser.add_argument('video_path', help='video path')
    parser.add_argument('frame_rate', type=float, help='frame rate')
    parser.add_argument('frame_nums', nargs='+', type=int, help='frame nums')
    args = parser.parse_args()

    video_path = os.path.abspath(args.video_path)
    frame_rate = args.frame_rate
    frame_nums = args.frame_nums

    video_shape = get_video_shape(video_path)
    view_frame(video_path, video_shape, frame_rate, frame_nums)
Exemple #5
0
def get_frame_generator(video_path, frame_rate):
    video_shape = get_video_shape(video_path)
    return VideoFrameGenerator(video_path, video_shape, frame_rate)
Exemple #6
0
def process_image_handler(error_csv_output_path, fr_csv_output_path,
                          video_file_path, image_basedir, roi_setting,
                          settings):
    frame_type = 'bmp'
    video_name = os.path.basename(video_file_path).split('.')[0]
    platform = video_name.split('_')[1]
    app_name = video_name.split('_')[0]
    # TODO 把参数获取方式整理好,现在好恶心
    pre_roi_frames_dict = None
    try:
        pre_roi_frames_dict = settings.get_specific_settings(
            platform, app_name, 'pre_roi')
    except ValueError as e:
        logger.info('%s_%s no need pre roi' % (app_name, platform))

    resize_frames_dict = settings.get_specific_settings(
        platform, app_name, 'resize')
    video_shape = get_video_shape(video_file_path)
    # frame_rate = get_frame_rate(video_file_path)
    frame_rate = 60
    if not os.path.exists(image_basedir):
        os.makedirs(image_basedir)

    src_align_frames_dict = settings.get_specific_settings(
        platform, app_name, 'align')
    src_align_frames_path = src_align_frames_dict.get('align_src')
    fill_format = '%05d.'
    frames_file_pattern = os.path.join(image_basedir,
                                       video_name + fill_format + frame_type)
    src_frames_file_pattern = os.path.join(
        # src_align_frames_path, 'default' + fill_format + frame_type
        src_align_frames_path,
        '0' + fill_format + frame_type)

    x = roi_setting.get('x')
    y = roi_setting.get('y')
    width = roi_setting.get('width')
    height = roi_setting.get('height')

    if pre_roi_frames_dict:
        pre_x = pre_roi_frames_dict.get('x')
        pre_y = pre_roi_frames_dict.get('y')
        pre_width = pre_roi_frames_dict.get('width')
        pre_height = pre_roi_frames_dict.get('height')

    resize_width = resize_frames_dict.get('width')
    resize_height = resize_frames_dict.get('height')

    if not width:
        width = video_shape[0] - x
    if not height:
        height = video_shape[1] - y

    logger.info('use frame rate:{0} to extract video'.format(frame_rate))
    frame_generator = VideoFrameGenerator(video_file_path, video_shape,
                                          frame_rate)
    with frame_generator as frames:
        i = 1
        prev_frame = None
        # 数字区域坐标信息
        block_info = []
        with open(error_csv_output_path,
                  'wb') as error_csv, open(fr_csv_output_path, 'wb') as fr_csv:
            error_csv_writer = csv.writer(error_csv)
            fr_csv_writer = csv.writer(fr_csv)
            for frame in frames:
                should_write = True

                # 切黑边
                if pre_roi_frames_dict:
                    pre_roi_filter = ROIFrameFilter(pre_x, pre_y, pre_width,
                                                    pre_height)
                    frame = pre_roi_filter.filter(frame)
                # 放大
                if video_shape[0] != resize_width or video_shape[
                        1] != resize_height:
                    resize_filter = ResizeFrameFilter(resize_width,
                                                      resize_height)
                    frame = resize_filter.filter(frame)
                # 切广告和水印
                roi_filter = ROIFrameFilter(x, y, width, height)
                roi_frame = roi_filter.filter(frame)

                if prev_frame is not None:
                    should_write = not numpy.array_equal(prev_frame, roi_frame)
                if should_write:
                    frame_file_name = frames_file_pattern % (i, )
                    black_detector = AverageGrayThresholdDetector(0)
                    if int(black_detector.get_avg_gray(roi_frame)) > 20:
                        cv2.imwrite(frames_file_pattern % (i, ), roi_frame)
                        try:
                            # 识别失败会返回空字符串,int 强制转换会 raise exception
                            num = int(
                                detector.Detector(img=frame,
                                                  logger=logger).num_str)

                            src_path = src_frames_file_pattern % (num, )
                            if not os.path.exists(src_path):
                                logger.error('src pic %s is not exists.' %
                                             src_path)
                            target_path = frame_file_name
                            src = cv2.imread(src_path, cv2.IMREAD_GRAYSCALE)
                            target = cv2.imread(target_path,
                                                cv2.IMREAD_GRAYSCALE)
                            score = ssim(src, target, downsample=False)
                            row = [
                                target_path, src_path,
                                round(float(score), 5)
                            ]
                            fr_csv_writer.writerow(row)
                        except Exception:
                            logger.exception('%s detect number failed' %
                                             frame_file_name)
                            error_csv_writer.writerow([frame_file_name])
                prev_frame = roi_frame
                i += 1
    return [
        os.path.join(image_basedir, _) for _ in os.listdir(image_basedir)
        if os.path.isfile(os.path.join(image_basedir, _))
    ]
Exemple #7
0
def process_image_handler(error_csv_output_path, fr_csv_output_path,
                          video_file_path, image_basedir, roi_setting,
                          settings):
    frame_type = 'bmp'
    video_name = os.path.basename(video_file_path).split('.')[0]
    platform = video_name.split('_')[1]
    app_name = video_name.split('_')[0]
    # TODO 把参数获取方式整理好,现在好恶心
    pre_roi_frames_dict = None
    try:
        pre_roi_frames_dict = settings.get_specific_settings(
            platform, app_name, 'pre_roi')
    except ValueError as e:
        logger.info('%s_%s no need pre roi' % (app_name, platform))

    resize_frames_dict = settings.get_specific_settings(
        platform, app_name, 'resize')
    video_shape = get_video_shape(video_file_path)
    # frame_rate = get_frame_rate(video_file_path)
    frame_rate = 60
    if not os.path.exists(image_basedir):
        os.makedirs(image_basedir)

    src_align_frames_dict = settings.get_specific_settings(
        platform, app_name, 'align')
    src_align_frames_path = src_align_frames_dict.get('align_src')
    fill_format = '%05d.'
    frames_file_pattern = os.path.join(image_basedir,
                                       video_name + fill_format + frame_type)
    src_frames_file_pattern = os.path.join(
        # src_align_frames_path, 'default' + fill_format + frame_type
        src_align_frames_path,
        '0' + fill_format + frame_type)

    x = roi_setting.get('x')
    y = roi_setting.get('y')
    width = roi_setting.get('width')
    height = roi_setting.get('height')

    if pre_roi_frames_dict:
        pre_x = pre_roi_frames_dict.get('x')
        pre_y = pre_roi_frames_dict.get('y')
        pre_width = pre_roi_frames_dict.get('width')
        pre_height = pre_roi_frames_dict.get('height')

    resize_width = resize_frames_dict.get('width')
    resize_height = resize_frames_dict.get('height')

    if not width:
        width = video_shape[0] - x
    if not height:
        height = video_shape[1] - y

    logger.info('use frame rate:{0} to extract video'.format(frame_rate))
    frame_generator = VideoFrameGenerator(video_file_path, video_shape,
                                          frame_rate)
    with frame_generator as frames:
        i = 1
        prev_frame = None
        # 数字区域坐标信息
        block_info = []
        from contextlib import nested
        with nested(open(error_csv_output_path, 'wb'),
                    open(fr_csv_output_path, 'wb')) as (error_csv, fr_csv):
            error_csv_writer = csv.writer(error_csv)
            fr_csv_writer = csv.writer(fr_csv)
            for frame in frames:
                should_write = True

                # 切黑边
                if pre_roi_frames_dict:
                    pre_roi_filter = ROIFrameFilter(pre_x, pre_y, pre_width,
                                                    pre_height)
                    frame = pre_roi_filter.filter(frame)
                # 放大
                if video_shape[0] != resize_width or video_shape[
                        1] != resize_height:
                    resize_filter = ResizeFrameFilter(resize_width,
                                                      resize_height)
                    frame = resize_filter.filter(frame)
                # 切广告和水印
                roi_filter = ROIFrameFilter(x, y, width, height)
                roi_frame = roi_filter.filter(frame)

                if prev_frame is not None:
                    should_write = not numpy.array_equal(prev_frame, roi_frame)
                if should_write:
                    frame_file_name = frames_file_pattern % (i, )

                    # 数字识别,使用放大后的图(未切广告和水印)
                    if not block_info:
                        block_info = num_detector.detect_num_block(frame)
                        if block_info:
                            logger.info('detect block_info by %s success: %s' %
                                        (frame_file_name, block_info))
                        else:
                            logger.error('detect block_info by %s failed.' %
                                         frame_file_name)

                    black_detector = AverageGrayThresholdDetector(0)
                    if int(black_detector.get_avg_gray(roi_frame)) > 2:
                        cv2.imwrite(frames_file_pattern % (i, ), roi_frame)

                        num_str = num_detector.detect(frame, block_info)
                        # print num_str
                        try:
                            num = int(num_str)
                            # 黑帧不写入对帧结果,有效帧范围:301~7506
                            if 300 < num < 7507:
                                src_path = src_frames_file_pattern % (num, )
                                target_path = frame_file_name
                                src = cv2.imread(src_path,
                                                 cv2.IMREAD_GRAYSCALE)
                                target = cv2.imread(target_path,
                                                    cv2.IMREAD_GRAYSCALE)
                                score = ssim(src, target, downsample=False)
                                row = [
                                    target_path, src_path,
                                    round(float(score), 5)
                                ]
                                # detect_csv_writer.writerow(row)
                                fr_csv_writer.writerow(row)
                        except ValueError as e:
                            logger.error('%s detect number failed' %
                                         frame_file_name)
                            error_csv_writer.writerow([frame_file_name])
                prev_frame = roi_frame
                i += 1
    return [
        os.path.join(image_basedir, _) for _ in os.listdir(image_basedir)
        if os.path.isfile(os.path.join(image_basedir, _))
    ]
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        default=False,
                        help='verbose')
    parser.add_argument('-r',
                        '--frame-rate',
                        type=float,
                        help='frame rate',
                        default='1.0')
    parser.add_argument('-d',
                        '--distinct',
                        action='store_true',
                        default=False,
                        help='distinct')
    parser.add_argument('-t', '--frame-type', help='frame type')
    parser.add_argument('video_dir', help='video\'s dir')
    parser.add_argument('output_disk', help='output disk')
    parser.add_argument('business_type',
                        help="pron:0, ad_orc:1, game_ocr:2",
                        choices=['0', '1', '2'])
    parser.add_argument('col_int_01',
                        help='game type or something else',
                        choices=[
                            'juediqiusheng', 'huangyexingdong',
                            'cijizhanchang', 'quanjunchuji', 'wangzherongyao'
                        ])
    args = parser.parse_args()

    frame_rate = args.frame_rate
    distinct = args.distinct
    frame_type = args.frame_type if args.frame_type is not None else 'jpg'
    output_disk = args.output_disk
    business_type = args.business_type
    col_int_01 = args.col_int_01

    if not os.path.exists(args.video_dir):
        raise RuntimeError('video dir ' + args.video_dir + 'not exists')

    if not os.path.exists(output_disk):
        raise RuntimeError('output disk ' + args.output_disk + 'not exists')

    # prepare video
    video_metas = []
    video_metas_queue = Queue.Queue()
    for root, dirs, files in os.walk(args.video_dir):
        if files:
            for file_ in files:
                # print file_
                if file_.lower().endswith(deal_ext):
                    video_metas_queue.put(
                        VideoMeta(os.path.join(root, file_), output_disk))

    video_meta = None
    try:
        while video_metas_queue.qsize() > 0:
            video_meta = video_metas_queue.get()
            print("undeal video %s" % (video_metas_queue.qsize()))
            if not os.path.exists(video_meta.frames_file_dir):
                os.makedirs(video_meta.frames_file_dir)

            if os.path.exists(video_meta.meta_path) and os.path.isfile(
                    video_meta.meta_path):
                print('video %s had deal' % (video_meta.video_path, ))
                continue

            video_shape = get_video_shape(video_meta.video_path)
            frames_dirs = extract_frame(video_meta,
                                        video_shape,
                                        video_meta.frames_file_pattern,
                                        frame_rate=frame_rate,
                                        distinct=distinct)
            with open(video_meta.meta_path, 'wb') as meta_file:
                meta_file.writelines(frames_dirs)

            save_to_database(video_meta.meta_path, business_type,
                             GameType[col_int_01].value)
    except Exception:
        if os.path.exists(video_meta.meta_path):
            os.remove(video_meta.meta_path)

        print traceback.format_exc()