Crowd11 Preprocessing
Crowd11은 여러가지 길이와 크기를 가진 데이터셋들의 집합이다. 이를 사용하기 위해선 일정한 규격으로 클립을 생성하는 전처리 과정이 필요하다.
다음은 preprocessing.py의 코드다.
# import numpy as np
import skvideo.io
# import cv2
import os
import subprocess
import csv
from collections import namedtuple
Clip = namedtuple('Clip', 'video_name label start_frame end_frame width height left_distance top_distance dataset scene_number crop_number')
INPUT_FOLDER = "./VOI/"
OUTPUT_FOLDER = "./Crowd-11/"
if not os.path.exists(OUTPUT_FOLDER):
os.makedirs(OUTPUT_FOLDER)
def read_line(line):
# print(line)
video_name=line[0]
label=line[1]
start_frame=int(line[2])
end_frame=int(line[3])
left_distance=float(line[4])
top_distance=float(line[5])
width=float(line[6])
height=float(line[7])
dataset=line[8]
scene_number=line[9]
crop_number=str(int(line[10]))
clip=Clip(video_name, label, start_frame, end_frame, width, height, left_distance, top_distance, dataset, scene_number, crop_number)
return clip
def create_clips(clip):
input_video_folder = INPUT_FOLDER + clip.dataset+'/'
if not os.path.exists(input_video_folder + clip.video_name):
return
print(input_video_folder + clip.video_name)
metadata = skvideo.io.ffprobe(input_video_folder+clip.video_name)
if 'video' not in metadata.keys():
return
real_width = int(metadata['video']['@width'])
real_height = int(metadata['video']['@height'])
crop_width = clip.width * real_width
crop_height = clip.width * real_height
crop_left_distance = clip.left_distance * real_width
crop_top_distance = clip.top_distance * real_height
video = OUTPUT_FOLDER + clip.label + '_' + clip.scene_number + '_' + clip.crop_number + '_' + clip.video_name
if os.path.exists(video):
return
if crop_height<224:
print("...............too small: rescaling")
subprocess.call('avconv -i %s -an -filter_complex "select=between(n\,%s\,%s),setpts=PTS-STARTPTS, crop=%s:%s:%s:%s, scale=-2:224" %s'%(
input_video_folder+clip.video_name,
clip.start_frame,
clip.end_frame,
crop_width,
crop_height,
crop_left_distance,
crop_top_distance,
video),
shell=True)
else:
subprocess.call('avconv -i %s -an -filter_complex "select=between(n\,%s\,%s),setpts=PTS-STARTPTS, crop=%s:%s:%s:%s" %s'%(
input_video_folder+clip.video_name,
clip.start_frame,
clip.end_frame,
crop_width,
crop_height,
crop_left_distance,
crop_top_distance,
video),
shell=True)
def main():
file = 'preprocessing.csv'
list_clips=[]
with open(file, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=';', quotechar='|')
for line in reader:
clip = read_line(line)
list_clips.append(clip)
for clip in list_clips:
create_clips(clip)
if __name__ == "__main__":
main()
먼저 라이브러리 install과 virtualenv 실행 코드는 README.md에 있다.
You need to have avconv installed:
```
sudo apt-get install avconv
```
Then, you need to install several python package. A virtualeenv installation is recommended:
```
virtualenv -p python3 py
source py/bin/activate
pip install sk-video
```
Execution (in the virtualenv):
```
python script_formating.py
```
이 코드는 리눅스 기준이며, 설치 라이브러리 이름이 바뀐 것이 있다.
avconv는 libav-tools로 설치한다.
sk-video는 scikit-video로 설치한다..
script_formatting.py가 아닌 preprocessing.py를 실행시켜야 한다.
꼭 virtualenv에서 실행할 필요는 없다. sk-video와 avconv(혹은 ffmpeg)만 pip install 하면 된다.
라이브러리를 모두 설치한 뒤 실행을 하려 하면 몇가지 오류가 생길 수 있다.
ffmpeg의 경로를 찾을 수 없다면, preprocessong.py의 맨 위에 다음 코드를 추가한다.
import skvideo
skvideo.setFFmpegPath("C:/FFmpeg/bin")
위 경로는 ffmpeg가 설치된 경로이다. ffmpeg는 다음 링크에서 직접 설치하는것이 좋다.
https://ffmpeg.org/download.html
Download FFmpeg
If you find FFmpeg useful, you are welcome to contribute by donating. More downloading options Git Repositories Since FFmpeg is developed with Git, multiple repositories from developers and groups of developers are available. Release Verification All FFmpe
ffmpeg.org
avconv 관련 오류가 생긴다면. 코드를 다음과 같이 수정해본다.
subprocess.call('ffmpeg -i %s -an -filter_complex "select=between(n\,%s\,%s),setpts=PTS-STARTPTS, crop=%s:%s:%s:%s" %s'%(
input_video_folder+clip.video_name,
clip.start_frame,
clip.end_frame,
crop_width,
crop_height,
crop_left_distance,
crop_top_distance,
video),
shell=True)
subprocess.call 함수를 사용하는 부분이 두 군데 있는데, 이 안의 avconv를 ffmpeg로 변경한것이다.
전처리가 완료되면 Crowd-11 폴더가 생성되고, 클립으로 나뉜 동영상 파일들이 들어있는 것을 확인할 수 있다.
참조
Crowd-11: A Dataset for Fine Grained Crowd Behaviour Analysis Camille Dupont∗ Luis Tob´ıas∗ Bertrand Luvison CEA, LIST, Vision and Content Engineering Laboratory, Point Courrier 173, F-91191 Gif-sur-Yvette, France