import os import cv2 from PIL import Image, ImageStat import numpy as np from…
- User
import os import cv2 from PIL import Image, ImageStat import numpy as np from sklearn.neighbors import NearestNeighbors from tqdm import tqdm import re import shutil from typing import Union, List, Dict, Tuple
import warnings warnings.filterwarnings("ignore", category=UserWarning, module='PIL')
class FrameExtractor: def init(self, video_path: str, output_dir: str, frame_interval: int): self.video_path = video_path self.output_dir = output_dir self.frame_interval = frame_interval self._create_directory_if_not_exists()
code@staticmethod def _get_total_frames(video) -> int: return int(video.get(cv2.CAP_PROP_FRAME_COUNT)) @staticmethod def _read_frame(video) -> Tuple[bool, Union[np.ndarray, None]]: return video.read() def _is_frame_in_interval(self, frame_number: int) -> bool: return frame_number % self.frame_interval == 0 def _write_frame_to_disk(self, frame: np.ndarray, frame_number: int) -> None: output_path = os.path.join(self.output_dir, f"frame_{frame_number}.png") cv2.imwrite(output_path, frame) @staticmethod def _print_progress(frame_number: int, total_frames: int) -> None: if frame_number % 100 == 0: print(f"Processed frame {frame_number} of {total_frames}") def _create_directory_if_not_exists(self) -> None: if not os.path.exists(self.output_dir): os.makedirs(self.output_dir) def extract_frames(self) -> None: video = cv2.VideoCapture(self.video_path) total_frames = self._get_total_frames(video) frame_number = 0 while True: ret, frame = self._read_frame(video) if not ret: break if self._is_frame_in_interval(frame_number): self._write_frame_to_disk(frame, frame_number) self._print_progress(frame_number, total_frames) frame_number += 1 - Assistant
Here are a few suggestions to improve the code:
-
Use more descriptive variable names. For example, instead of
retandframe, you could useframe_read_successandcurrent_frame. -
Add type hints consistently for all function parameters and return values. This makes the code more readable and can catch type errors early.
-
Use f-strings for string formatting instead of string concatenation. For example:
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.