From 898384fc756de58154d121ed3afa0b90718ed53e Mon Sep 17 00:00:00 2001 From: ChengZi Date: Wed, 9 Nov 2022 19:40:44 +0800 Subject: [PATCH] filter_tiny_segments --- README.md | 71 +++++++++++++++++++++++++++++++++++++++- __init__.py | 19 +++++++++++ filter_tiny_segments.py | 32 ++++++++++++++++++ result.png | Bin 0 -> 11094 bytes 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 __init__.py create mode 100644 filter_tiny_segments.py create mode 100644 result.png 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 0000000000000000000000000000000000000000..ab39c6072c71d8344c221deff0f168b0604abe81 GIT binary patch literal 11094 zcmdsdc{r5+`)`yMLMkdkDG4Ea$Wo%LBijf=k?d3q!p!)nRD>u^vaf@&4TF%GqzEy# zv5aMueXL;yGmP^LpU?02{r%2zUFV$ZT<2Wp`NO>Dndf;w@8^Es_v?P$uiGm#)0^C! zf}DHy?BO=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