Last active
November 4, 2019 05:17
-
-
Save dgnsrekt/34f76264202d8c78ed8f29f220ff3b7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from abc import ABCMeta | |
from cv2 import VideoCapture, CAP_PROP_FRAME_COUNT | |
from cv2 import imshow | |
from pathlib import Path | |
VALID_VIDEO_FORMATS = [".avi", ".flv", ".mov", ".mp4", ".wmv", ".mkv"] | |
VALID_IMAGE_FORMATS = [".bmp", ".jpg", ".png"] | |
VALID_MEDIA_FORMATS = VALID_VIDEO_FORMATS + VALID_VIDEO_FORMATS | |
class Media: | |
def __init__(self, file_path): | |
self.path = Path(file_path) | |
self.check_valid_media() | |
def check_valid_media(self): | |
if self.path.suffix not in VALID_MEDIA_FORMATS: | |
error_message = f"{self.path.suffix} is not a valid format." | |
raise ValueError(error_message) | |
@property | |
def is_image(self): | |
if self.path.suffix in VALID_IMAGE_FORMATS: | |
return True | |
return False | |
@property | |
def is_video(self): | |
if self.path.suffix in VALID_VIDEO_FORMATS: | |
return True | |
return False | |
class Image: | |
def __init__(self, media): | |
self.media = media | |
class Video: | |
def __init__(self, media): | |
self.media = media | |
self.video = VideoCapture(str(self.media.path)) | |
def read(self): | |
return self.video.read() | |
def __len__(self): | |
return round(self.video.get(CAP_PROP_FRAME_COUNT)) | |
def __iter__(self): | |
return self | |
def __next__(self): | |
no_errors, frame = self.read() | |
if no_errors: | |
return frame | |
raise StopIteration | |
class Decoder(metaclass=ABCMeta): | |
def decode(): | |
pass | |
class VideoDecoder(Decoder): | |
def decode(index, frames_left, frame): | |
print("Decoding_frame:", index, frames_left) | |
# do something to frame | |
return "Im some video output" | |
class ImageDecoder(Decoder): | |
def decode(image): | |
pass | |
class BaseProcessor(metaclass=ABCMeta): | |
something = 1 | |
class VideoFramePreProcessing(BaseProcessor): | |
def process(media): | |
return True | |
class VideoRandomProcessTool(BaseProcessor): | |
@classmethod | |
def process(cls, media): | |
print(f"found a random {cls.something}") | |
class VideoPostProcessing(BaseProcessor): | |
def process(media): | |
return "blah" | |
media = Media("video.mp4") | |
print(media.is_video) | |
print(media.is_image) | |
if media.is_video: | |
video = Video(media) | |
for index, frame in enumerate(video): | |
#bunch of raneom processes | |
frame = VideoFramePreProcessing.process(frame) | |
decoded_frame = VideoDecoder.decode(index, len(video), frame) | |
x = VideoRandomProcessTool.process(frame) | |
post_processed_frame = VideoPostProcessing.process(decoded_frame) | |
if media.is_image: | |
image = Image(media) | |
ImageDecoder.decode(image) | |
print("done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment