allo- / virtual_webcam_background

Use a virtual webcam background and overlays with body-pix and v4l2loopback
GNU General Public License v3.0
306 stars 48 forks source link

cannot change resolution via config file #62

Closed coro1404 closed 3 years ago

coro1404 commented 3 years ago

trying to adjust config file values width and hight to actual webcam resolution fails with error message:

    raise Exception('frame height does not match the height of webcam device: {}!={}\n'.format(self._settings.fmt.pix.height, frame.shape[0]))
Exception: frame height does not match the height of webcam device: 480!=1080

Checking camera resolution with this code lists 1920 x 1080 as capability:

#!/usr/bin/env python3

import cv2

HIGH_VALUE = 10000
WIDTH = HIGH_VALUE
HEIGHT = HIGH_VALUE

capture = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
capture.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))

print(width,height)

results in 1920 1080

config file is set to:

segmentation_threshold: 0.75
width: 1920
height: 1080
blur: 3
erode: 10
dilate: 10
virtual_video_device: "/dev/video2"
real_video_device: "/dev/video0"
average_masks: 3
mjpeg: False
layers:
  - "empty": [["image", "bpk.jpg"]]
  - "foreground": []

any hints on how to set the resolution? It always remains on 640x480

coro1404 commented 3 years ago

exception is raised by:

pyfakewebcam/pyfakewebcam.py", line 68, in schedule_frame

allo- commented 3 years ago

Maybe you can have a look into the code, when you wrote quite similar test code anyway. From your example code it looks like you're explictely requesting XVID. Can you add this line to the program and look if it works for you? We have a similar option for MJPEG and when needed we could add a configuration option for the fourcc setting.

coro1404 commented 3 years ago

ok problem solved: the script cannot change the resolution as long as any app holds an open filehandle on /dev/videoX (even ist no video output is requested). MS Teams for example keeps a lock without displaying a video - identifying the process via lsof an killing it allows the script to change resolution on next start.

Regarding the XVID - I took possible options from here: https://docs.opencv.org/master/dd/d43/tutorial_py_video_display.html

In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG results in high size video. X264 gives very small size video)

and tried them instead of MJPG but I could not see a significant performance change.

Thanks for your help I will close this issue next

allo- commented 3 years ago

I guess that's why chrome requires the device to have exclusive_caps set. If one program expects frames with the current settings, another program should not change the setting. If the device is only opened when used depends on the program. Maybe MS Teams locks the device to make sure it will be able to set the resolution when you turn on the camera. I added this to the FAQ issue, so it can be added to the documentation.

allo- commented 3 years ago

I close the issue now. If there is still a code issue or you see a good point how to integrate a function that detects why the resolution cannot be changed, you can reopen it, but I consider it to be a documentation issue and added it to #59.