logo
Browse Source

update select

Signed-off-by: Kaiyuan Hu <kaiyuan.hu@zilliz.com>
main
Kaiyuan Hu 2 years ago
parent
commit
b79ab602d5
  1. 28
      select_video.py

28
select_video.py

@ -25,18 +25,13 @@ class SelectVideo:
self.reduce_function = reduce_function self.reduce_function = reduce_function
self.reverse = reverse self.reverse = reverse
def __call__(self, video_urls: List[str], scores: List[float]) -> List[str]:
"""
args:
video_urls
scores
return:
video_list
"""
def _select(self, video_urls, scores):
assert len(video_urls) == len(scores), 'len(video_urls) must equal len(scores)' assert len(video_urls) == len(scores), 'len(video_urls) must equal len(scores)'
video2score_list = [{'video_url': video_url, 'score': score} for video_url, score in zip(video_urls, scores)] video2score_list = [{'video_url': video_url, 'score': score} for video_url, score in zip(video_urls, scores)]
video2score_list.sort(key=lambda d: d['video_url']) video2score_list.sort(key=lambda d: d['video_url'])
reduced_dict = {} reduced_dict = {}
for video_url, items in groupby(video2score_list, key=lambda d: d['video_url']): for video_url, items in groupby(video2score_list, key=lambda d: d['video_url']):
video_score_list = [item['score'] for item in items] video_score_list = [item['score'] for item in items]
if self.reduce_function == 'sum': if self.reduce_function == 'sum':
@ -50,10 +45,27 @@ class SelectVideo:
else: else:
raise Exception('unknown reduce_function') raise Exception('unknown reduce_function')
reduced_dict[video_url] = reduced_value reduced_dict[video_url] = reduced_value
sorted_video_urls = [k for k, v in sorted(reduced_dict.items(), key=lambda item: item[1], reverse=self.reverse)] sorted_video_urls = [k for k, v in sorted(reduced_dict.items(), key=lambda item: item[1], reverse=self.reverse)]
top_k = min(self.top_k, len(sorted_video_urls)) top_k = min(self.top_k, len(sorted_video_urls))
return sorted_video_urls[:top_k] return sorted_video_urls[:top_k]
def __call__(self, video_urls: List[str], scores: List[float]) -> List[str]:
"""
args:
video_urls
scores
return:
video_list
"""
if isinstance(video_urls[0], list):
video_urls = sum(video_urls, [])
scores = sum(scores, [])
if isinstance(video_urls[0], str):
return self._select(video_urls, scores)
# if __name__ == '__main__': # if __name__ == '__main__':
# op = SelectVideo(top_k=2, reverse=True, reduce_function='min') # op = SelectVideo(top_k=2, reverse=True, reduce_function='min')
# res = op(['a', 'a', 'c', 'a', 'b', 'b', 'c', 'c'], # res = op(['a', 'a', 'c', 'a', 'b', 'b', 'c', 'c'],

Loading…
Cancel
Save