def classify(self, video_path: str, data_home: str, output_path: str = None, compress_rate: float = 0.2, limit: int = None): # TODO model? cut_result_json = os.path.join(data_home, 'cut_result.json') res = None stable = None if os.path.isfile(cut_result_json): res = VideoCutResult.load(cut_result_json) stable, _ = res.get_range(limit=limit) cl = SVMClassifier(compress_rate=compress_rate) cl.load(data_home) cl.train() classify_result = cl.classify(video_path, stable) # --- draw --- r = Reporter() r.add_dir_link(data_home) r.draw( classify_result, report_path=os.path.join(output_path or data_home, 'report.html'), cut_result=res, )
def one_step(self, video_path: str, output_path: str = None, threshold: float = 0.95, frame_count: int = 5, compress_rate: float = 0.2, limit: int = None): """ one step => cut, classifier, draw :param video_path: your video path :param output_path: output path (dir) :param threshold: float, 0-1, default to 0.95. decided whether a range is stable. larger => more unstable ranges :param frame_count: default to 5, and finally you will get 5 frames for each range :param compress_rate: before_pic * compress_rate = after_pic. default to 0.2 :param limit: ignore some ranges which are too short, 5 means ignore stable ranges which length < 5 :return: """ # --- cutter --- cutter = VideoCutter() res = cutter.cut(video_path, compress_rate=compress_rate) stable, unstable = res.get_range( threshold=threshold, limit=limit, ) data_home = res.pick_and_save(stable, frame_count, to_dir=output_path) res_json_path = os.path.join(data_home, 'cut_result.json') res.dump(res_json_path) # --- classify --- cl = SVMClassifier(compress_rate=compress_rate) cl.load(data_home) cl.train() classify_result = cl.classify(video_path, stable) # --- draw --- r = Reporter() r.add_dir_link(data_home) r.draw( classify_result, report_path=os.path.join(data_home, 'report.html'), cut_result=res, )
# 在加载数据完成之后需要先训练 cl.train() # # 在训练后你可以把模型保存起来 # cl.save_model('model.pkl') # # 或者直接读取已经训练好的模型 # cl.load_model('model.pkl') # 开始分类 res = cl.classify( '../test.mp4', # 步长,可以自行设置用于平衡效率与颗粒度 # 默认为1,即每帧都检测 step=1, ) # 为了更方便的可读性,stagesepx已经内置了图表绘制功能 # 你可以直接把分析结果绘制成图表 report = Reporter() # 你可以将把一些文件夹路径插入到报告中 # 这样你可以很方便地从报告中查看各项相关内容 # 当然,你需要想好这些路径与报告最后所在位置之间的相对位置,以确保他们能够被访问到 report.add_dir_link(data_home) report.draw( res, report_path='report.html', )
data_home = res.pick_and_save(stable, 3) # classify cl = SVMClassifier() cl.load(data_home) cl.train() # 注意,如果在classify方法指定了范围 # 那么分析时只会分析处于范围内的帧! # 例如,这里只传入了stable的范围,那么非stable范围内的帧都会被忽略掉,标记为 -1 classify_result = cl.classify( video_path, stable, # 步长,可以自行设置用于平衡效率与颗粒度 # 默认为1,即每帧都检测 step=1) # draw r = Reporter() r.add_dir_link(data_home) # 你可以将 thumbnail 直接嵌入到report中 for each in unstable: r.add_thumbnail( f'{each.start}({each.start_time}) - {each.end}({each.end_time})', res.thumbnail(each)) # 在0.3.2及之后的版本,你可以在报告中加入一些自定义内容 (https://github.com/williamfzc/stagesepx/issues/13) # r.add_extra('here is title', 'here is content') r.draw(classify_result)