class VCamOutputTransform(ObservableTransform, BoxLayout, TransformObserver): vcam_id = NumericProperty(defaultvalue=-1) resolution = ObjectProperty() def __init__(self, *args, **kwargs): self.vcam: Union[FakeWebcam, None] = None super().__init__(*args, **kwargs) def on_kv_post(self, base_widget): self.load_vcam() self.bind(vcam_id=self.load_vcam) self.bind(resolution=self.load_vcam) self.preview = self.ids['preview'] self.ids['dropdown'].bind( on_select=lambda _, choice: setattr(self, 'vcam_id', choice)) self.fill_dropdown_menu() def load_vcam(self, *args): print(f'Loading vcam {self.vcam_id}') if self.vcam_id == -1 or not path.exists(f'/dev/video{self.vcam_id}'): return if self.resolution is None: return self.vcam = FakeWebcam(f'/dev/video{self.vcam_id}', *self.resolution) print("virt cam is set up") def notify_new_frame(self, texture: Union[Texture, TextureRegion]): super(VCamOutputTransform, self).notify_new_frame(texture) self.on_new_frame(texture) if self.resolution != texture.size: self.resolution = texture.size if self.vcam is not None: buf = np.frombuffer(texture.pixels, np.uint8) buf = buf.reshape(texture.height, texture.width, 4) self.vcam.schedule_frame(cv2.cvtColor(buf, cv2.COLOR_RGBA2RGB)) def is_active(self) -> bool: return self._source is not None def fill_dropdown_menu(self): dropdown = self.ids['dropdown'] dropdown.clear() dev_path = Path('/dev') for file in dev_path.glob("video*"): print(f'checking camera {file}') cam_id = int(re.match(r'video(\d+)', file.name).group(1)) dropdown.add_option(f'Camera No. {cam_id}', cam_id) dropdown.bind( on_select=lambda _, choice: setattr(self, 'vcam_id', choice))
def load_vcam(self, *args): print(f'Loading vcam {self.vcam_id}') if self.vcam_id == -1 or not path.exists(f'/dev/video{self.vcam_id}'): return if self.resolution is None: return self.vcam = FakeWebcam(f'/dev/video{self.vcam_id}', *self.resolution) print("virt cam is set up")
def create_fakewebcam(device_name: str, preferred_width: int, preferred_height: int) -> FakeWebcam: fakewebcam_instance = FakeWebcam(device_name, width=preferred_width, height=preferred_height) fakewebcam_settings = fakewebcam_instance._settings actual_width = fakewebcam_settings.fmt.pix.width actual_height = fakewebcam_settings.fmt.pix.height if actual_height != preferred_height or actual_width != preferred_width: LOGGER.warning( 'unable to set virtual webcam resolution, using: width=%d, height=%d', actual_width, actual_height) fakewebcam_instance._buffer = np.zeros( (actual_height, 2 * actual_width), dtype=np.uint8) fakewebcam_instance._yuv = np.zeros((actual_height, actual_width, 3), dtype=np.uint8) fakewebcam_instance._ones = np.ones((actual_height, actual_width, 1), dtype=np.uint8) return fakewebcam_instance
def startup(): startup_state = LooperState() listener = Listener(on_press=startup_state.change_state) listener.start() vid_path = './videos/' cam_stream = setup_cam_stream(1) out_stream, vid_file = setup_recording(vid_path) cam = FakeWebcam('/dev/video2', 640, 480) while startup_state.prev_state != 'recording': if startup_state.cur_state == 'recording': ret, frame = cam_stream.read() frame = cv2.resize(frame, (640, 480)) out_stream.write(frame) # quit if q is pressed elif startup_state.quit: break else: startup_state.cur_state == 'live' ret, frame = cam_stream.read() frame = cv2.resize(frame, (640, 480)) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) cam.schedule_frame(frame) time.sleep(1 / 30.0) cam_stream.release() out_stream.release() listener.stop() listener.join() return vid_file
def loop(vid_file=None, vid_path='./videos/'): state = LooperState(video_loaded=True) cam = FakeWebcam('/dev/video2', 640, 480) cam.print_capabilities() cam_stream = setup_cam_stream(1) # out_stream, vid_file = setup_recording(vid_path) file_stream = setup_file_stream(vid_path, vid_file) listener = Listener(on_press=state.change_state) listener.start() while not state.quit: if state.cur_state == 'loop': ret, frame = file_stream.read() if ret: frame = cv2.resize(frame, (640, 480)) else: file_stream.set(cv2.CAP_PROP_POS_FRAMES, 0) continue elif state.cur_state == 'live': ret, frame = cam_stream.read() frame = cv2.resize(frame, (640, 480)) elif state.cur_state == 'blank': frame = np.zeros([480, 640, 3], np.uint8) elif state.cur_state == 'frozen': frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) elif state.cur_state == 'recording': ret, frame = cam_stream.read() frame = cv2.resize(frame, (640, 480)) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) cam.schedule_frame(frame) time.sleep(1 / 30.0) print('Closing all streams...') file_stream.release() cam_stream.release() listener.stop() listener.join()
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, config.get("height")) if config.get("fps"): cap.set(cv2.CAP_PROP_FPS, config.get("fps")) # Attempt to reduce the buffer size if not cap.set(cv2.CAP_PROP_BUFFERSIZE, 1): print('Failed to reduce capture buffer size. Latency will be higher!') # Get the actual resolution (either webcam default or the configured one) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) config['width'], config['height'] = width, height # Initialize a fake video device with the same resolution as the real device fakewebcam = FakeWebcam(config.get("virtual_video_device"), width, height) # pyfakewebcam sometimes does not save the values it is initialized with, so we rewrite them fakewebcam._settings.fmt.pix.width = width fakewebcam._settings.fmt.pix.height = height # Choose the bodypix (mobilenet) model # Allowed values: # - Stride 8 or 16 # internal_resolution: 0.25, 0.5, 0.75, 1.0 output_stride = config.get("stride", 16) multiplier = config.get("multiplier", 0.5) model_type = config.get("model", "mobilenet") if model_type == "resnet":
if self.is_static_image: success, self.static_image = self.webcam_videocapture.read() # BGR to RGB self.static_image = self.static_image[..., ::-1] self.static_image = self.static_image.astype(np.float) if not success: raise ValueError(f"Static image {self.video_device_path} could not be loaded!") def get_frame(self): if self.is_static_image and self.static_image: return self.static_image success, frame = self.webcam_videocapture.read() if not success: raise ValueError("Webcam failed to read image!") frame = frame[..., ::-1] frame = frame.astype(np.float) return frame.astype(np.uint8) def close(self): self.webcam_videocapture.release() class WebcamOutput(Webcam): NotImplemented conf = Config(config_path="config.yaml") webcam_inp = WebcamInput(640, 480, 30, '/dev/video0', conf) webcam_out = FakeWebcam('/dev/video2', 640, 480) while True: webcam_out.schedule_frame(webcam_inp.get_frame())
args = vars(parser.parse_args()) # Start Camera camera = Camera() camera.start() if __name__ == "__main__": signal(SIGINT, stop) # Setup ZeroMQ ctx = zmq.Context() sock = ctx.socket(zmq.REQ) sock.connect('ipc:///tmp/bodypix') background, (width, height) = get_background(args['background']) fake = FakeWebcam('/dev/video20', height, width) running = True print("Running camera at /dev/video20 ...") while running: frame = camera.read() _, image = cv2.imencode('.jpg', frame) frame = frame.astype(np.uint8) # Process image to find body masks sock.send(image.tostring()) mask = process_mask(sock.recv(), (width, height)) frame = composite(frame, background, mask)
import numpy as np from itertools import cycle from time import sleep from timer import Timer from pyfakewebcam import FakeWebcam from cv2 import VideoCapture cam = FakeWebcam('/dev/video4', 640, 480) while True: video = VideoCapture('test-resized.mp4') while (read := video.read())[0]: with Timer("Scheduling frame", verbose=True): cam.schedule_frame(read[1][:, :, ::-1]) sleep(1 / 60)