From 4515c6a6b76af02c13d43a68eed38d4f361bfcce Mon Sep 17 00:00:00 2001 From: VedantDave Date: Sat, 25 Mar 2023 17:36:58 +0100 Subject: [PATCH] Detecting and removing corrupt video files from the dataset --- DPI/utils.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/DPI/utils.py b/DPI/utils.py index 8cf759b..9892ed8 100644 --- a/DPI/utils.py +++ b/DPI/utils.py @@ -215,4 +215,42 @@ def video_from_array(arr, high_noise, filename): frame = np.uint8(frame) frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) out.write(frame) - out.release() \ No newline at end of file + out.release() + + +class CorruptVideos: + def __init__(self, dir_path): + self.dir_path = dir_path + + def _is_video_corrupt(self,filepath): + """ + Check if a video file is corrupt. + + Args: + filepath (str): Path to the video file. + + Returns: + bool: True if the video is corrupt, False otherwise. + """ + # Open the video file + cap = cv2.VideoCapture(filepath) + if not cap.isOpened(): + return True + ret, frame = cap.read() + if not ret: + return True + cap.release() + return False + + def _delete_corrupt_video(self, filepath): + os.remove(filepath) + + def is_video_corrupt(self, delete=False): + for filename in os.listdir(self.dir_path): + filepath = os.path.join(self.dir_path, filename) + if filepath.endswith(".mp4"): + if self._is_video_corrupt(filepath): + print(f"{filepath} is corrupt.") + if delete: + self._delete_corrupt_video(filepath) + print(f"Deleted {filepath}") \ No newline at end of file