-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preprocessing videos: extracting RGB images + flow data #2
Comments
Hi, Frederik Schorr: A. I have use two ways to extract flow
pip install opencv-python Then, you can use the TVL1 api to extract optical flow (Note: opencv-python cannot use GPU to compute flow. You can use multi-processing) B. There are some ways to extract rgb. Finally, we have create a new repo about i3d. You can try it. |
import os
import numpy as np
import cv2
from glob import glob
from multiprocessing import Pool
_IMAGE_SIZE = 256
def cal_for_frames(video_path):
frames = glob(os.path.join(video_path, '*.jpg'))
frames.sort()
flow = []
prev = cv2.imread(frames[0])
prev = cv2.cvtColor(prev, cv2.COLOR_BGR2GRAY)
for i, frame_curr in enumerate(frames):
curr = cv2.imread(frame_curr)
curr = cv2.cvtColor(curr, cv2.COLOR_BGR2GRAY)
tmp_flow = compute_TVL1(prev, curr)
flow.append(tmp_flow)
prev = curr
return flow
def compute_TVL1(prev, curr, bound=15):
"""Compute the TV-L1 optical flow."""
TVL1 = cv2.DualTVL1OpticalFlow_create()
flow = TVL1.calc(prev, curr, None)
assert flow.dtype == np.float32
flow = (flow + bound) * (255.0 / (2*bound))
flow = np.round(flow).astype(int)
flow[flow >= 255] = 255
flow[flow <= 0] = 0
return flow
def save_flow(video_flows, flow_path):
for i, flow in enumerate(video_flows):
cv2.imwrite(os.path.join(flow_path.format('u'), "{:06d}.jpg".format(i)),
flow[:, :, 0])
cv2.imwrite(os.path.join(flow_path.format('v'), "{:06d}.jpg".format(i)),
flow[:, :, 1])
def gen_video_path():
path = []
flow_path = []
length = []
base = ''
flow_base = ''
for task in ['train', 'dev', 'test']:
videos = os.listdir(os.path.join(base, task))
for video in videos:
tmp_path = os.path.join(base, task, video, '1')
tmp_flow = os.path.join(flow_base, task, '{:s}', video)
tmp_len = len(glob(os.path.join(tmp_path, '*.png')))
u = False
v = False
if os.path.exists(tmp_flow.format('u')):
if len(glob(os.path.join(tmp_flow.format('u'), '*.jpg'))) == tmp_len:
u = True
else:
os.makedirs(tmp_flow.format('u'))
if os.path.exists(tmp_flow.format('v')):
if len(glob(os.path.join(tmp_flow.format('v'), '*.jpg'))) == tmp_len:
v = True
else:
os.makedirs(tmp_flow.format('v'))
if u and v:
print('skip:' + tmp_flow)
continue
path.append(tmp_path)
flow_path.append(tmp_flow)
length.append(tmp_len)
return path, flow_path, length
def extract_flow(args):
video_path, flow_path = args
flow = cal_for_frames(video_path)
save_flow(flow, flow_path)
print('complete:' + flow_path)
return
if __name__ =='__main__':
pool = Pool(2) # multi-processing
video_paths, flow_paths, video_lengths = gen_video_path()
pool.map(extract_flow, zip(video_paths, flow_paths)) |
Thank you Rythmblue - this is exactly what I was looking for! I will re-use in a project on sign language recognition (for deaf-mute people). Thx! kind regards, Frederik |
but , TVL1 = cv2.DualTVL1OpticalFlow_create() ,do not work in the lastest opencv-python==0.4.0 |
|
|
Hi Rhythmblue, With finetuned model of HMDB51, the accuracy is as below I am trying to understand why i am getting such a low accuracy rate using HMDB51, and high accuracy rate with UCF101 flow data, Could you plz suggest me any directions to find out the same? Thanks, |
@VeeranjaneyuluThoka ,sorry for the late response. I was busy doing experiments and tried to catch the deadline for a conference. About your question, I think 0.99 of flow on UCF101 is an unexpected result. So, the code used for evaluation may have some problems. (Sometimes problem happens in the file list of video names and ground-truths. ) |
Hi @Rhythmblue , thank you for sharing the code to compute optical flow, could you explain more why you use the |
@yuanzhedong , I suggest that you can change bound to 20, following denseflow.
|
Thank you for the detailed answer, it's very helpful!! |
Thank you @Rhythmblue for great work!
I guess you preprocess the UCF videos upfront to extract RGB images + flow data. Can you share these as well? I am particularly interested in which tools you use to extract the flow data.
Thank you very much,
Frederik
The text was updated successfully, but these errors were encountered: