import cv2 import sh class Camara(object): __device_name = None __device = None __setupScriptPath = '' __w = 1280 __h = 720 __contrast = 0.0 __brightness = 0.0 __saturation = 55.0 __hue = 0.0 __exposure = 166 def __init__(self, setup_script_path, device="video0", width=1280, height=720): self.__device_name = device self.__w = width self.__h = height self.__setupScriptPath = setup_script_path def Close(self): if self.__device is not None: del self.__device self.__device = None def Open(self): self.Close() self.__device = cv2.VideoCapture("/dev/{}".format(self.__device_name)) if self.__device.isOpened(): self.__configure() return True return False def getSize(self): return self.__w, self.__h def setSize(self, w, h): if self.__device is not None and self.__device.isOpened(): self.__w = self.__setCamVar(cv2.CAP_PROP_FRAME_WIDTH, w) self.__h = self.__setCamVar(cv2.CAP_PROP_FRAME_HEIGHT, h) else: self.__w = w self.__h = h def setContrast(self, newVal): if self.__device.isOpened(): self.__contrast = self.__setCamVar(cv2.CAP_PROP_CONTRAST, newVal) else: self.__contrast = newVal def getContrast(self): return self.__contrast def setBrightness(self, newVal): if self.__device.isOpened(): self.__brightness = self.__setCamVar(cv2.CAP_PROP_BRIGHTNESS, newVal) else: self.__brightness = newVal def getBrightness(self): return self.__brightness def __configure(self): self.__w = self.__setCamVar(cv2.CAP_PROP_FRAME_WIDTH, self.__w) self.__h = self.__setCamVar(cv2.CAP_PROP_FRAME_HEIGHT, self.__h) sh.bash(self.__setupScriptPath) def __setCamVar(self, key, val): valold = cv2.VideoCapture.get(self.__device, key) if valold != val: cv2.VideoCapture.set(self.__device, key, val) t = cv2.VideoCapture.get(self.__device, key) return t return val def __getCamVar(self, key): return cv2.VideoCapture.get(self.__device, key); def getFrameCount(self): return cv2.VideoCapture.get(self.__device, cv2.CAP_PROP_BUFFERSIZE); def getFrame(self): if self.__device.isOpened(): retval, image = self.__device.read() if retval: return image return None else: return None def getImageSize(self, image): if hasattr(image, 'shape'): return image.shape[1], image.shape[0] else: return (0, 0, 0) def showFrame(self, name, frame, w=False, maxTime=3000): cv2.imshow(name, frame) if w: if maxTime == -1: cv2.waitKey() else: cv2.waitKey(maxTime) def saveFrame(self, fileName, Frame): cv2.imwrite(fileName, Frame) def readImage(self, filename): return cv2.imread('{}'.format(filename), 0) def destroyWindow(self, name): cv2.destroyWindow(name) def closeWindows(self): cv2.destroyAllWindows()