コード例 #1
0
def extract_features_from_vid(video_path):
    """
    This function extracts 3D CNN features for a single query video.
    """
    opt = parse_opts()
    opt.input = video_path
    opt.model = './3d-cnn/resnet-34-kinetics.pth'
    opt.mode = 'feature'
    opt.mean = get_mean()
    opt.arch = '{}-{}'.format(opt.model_name, opt.model_depth)
    opt.sample_size = 112
    opt.sample_duration = 8
    opt.n_classes = 400

    model = generate_model(opt)
    # print('loading model {}'.format(opt.model))
    model_data = torch.load(opt.model)
    assert opt.arch == model_data['arch']
    model.load_state_dict(model_data['state_dict'])
    model.eval()
    if opt.verbose:
        print(model)

    outputs = []
    class_names = []
    with open('./3d-cnn/class_names_list') as f:
        for row in f:
            class_names.append(row[:-1])

    if os.path.exists(opt.input):
        subprocess.call('mkdir tmp', shell=True)
        subprocess.call('ffmpeg -loglevel panic -i {} tmp/image_%05d.jpg'.format(video_path),shell=True)
        result = classify_video('tmp', video_path, class_names, model, opt)
        outputs.append(result)
        subprocess.call('rm -rf tmp', shell=True)
    else:
        print('{} does not exist'.format(video_path))

    return np.array(outputs[0]['clips'][0]['features']).astype('float32')
コード例 #2
0
    if opt.verbose:
        ffmpeg_loglevel = 'info'
    
    #删除tmp文件夹
    if os.path.exists('tmp'):
        subprocess.call('rm -rf tmp', shell=True)
    
    #测试
    outputs = []
    for input_file in input_files:
        video_path = os.path.join(opt.video_root, input_file)#视频路径
        if os.path.exists(video_path):
            print(video_path)
            subprocess.call('mkdir tmp', shell=True)
            subprocess.call('ffmpeg -i {} tmp/image_%05d.jpg'.format(video_path),
                            shell=True)

            result = classify_video('tmp', input_file, class_names, model, opt)
            outputs.append(result)#测试结果

            subprocess.call('rm -rf tmp', shell=True)
        else:
            print('{} does not exist'.format(input_file))

    if os.path.exists('tmp'):
        subprocess.call('rm -rf tmp', shell=True)
    
    #保存结果
    with open(opt.output, 'w') as f:
        json.dump(outputs, f)
コード例 #3
0
                if prefix in exists_keys:
                    print('pass: '******'too small: ' + input_file)
                    continue
                if os.path.exists(video_path):

                    subprocess.call('mkdir tmp_' + ctg, shell=True)
                    subprocess.call('ffmpeg -i {} tmp_'.format(video_path) +
                                    ctg + '/image_%05d.jpg',
                                    shell=True)
                    # pdb.set_trace()
                    video, clips = classify_video('tmp_' + ctg, input_file,
                                                  model, opt)
                    prefix = video.split('.')[0]
                    outputs[prefix] = clips
                    subprocess.call('rm -rf tmp_' + ctg, shell=True)
                else:
                    print('{} does not exist'.format(input_file))
            except Exception as e:
                print(e)
                # os.remove(os.path.join(opt.video_root, input_file))
                if os.path.exists('tmp_' + ctg):
                    subprocess.call('rm -rf tmp_' + ctg, shell=True)
        # np.save(opt.duration_path,mp4_duration)
    # pdb.set_trace()
    np.save(opt.savename, outputs)
    # with open(opt.output, 'w') as f:
    #     json.dump(outputs, f)
コード例 #4
0
    ffmpeg_loglevel = 'quiet'
    if opt.verbose:
        ffmpeg_loglevel = 'info'

    if os.path.exists('tmp'):
        subprocess.call('rm -rf tmp', shell=True)

    outputs = []
    for input_file in input_files:
        video_path = os.path.join(opt.video_root, input_file)
        if os.path.exists(video_path):
            print(video_path)
            subprocess.call('mkdir tmp', shell=True)
            # subprocess.call('ffmpeg -i {} tmp/image_%05d.jpg'.format(video_path),
            #                 shell=True)
            # subprocess.call('cp {}/* tmp/'.format(video_path))

            result = classify_video(video_path, input_file, class_names, model,
                                    opt, classify_time)
            outputs.append(result)

            subprocess.call('rm -rf tmp', shell=True)
        else:
            print('{} does not exist'.format(input_file))

    if os.path.exists('tmp'):
        subprocess.call('rm -rf tmp', shell=True)

    with open(opt.output, 'w') as f:
        json.dump(outputs, f)
コード例 #5
0
        n = n+1
        if not ok:
            break

        # Start timer
        timer = cv2.getTickCount()

        save_dir = "tmp"
        pil_im = frame
        save_change(save_dir, pil_im, n, bbox)

        # Update tracker
        ok, bbox = tracker.update(frame)

        if n % 17 == 0:   # do not about the number bigger than 16
            result = classify_video('tmp', "tmp_video" , class_names, model, opt)
            print(result)
            list_result = result["clips"]
            list_result_init = list_result[0]

            if os.path.exists('tmp'):
                subprocess.call('rm -rf tmp', shell=True)

            subprocess.call('mkdir tmp', shell=True)
            n = 0





コード例 #6
0
            subprocess.call('mkdir -p %s' % (video_name), shell=True)
            #subprocess.call('ffmpeg -i {} %s/image_%05d.jpg'.format(video_path, video_name),
            #                shell=True)
            command = 'ffmpeg -i ' + video_path + ' %s/image_' % (
                video_name) + '%05d.jpg'
            print(command)
            #sys.exit(0)
            os.system(command)

            print('Number of images', len(os.listdir(video_name)))
            #sys.exit(0)

            #for filename in os.listdir('./tmp'):
            #    print(filename)

            result = classify_video(video_name, input_file, class_names, model,
                                    opt)
            outputs.append(result)

            #subprocess.call('rm -rf tmp', shell=True)
            #if os.path.exists(video_name):
            #    subprocess.call('rm -rf %s' %(video_name), shell=True)

            output_filename = os.path.join(
                '/checkpoint02/spandanagella/dataset/activitynet/3DResNet/%s_%s'
                % (opt.model_name, opt.model_depth), name + '.json')
            if len(outputs[0]['clips']) > 0:
                with open(output_filename, 'w') as f:
                    json.dump(outputs, f)
            print output_filename
            #sys.exit(0)
        else:
コード例 #7
0
        ext = 'gif'
    else:
        ext = 'mp4'
    files = sorted(glob.glob(opt.video_root + '/*.{}'.format(ext)))
    files = files[opt.start_idx:opt.end_idx]

    if opt.vidset is not None:
        vidset = pkl.load(open(opt.vidset, 'rb'))
    else:
        vidset = None

    for video_path in tqdm(files):
        input_file = video_path.split('/')[-1]
        video_name = input_file.split('.')[0]
        if not os.path.exists(video_path) or (vidset is not None
                                              and video_name not in vidset):
            continue
        subprocess.call('mkdir {}/{}'.format(opt.tmp, video_name), shell=True)
        subprocess.call(
            'ffmpeg -loglevel quiet -nostats -i {} -vsync 0 {}/{}/image_%05d.jpg'
            .format(video_path, opt.tmp, video_name),
            shell=True)
        result = classify_video('{}/{}'.format(opt.tmp, video_name),
                                input_file, class_names, model, opt)
        video_name = input_file.split('.')[0]
        result['st_feature'].dump(opt.output + '/' + video_name + '.npy')
        subprocess.call('rm -rf {}/{}'.format(opt.tmp, video_name), shell=True)

    if os.path.exists(opt.tmp):
        subprocess.call('rm -rf {}'.format(opt.tmp), shell=True)
コード例 #8
0
        subprocess.call('rm -rf tmpv11', shell=True)
    if not os.path.exists('resnext101-64f'):
        subprocess.call('mkdir resnext101-64f', shell=True)

    val_set = hdf5storage.read(path='/img_path', filename='./val_set.h5')
    print(len(val_set))
    for i in range(len(val_set)):

        subprocess.call('mkdir tmpv11', shell=True)
        for j in range(8):
            img_path = './' + val_set[i][j].replace('\\', '/')

            subprocess.call('cp ' + img_path + ' tmpv11/' +
                            'image_{:05d}.jpg'.format(j + 1),
                            shell=True)

        result = classify_video('tmpv11', str(i), class_names, model, opt)
        outputs = []
        for kk in range(7):
            outputs.append(result['clips'][kk]['features'])
        file_nm = './resnext101-64f/v_video' + str(i + 1) + '.npy'
        np.save(file_nm, outputs)
        subprocess.call('rm -rf tmpv11', shell=True)
        print(file_nm)

    if os.path.exists('tmpv11'):
        subprocess.call('rm -rf tmpv11', shell=True)

    # with open(opt.output, 'w') as f:
    #     json.dump(outputs, f)
コード例 #9
0
def extract_feats(opt, model):
    print('loading model {}'.format(opt.model))
    model_data = torch.load(opt.model)
    assert opt.arch == model_data['arch']
    model.load_state_dict(model_data['state_dict'])
    model.eval()
    if opt.verbose:
        print(model)

    dir_fc = opt.output_dir
    if not os.path.isdir(dir_fc):
        os.mkdir(dir_fc)

    class_names = []
    with open('class_names_list') as f:
        for row in f:
            class_names.append(row[:-1])

    ffmpeg_loglevel = 'quiet'
    if opt.verbose:
        ffmpeg_loglevel = 'info'

    if os.path.exists('tmp'):
        subprocess.call('rm -rf tmp', shell=True)

    video_list = glob.glob(os.path.join(opt.video_root, '*.mp4'))
    for video in tqdm(video_list):
        video_id = video.split("/")[-1].split(".")[0]
        outfile = os.path.join(dir_fc, video_id + '.npy')

        if os.path.exists(outfile):
            continue

        with open(os.devnull, "w") as ffmpeg_log:
            if os.path.exists(video):
                # print(video)
                subprocess.call('mkdir tmp',
                                stdout=ffmpeg_log,
                                stderr=ffmpeg_log,
                                shell=True)
                subprocess.call(
                    'ffmpeg -i {} tmp/image_%05d.jpg'.format(video),
                    stdout=ffmpeg_log,
                    stderr=ffmpeg_log,
                    shell=True)

                result = classify_video('tmp', video, class_names, model, opt)
                # print(type(result))
                if result is None:
                    print("result for video[ {} ]is empty".format(video_id))
                else:
                    np.save(outfile, result)

                subprocess.call('rm -rf tmp',
                                stdout=ffmpeg_log,
                                stderr=ffmpeg_log,
                                shell=True)
            else:
                print('{} does not exist'.format(video))

    if os.path.exists('tmp'):
        subprocess.call('rm -rf tmp',
                        stdout=ffmpeg_log,
                        stderr=ffmpeg_log,
                        shell=True)
コード例 #10
0
    model_data = torch.load(opt.model)
    assert opt.arch == model_data['arch']
    model.load_state_dict(model_data['state_dict'])
    model.eval()

    video_paths = []
    for k, v in annotations['database'].items():
        if v['subset'] == 'testing':
            if 'video_path' in v.keys():
                video_path = v['video_path'].split('/')
                video_path = os.path.join(opt.video_path, *video_path[-3:])
            else:
                video_path = os.path.join(opt.video_path, 'jpg',
                                          v['annotations']['label'], k)
            video_paths.append(video_path)

    outputs = []
    for video_path in video_paths:
        video_name = os.path.basename(video_path)
        if not os.path.exists(video_path):
            print(f'{video_name} not found')
            continue

        print(video_name)
        result = classify_video(video_path, video_name, class_names, model,
                                opt)
        outputs.append(result)

    with open(opt.output, 'w') as f:
        json.dump(outputs, f)
コード例 #11
0
    X_train, y_train = get_data_by_labels(labels,
                                          train,
                                          method="dense",
                                          sub=True)

    # TODO: Train de model
    model = SimpleNeuralNetwork.SimpleNeuralNetwork(
        [X_train.shape[1], 15, 5, 3], 3, False, 5000, 1e-4, True)
    y = np.zeros((len(y_train), 3))
    for k in range(len(y_train)):
        y[k, (int)(y_train[k])] = 1
    model.train(X_train, y)

    # Classify all frames of a video
    index = np.random.randint(len(test))
    classify.classify_video(model, labels, test[index], "testEventW2.c3d",
                            "dense")

# Visualize data for separability
if (to_execute == 3):
    # Displays graphics with classified position of labels in (Y,Z) dimensions
    (train, test) = split_train_test_files("data/")
    print("Processing the data...")
    show_graphic('LTOE', train)
    show_graphic('RTOE', train)
    show_graphic('T10', train)
    show_graphic('RANK', train)
    plt.show()

# Substract x from a file and visualize it
if (to_execute == 4):
    sub_x_from_file('CP_GMFCS1_01916_20130128_18.c3d', 'file_with_sub_x.c3d')