Skip to content

Commit

Permalink
feat: support 'step' in classify
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfzc committed Jul 19, 2019
1 parent 0ca7e9b commit d828b40
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
7 changes: 6 additions & 1 deletion example/classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
# 如果你改动了,这里也要做相应修改
cl.load('./cut_result')
# 开始分析即可
res = cl.classify('../test.mp4')
res = cl.classify(
'../test.mp4',
# 步长,可以自行设置用于平衡效率与颗粒度
# 默认为1,即每帧都检测
step=1
)

# 分类出来的结果是一个 list,里面包含 ClassifierResult 对象
# 你可以用它进行二次开发
Expand Down
4 changes: 4 additions & 0 deletions stagesepx/classifier/ssim.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ class SSIMClassifier(BaseClassifier):
def classify(self,
video_path: str,
limit_range: typing.List[VideoCutRange] = None,
step: int = None,
threshold: float = None) -> typing.List[ClassifierResult]:
logger.debug(f'classify with {self.__class__.__name__}')
assert self.data, 'should load data first'

if not threshold:
threshold = 0.85
if not step:
step = 1

final_result: typing.List[ClassifierResult] = list()
with toolbox.video_capture(video_path) as cap:
Expand Down Expand Up @@ -52,5 +55,6 @@ def classify(self,
logger.debug(f'frame {frame_id} ({frame_timestamp}) belongs to {result[0]}')

final_result.append(ClassifierResult(video_path, frame_id, frame_timestamp, result[0]))
toolbox.video_jump(cap, frame_id + step - 1)
ret, frame = cap.read()
return final_result
7 changes: 6 additions & 1 deletion stagesepx/classifier/svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,15 @@ def predict_with_object(self, pic_object: np.ndarray) -> str:

def classify(self,
video_path: str,
limit_range: typing.List[VideoCutRange] = None) -> typing.List[ClassifierResult]:
limit_range: typing.List[VideoCutRange] = None,
step: int = None) -> typing.List[ClassifierResult]:
logger.debug(f'classify with {self.__class__.__name__}')
assert self.data, 'should load data first'
assert self._model, 'should train before classify'

if not step:
step = 1

final_result: typing.List[ClassifierResult] = list()
with toolbox.video_capture(video_path) as cap:
ret, frame = cap.read()
Expand All @@ -108,5 +112,6 @@ def classify(self,
result = self.predict_with_object(frame)
logger.debug(f'frame {frame_id} ({frame_timestamp}) belongs to {result}')
final_result.append(ClassifierResult(video_path, frame_id, frame_timestamp, result))
toolbox.video_jump(cap, frame_id + step - 1)
ret, frame = cap.read()
return final_result

0 comments on commit d828b40

Please sign in to comment.