class WebcamThread(Thread): def __init__(self, device, width, height, color='RGB'): '''Intialize device ''' self._cam = Camera(device, width, height) self.width, self.height = self._cam.width, self._cam.height self.running = True self.img = None self.t_wait = 1.0 / 60 # Webcam operates at 30FPS super().__init__() def run(self): '''Thread loop. Read continuously from cam buffer. ''' while self.running: self.img = self._cam.get_frame() sleep(self.t_wait) self._cam.close() def capture(self, path_file): '''Capture image into a file ''' image = self.get_img() image.save(path_file) def get_img(self): return Image.frombytes('RGB', (self.width, self.height), self.img, 'raw', 'RGB') def close(self): '''Stop webcam and thread ''' self.running = False
def video_capture(video_device_index): video_device_name = '/dev/video{}'.format(video_device_index) camera = Camera(video_device_name) try: yield camera finally: camera.close()
from PyV4L2Camera.camera import Camera from PyV4L2Camera.controls import ControlIDs camera = Camera('/dev/video0', 1920, 1080) controls = camera.get_controls() for control in controls: print(control.name) camera.set_control_value(ControlIDs.BRIGHTNESS, 48) for _ in range(2): frame = camera.get_frame() # Decode the image im = Image.frombytes('RGB', (camera.width, camera.height), frame, 'raw', 'RGB') # Convert the image to a numpy array and back to the pillow image arr = np.asarray(im) im = Image.fromarray(np.uint8(arr)) print(np.mean(arr[:, :, 0])) print(np.mean(arr[:, :, 1])) print(np.mean(arr[:, :, 2])) # Display the image to show that everything works fine im.show() camera.close()