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, offset: int = 3, 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 offset: it will change the way to decided whether two ranges can be merged before: first_range.end == second_range.start after: first_range.end + offset >= secord_range.start :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, offset=offset, ) 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.draw( classify_result, report_path=os.path.join(data_home, 'report.html'), cut_result=res, # kwargs of get_range # otherwise these thumbnails may become different threshold=threshold, limit=limit, offset=offset, )
def test_result(): cl = SVMClassifier() cl.load_model(MODEL_PATH) stable, _ = cutter_res.get_range() classify_result = cl.classify(VIDEO_PATH, stable, keep_data=True) assert classify_result.to_dict() classify_result.mark_range(1, 3, "0") classify_result.mark_range_unstable(1, 3) classify_result.get_important_frame_list()
def test_result(): cl = SVMClassifier() cl.load_model(MODEL_PATH) stable, _ = cutter_res.get_range() classify_result = cl.classify(VIDEO_PATH, stable, keep_data=True) assert classify_result.to_dict() classify_result.mark_range(1, 3, "0") classify_result.mark_range_unstable(1, 3) classify_result.get_important_frame_list() classify_result.get_stage_range() classify_result.get_specific_stage_range("0") classify_result.get_not_stable_stage_range() assert classify_result.first("1").frame_id == 20 assert classify_result.last("1").frame_id == 21
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, )
def calculate_result(_cl, _SVM_or_Keras, _param, _from_movie_2_picture, _model_file, _video_path_for_forecast): if _SVM_or_Keras == '1\n': print('使用SVM进行预测') cl = SVMClassifier( # 默认情况下使用 HoG 进行特征提取。你可以将其关闭从而直接对原始图片进行训练与测试:feature_type='raw' feature_type="hog", # 默认为0.2,即将图片缩放为0.2倍。主要为了提高计算效率,如果你担心影响分析效果,可以将其提高 compress_rate=_param[0], ) # 加载待训练数据 cl.load_model(_model_file) elif _SVM_or_Keras == '2\n': # 分析视频_聚类 print('使用KerasClassifier进行预测') cl = KerasClassifier( compress_rate=_param[0], # 在使用时需要保证数据集格式统一(与训练集)。因为 train_model.py 用了 600x800,所以这里设定成一样的 # target_size=(600, 800), ) cl.load_model(_model_file) # 开始预测 _forecast_result = [] # 获取forecast文件夹的mp4文件列表 forecast_video_list = public_fun.get_mp4file_name(_video_path_for_forecast) for i in forecast_video_list: # 分析视频_切割视频 stable = get_data.get_range('forecast', i, _param, _from_movie_2_picture) classify_result = cl.classify(i, stable, keep_data=True) result_dict = classify_result.to_dict() _forecast_result.append( public_fun.write_result_to_local(i, _from_movie_2_picture, result_dict, classify_result)) # _forecast_result = [['5.mp4', '3.161888888888889', '1', # '4.155888888888889', '1', '8.040888888888889', '1']] return _forecast_result
def handle(self, video_path: str) -> bool: super(NormalHandler, self).handle(video_path) video = VideoObject(video_path) if self.preload: video.load_frames() # --- cutter --- cutter = VideoCutter() res = cutter.cut(video) stable, unstable = res.get_range(threshold=0.98, offset=3) data_home = res.pick_and_save(stable, self.frame_count, to_dir=self.result_path) # --- classify --- cl = SVMClassifier() cl.load(data_home) cl.train() self.classifier_result = cl.classify(video, stable) # --- draw --- r = Reporter() r.draw(self.classifier_result, report_path=self.result_report_path) return True
from stagesepx.classifier import SVMClassifier from stagesepx.cutter import VideoCutter from stagesepx.reporter import Reporter TARGET_VIDEO = '../../demo.mp4' # cut # 这里依旧使用了 cut,主要目的还是为了可以比较好的处理变化中的过程 # 但这次我们不需要用到 pick_and_save,因为这次 classifier 不会使用 cutter 的数据 cutter = VideoCutter() res = cutter.cut(TARGET_VIDEO) stable, _ = res.get_range() # classify # 这里的参数需要保持与train.py一致,如果你有改动的话 cl = SVMClassifier() cl.load_model('./model.pkl') classify_result = cl.classify( TARGET_VIDEO, stable, ) r = Reporter() r.draw( classify_result, report_path='report.html', cut_result=res, )