diff --git a/README.md b/README.md index 0871e60..42a6b22 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,71 @@ -# filter-tiny-segments +# Filter Tiny Segments +*author: Chen Zhang* + + +
+ + + +## Description + +This operator can filter tiny detected segments with format of list of `[start_second_1, start_second_2, end_second_1, end_second_2]` +
+ + +## Code Example + + +```python +import towhee +towhee.dc['pred']([[[0, 0, 100, 100], [0, 0, 10, 10], [0, 0, 60, 10]]]) \ + .video_copy_detection.filter_tiny_segments['pred', 'filtered_pred'](filter_s_thresh=20) \ + .show() +``` + +![](result.png) + + + +## Factory Constructor + +Create the operator via the following factory method + +***filter_tiny_segments(filter_s_thresh, segment_len_rate)*** + + +**Parameters:** + +​ ***filter_s_thresh:*** *float* + +​ Use a thresh to filter detected box which is smaller than it. + +​ ***segment_len_rate:*** *float* + +​ Filter expect longer then segment_len_rate * video length. Only useful for filter expect near video length segments. + +
+ + + +## Interface + +A Temporal Network operator takes two numpy.ndarray(shape(N,D) N: number of features. D: dimension of features) and get the duplicated ranges and scores. + + +**Parameters:** + +​ ***pred_value:*** *List* + +​ List of predicted segment second infos of a video pair + +​ ***sim_hw:*** *Tuple* + +​ Similarity matrix height and weight of a video pair. If sample rate is 1s, sim_hw is also the lengths of these videos. + + +**Returns:** + +​ ***res_pred_list:*** *List* + +​ List of filtered predicted segment second infos diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..adea38d --- /dev/null +++ b/__init__.py @@ -0,0 +1,19 @@ +# Copyright 2022 Zilliz. All rights reserved. +# +# 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. + +from .filter_tiny_segments import FilterTinySegments + + +def filter_tiny_segments(filter_s_thresh: float, segment_len_rate: float): + return FilterTinySegments(filter_s_thresh, segment_len_rate) \ No newline at end of file diff --git a/filter_tiny_segments.py b/filter_tiny_segments.py new file mode 100644 index 0000000..371f9d4 --- /dev/null +++ b/filter_tiny_segments.py @@ -0,0 +1,32 @@ +from typing import List, Tuple, Optional +from towhee.operator.base import Operator +from towhee import register + + +@register(output_schema=['vec']) +class FilterTinySegments(Operator): + """ + FilterTinySegments + """ + + def __init__(self, filter_s_thresh: float, segment_len_rate: float = 0.8): + super().__init__() + self.filter_s_thresh = filter_s_thresh + self.segment_len_rate = segment_len_rate + + def __call__(self, pred_value: List[Optional[List]], sim_hw: Tuple = None) -> List: + res_pred_list = [] + for time_info in pred_value: + filterable = True + if ((time_info[2] - time_info[0]) + (time_info[3] - time_info[1])) / 2.0 > self.filter_s_thresh: + filterable = False + if sim_hw is not None: + sim_h, sim_w = sim_hw + if (time_info[2] - time_info[0]) > sim_h * self.segment_len_rate or ( + time_info[3] - time_info[1]) > sim_w * self.segment_len_rate: + filterable = False + if not filterable: + res_pred_list.append(time_info) + return res_pred_list + + diff --git a/result.png b/result.png new file mode 100644 index 0000000..ab39c60 Binary files /dev/null and b/result.png differ