Spaces:
Running
Running
| import cv2 as cv | |
| class Camera: | |
| def __init__(self): | |
| self.camera = cv.VideoCapture(0) | |
| if not self.camera.isOpened(): | |
| raise ValueError("Unable to open the camera!") | |
| # IMPORTANT: convert to int | |
| self.width = int(self.camera.get(cv.CAP_PROP_FRAME_WIDTH)) | |
| self.height = int(self.camera.get(cv.CAP_PROP_FRAME_HEIGHT)) | |
| def __del__(self): | |
| if self.camera.isOpened(): | |
| self.camera.release() | |
| def get_frame(self): | |
| if self.camera.isOpened(): | |
| ret, frame = self.camera.read() | |
| if ret: | |
| frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB) | |
| return ret, frame | |
| return False, None | |