详细解释: import os import time import cv2 import imutils import shutil import img2pdf…
- User
详细解释: import os import time import cv2 import imutils import shutil import img2pdf import glob import argparse import gradio as gr
############# Define constants
OUTPUT_SLIDES_DIR = f"./output"
FRAME_RATE = 3 # no.of frames per second that needs to be processed, fewer the count faster the speed WARMUP = FRAME_RATE # initial number of frames to be skipped FGBG_HISTORY = FRAME_RATE * 15 # no.of frames in background object VAR_THRESHOLD = 16 # Threshold on the squared Mahalanobis distance between the pixel and the model to decide whether a pixel is well described by the background model. DETECT_SHADOWS = False # If true, the algorithm will detect shadows and mark them. MIN_PERCENT = 0.1 # min % of diff between foreground and background to detect if motion has stopped MAX_PERCENT = 3 # max % of diff between foreground and background to detect if frame is still in motion
def get_frames(video_path): '''A fucntion to return the frames from a video located at video_path this function skips frames as defined in FRAME_RATE'''
# open a pointer to the video file initialize the width and height of the frame vs = cv2.VideoCapture(video_path) if not vs.isOpened(): raise Exception(f'unable to open file {video_path}') total_frames = vs.get(cv2.CAP_PROP_FRAME_COUNT) frame_time = 0 frame_count = 0 # loop over the frames of the video while True: vs.set(cv2.CAP_PROP_POS_MSEC, frame_time * 1000) # move frame to a timestamp frame_time += 1/FRAME_RATE (_, frame) = vs.read() # if the frame is None, then we have reached the end of the video file if frame is None: break frame_count += 1 yield frame_count, frame_time, frame vs.release()def detect_unique_screenshots(video_path, output_folder_screenshot_path): '''''' # Initialize fgbg a Background object with Parameters # history = The number of frames history that effects the background subtractor # varThreshold = Threshold on the squared Mahalanobis distance between the pixel and the model to decide whether a pixel is well described by the background model. This parameter does not affect the background update. # detectShadows = If true, the algorithm will detect shadows and mark them. It decreases the speed a bit, so if you do not need this feature, set the parameter to false.
fgbg = cv2.createBackgroundSubtractorMOG2(history=FGBG_HISTORY, varThreshold=VAR_THRESHOLD,detectShadows=DETECT_SHADOWS) captured = False start_time = time.time() (W, H) = (None, None) screenshoots_count = 0 for frame_count, frame_time, frame in get_frames(video_path): orig = frame.copy() # clone the original frame (so we can save it later), frame = imutils.resize(frame, width=600) # resize the frame mask = fgbg.apply(frame) # apply the background subtractor if W is None or H is None: (H, W) = mask.shape[:2] p_diff = (cv2.countNonZero(mask) / float(W * H)) * 100 if p_diff < MIN_PERCENT and not captured and frame_count > WARMUP: captured = True filename = f"{screenshoots_count:03}_{round(frame_time/60, 2)}.png" path = os.path.join(output_folder_screenshot_path, filename) cv2.imwrite(path, orig) screenshoots_count += 1 elif captured and p_diff >= MAX_PERCENT: captured = False print(f'{screenshoots_count} screenshots Captured!') print(f'Time taken {time.time()-start_time}s') return - Assistant
这是一段Python代码,用于将视频文件转换为只含有不同幻灯片的PDF文件,并使用Gradio创建一个简单的Web界面供用户交云操作。这非常适合用于从包含幻灯片播放的视频讲座中提取出幻灯片。
以下是代码的详细分解:
导入必要的库:
- os: 用于处理文件和操作系统路径。
- time: 用于时间相关的功能。
- cv2: OpenCV库,用于图像和视频处理。
- imutils: 方便图像处理的功能(如旋转、缩放)的库。
- shutil: 提供了一些文件操作的函数。
- img2pdf: 将图像转换成PDF文件的库。
- glob: 用于文件路径名的模式匹配。
- argparse: 用于命令行参数解析。
- gradio: 用于创建交互式Web应用程序的库。
定义常量:
定义了一些用于视频处理和背景分离算法的参数,例如帧率、背景分离历史长度、阈值、检测阴影的开关及检测动作的阈值等。
函数定义:
- get_frames(video_path): 此函数通过指定的帧率获取视频文件的帧。
- detect_unique_screenshots(video_path, output_folder_screenshot_path): 该函数使用背景分离方法(MOG2)来检测视频中的独特帧。
- : 初始化输出文件夹,如果存在则删除并重新创建。