def handle(self, video_path: str) -> bool: super(KerasHandler, 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) # --- classify --- cl = KerasClassifier() if self.model_path: logger.info("load existed pre-train model") cl.load_model(self.model_path) else: data_home = res.pick_and_save(stable, self.frame_count, to_dir=self.result_path) cl.train(data_home) self.classifier_result = cl.classify(video, stable) # --- draw --- r = Reporter() r.draw(self.classifier_result, report_path=self.result_report_path) return True
def test_keras(): # set epochs to 1 for quickly training (test only) cl = KerasClassifier(epochs=1) cl.train(CUTTER_RESULT_DIR) cl.save_model("haha.h5") # recreate cl = KerasClassifier() cl.load_model("haha.h5") stable, _ = cutter_res.get_range() classify_result = cl.classify(VIDEO_PATH, stable, keep_data=True) assert classify_result.to_dict()
''' * @Description : 预测 * @Autor : Tommy * @Date : 2021-02-21 19:07:26 * @LastEditors : Tommy * @LastEditTime : 2021-03-15 12:15:48 ''' from stagesepx.classifier.keras import KerasClassifier from stagesepx.cutter import VideoCutter from stagesepx.video import VideoObject from stagesepx.reporter import Reporter # 使用Keras方法进行预测 cl = KerasClassifier() cl.load_model('./Color_finally.h5') # 将视频切分成帧 file_name = './video/TapColor_5.mp4' video = VideoObject(file_name) # 新建帧,计算视频总共多少帧,每帧多少ms video.load_frames() # 压缩视频 cutter = VideoCutter() # 计算每一帧视频的每一个block的ssim和psnr res = cutter.cut(video) # 判断A帧到B帧之间是否稳定还是不稳定 stable, unstable = res.get_range()
from stagesepx.cutter import VideoCutter from stagesepx.classifier.keras import KerasClassifier from stagesepx.reporter import Reporter from stagesepx.video import VideoObject video_path = "../videos/douyin1.mp4" video = VideoObject(video_path) video.load_frames() # --- cutter --- cutter = VideoCutter() res = cutter.cut(video) stable, unstable = res.get_range() # --- classifier --- cl = KerasClassifier( # 在使用时需要保证数据集格式统一(与训练集) # 因为 train_model.py 用了 600x800,所以这里设定成一样的 target_size=(600, 800), ) model_file = "./keras_model.h5" cl.load_model(model_file) classify_result = cl.classify(video, stable, keep_data=True) result_dict = classify_result.to_dict() # --- draw --- r = Reporter() r.draw(classify_result)
# 把分好类的稳定阶段的图片存本地 res.pick_and_save(stable, 100, to_dir='./picture/train_stable_frame', meaningful_name=True) # 训练模型文件 cl = KerasClassifier( # 训练轮数 epochs=10) cl.train('./train_stable_frame') cl.save_model('./model.h5', overwrite=True) # 使用Keras方法进行预测 cl = KerasClassifier() cl.load_model('./model.h5') # 将视频切分成帧 file_name = './video_for_forecast.mp4' # 预加载,大幅度提升分析速度 video = VideoObject(file_name, pre_load=True) # 新建帧,计算视频总共有多少帧,每帧多少ms video.load_frames() # 压缩视频 cutter = VideoCutter() # 这个hook是干什么的,后续做解释 hook = IgnoreHook( size=(0.05, 1), overwrite=True, ) cutter.add_hook(hook)