Skip to content

Commit

Permalink
test cases added for decord reader
Browse files Browse the repository at this point in the history
  • Loading branch information
Shashank authored and Shashank committed Feb 18, 2023
1 parent 029f34c commit 8b2b129
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 3 deletions.
1 change: 0 additions & 1 deletion eva/parser/lark_visitor/_table_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def table_source_item_with_sample(self, tree):
sample_freq = self.visit(child)
elif child.data == "sample_clause_with_type":
sample_freq, sample_type = self.visit(child)
print(sample_freq, sample_type)
elif child.data == "alias_clause":
alias = self.visit(child)

Expand Down
5 changes: 3 additions & 2 deletions eva/readers/decord_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import cv2
import decord
import math

from eva.constants import IFRAMES
from eva.expression.abstract_expression import AbstractExpression
Expand All @@ -41,6 +42,7 @@ def __init__(
can be converted to ranges. Defaults to None.
sampling_rate (int, optional): Set if the caller wants one frame
every `sampling_rate` number of frames. For example, if `sampling_rate = 10`, it returns every 10th frame. If both `predicate` and `sampling_rate` are specified, `sampling_rate` is given precedence.
sampling_type (str, optional): Set as 'iframe' if caller want to sample on top on iframes only.
"""
self._predicate = predicate
self._sampling_rate = sampling_rate or 1
Expand All @@ -60,7 +62,6 @@ def _read(self) -> Iterator[Dict]:

if self._sampling_type == IFRAMES:
iframes = video.get_key_indices()
print(iframes)
idx = 0
for (begin, end) in range_list:
while idx < len(iframes) and iframes[idx] < begin:
Expand All @@ -83,7 +84,7 @@ def _read(self) -> Iterator[Dict]:
yield {
"id": frame_id,
"data": frame,
"seconds": frame_id // video.get(cv2.CAP_PROP_FPS),
"seconds": math.floor(video.get_frame_timestamp(frame_id)[0]),
}
else:
break
Expand Down
11 changes: 11 additions & 0 deletions test/integration_tests/test_select_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ def test_select_and_sample(self):
self.assertEqual(len(actual_batch), len(expected_batch[0]))
self.assertEqual(actual_batch, expected_batch[0])

def test_select_and_iframe_sample(self):
select_query = "SELECT id FROM MyVideo SAMPLE 7 'iframe' ORDER BY id;"
actual_batch = execute_query_fetch_all(select_query)
actual_batch.sort()

expected_batch = list(create_dummy_batches(filters=range(0, NUM_FRAMES, 7)))
expected_batch[0] = expected_batch[0].project(["myvideo.id"])

self.assertEqual(len(actual_batch), len(expected_batch[0]))
self.assertEqual(actual_batch, expected_batch[0])

def test_select_and_groupby_first(self):
# groupby and orderby together not tested because groupby
# only applies to video data which is already sorted
Expand Down
103 changes: 103 additions & 0 deletions test/readers/test_decord_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# coding=utf-8
# Copyright 2018-2022 EVA
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import unittest
from test.util import (
FRAME_SIZE,
NUM_FRAMES,
create_dummy_batches,
create_sample_video,
file_remove,
upload_dir_from_config,
)

from eva.expression.abstract_expression import ExpressionType
from eva.expression.comparison_expression import ComparisonExpression
from eva.expression.constant_value_expression import ConstantValueExpression
from eva.expression.logical_expression import LogicalExpression
from eva.expression.tuple_value_expression import TupleValueExpression
from eva.readers.decord_reader import DecordReader


class DecordLoaderTest(unittest.TestCase):
def setUp(self):
create_sample_video()

def tearDown(self):
file_remove("dummy.avi")

def test_should_sample_every_k_frame(self):
for k in range(1, 10):
video_loader = DecordReader(
file_url=os.path.join(upload_dir_from_config, "dummy.avi"),
batch_mem_size=FRAME_SIZE * NUM_FRAMES,
sampling_rate=k,
)
batches = list(video_loader.read())
expected = list(
create_dummy_batches(filters=[i for i in range(0, NUM_FRAMES, k)])
)
self.assertTrue(batches, expected)

def test_should_sample_every_k_frame_with_predicate(self):
col = TupleValueExpression("id")
val = ConstantValueExpression(NUM_FRAMES // 2)
predicate = ComparisonExpression(
ExpressionType.COMPARE_GEQ, left=col, right=val
)
for k in range(2, 4):
video_loader = DecordReader(
file_url=os.path.join(upload_dir_from_config, "dummy.avi"),
batch_mem_size=FRAME_SIZE * NUM_FRAMES,
sampling_rate=k,
predicate=predicate,
)
batches = list(video_loader.read())
for batch in batches:
print(batch)
value = NUM_FRAMES // 2
start = value + k - (value % k) if value % k else value
expected = list(
create_dummy_batches(filters=[i for i in range(start, NUM_FRAMES, k)])
)
self.assertTrue(batches, expected)

value = 2
predicate_1 = ComparisonExpression(
ExpressionType.COMPARE_GEQ,
left=TupleValueExpression("id"),
right=ConstantValueExpression(value),
)
predicate_2 = ComparisonExpression(
ExpressionType.COMPARE_LEQ,
left=TupleValueExpression("id"),
right=ConstantValueExpression(8),
)
predicate = LogicalExpression(
ExpressionType.LOGICAL_AND, predicate_1, predicate_2
)
for k in range(2, 4):
video_loader = DecordReader(
file_url=os.path.join(upload_dir_from_config, "dummy.avi"),
batch_mem_size=FRAME_SIZE * NUM_FRAMES,
sampling_rate=k,
predicate=predicate,
)
batches = list(video_loader.read())
start = value + k - (value % k) if value % k else value
expected = list(
create_dummy_batches(filters=[i for i in range(start, 8, k)])
)
self.assertTrue(batches, expected)

0 comments on commit 8b2b129

Please sign in to comment.