This can be a easy app that begins the webcam both in shade or in grayscale mode relying on what the consumer needs:
The grayscale digital camera is carried out by way of the
GrayVideo class and the colour digital camera via
ColorVideo. I needed to have a standard
show_current_frame for each the
GrayVideo objects. That
show_current_frame will get the present body from the method technique and shows it within the GUI. Because the shade and the grayscale movies want completely different body processing, I wanted to have completely different
course of strategies for
GrayVideo. So, I considered having a
Video class with an summary
course of technique.
Is that this a good use of summary courses and inheritance?
What different issues and potential enhancements do you see in my code?
Or is the design improper within the first place?
import cv2 from abc import ABC, abstractmethod class Video(ABC): def __init__(self, supply): self.seize = cv2.VideoCapture(supply) # If supply = Zero the webcam begins def get_raw_frame(self): # Get a bolean and present body (numpy array) from the webcam ret, body = self.seize.learn() if ret: return body @abstractmethod def course of(self): # Methodology to course of frames. # Methodology might be overwritten by subclasses move def show_current_frame(self): # Get processed body and present it within the GUI current_frame = self.course of() cv2.imshow('Dwell', current_frame) def finish(self): # Releases webcam and closes GUI window self.seize.launch() cv2.destroyAllWindows() class ColorVideo(Video): def course of(self): # Since uncooked frames are in shade, there is no processing wanted return self.get_raw_frame() class GrayVideo(ColorVideo): def course of(self): # Grayscaling the uncooked frames raw_frame = self.get_raw_frame() grey = cv2.cvtColor(raw_frame, cv2.COLOR_BGR2GRAY) return grey user_preference = enter('Enter "c" for shade, or "g" for grayscale: ') if user_preference == 'c': video = ColorVideo(supply=0) if user_preference == 'g': video = GrayVideo(supply=0) whereas True: video.show_current_frame() if cv2.waitKey(1) & 0xFF == ord('q'): video.finish()