├── README.md ├── .gitignore └── lib └── frame └── source ├── validator.py └── __init__.py /README.md: -------------------------------------------------------------------------------- 1 | # INNOVA DRIVE 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /child 2 | /ecu 3 | /monitoring_ecu 4 | /output.mp4 5 | /output2.mp4 6 | /Pipfile 7 | /violence -------------------------------------------------------------------------------- /lib/frame/source/validator.py: -------------------------------------------------------------------------------- 1 | class Validator: 2 | def validate(source): 3 | raise NotImplementedError("validate method not implemented") 4 | 5 | class VideoSourceValidator(Validator): 6 | def validate(source): 7 | valid_path = os.path.exists(source) 8 | if not valid_path: 9 | raise ValueError(f"Invalid video source path: {source}") 10 | return True 11 | 12 | class CameraSourceValidator(Validator): 13 | def validate(source): 14 | # check with opencv if if camera with id exists 15 | valid_camera = cv2.VideoCapture(source).isOpened() 16 | if not valid_camera: 17 | raise ValueError(f"Invalid camera source: {source}") 18 | return True 19 | -------------------------------------------------------------------------------- /lib/frame/source/__init__.py: -------------------------------------------------------------------------------- 1 | from lib.frame.source.validator import VideoSourceValidator, CameraSourceValidator 2 | 3 | __all__ = ["Source", "SourceType"] 4 | 5 | class SourceType(enum.Enum): 6 | VIDEO = "VIDEO" 7 | CAMERA = "DEVICE" 8 | 9 | class Source: 10 | # initiate a source obj, set its type and validate it 11 | def __init__(self, source): 12 | self.source = source 13 | self.set_type() 14 | self.validate_source() 15 | self.set_validators() 16 | 17 | def __str__(self): 18 | return f"Source[{self.type}]: {self.source}" 19 | 20 | def __repr__(self): 21 | return self.__str__() 22 | 23 | def __eq__(self, other): 24 | return self.source == other.source and self.type == other.type 25 | 26 | def __dict__(self): 27 | return { 28 | "source": self.source, 29 | "type": self.type 30 | } 31 | 32 | def set_type(self): 33 | _type = type(self.source) 34 | if _type == str: 35 | self.type = SourceType.VIDEO 36 | elif _type == int: 37 | self.type = SourceType.CAMERA 38 | else: 39 | raise ValueError(f"Invalid source type. provided:{_type}. available types: (str) for video file path, (int) for camera device id") 40 | 41 | def set_validators(self): 42 | if self.type == SourceType.VIDEO: 43 | self.validators = [VideoSourceValidator] 44 | elif self.type == SourceType.CAMERA: 45 | self.validators = [CameraSourceValidator] 46 | 47 | def validate_source(self): 48 | for validator in self.validators: 49 | validator.validate(self.source) --------------------------------------------------------------------------------