forked from fangfufu/Linux-Fake-Background-Webcam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
akvcam.py
58 lines (51 loc) · 1.77 KB
/
akvcam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from fcntl import ioctl
import pyfakewebcam.v4l2 as v4l2
import os
import threading
from queue import Queue
import cv2
import numpy as np
class AkvCameraWriter:
def __init__(self, webcam, width, height):
self.webcam = webcam
self.width = width
self.height = height
self.d = self.open_camera()
self.queue = Queue(maxsize=1)
self.thread = threading.Thread(target=self.writer_thread)
self.thread.start()
def open_camera(self):
d = os.open(self.webcam, os.O_RDWR)
cap = v4l2.v4l2_capability()
ioctl(d, v4l2.VIDIOC_QUERYCAP, cap)
vid_format = v4l2.v4l2_format()
vid_format.type = v4l2.V4L2_BUF_TYPE_VIDEO_OUTPUT
vid_format.fmt.pix.width = self.width
vid_format.fmt.pix.height = self.height
vid_format.fmt.pix.pixelformat = v4l2.V4L2_PIX_FMT_RGB24
vid_format.fmt.pix.field = v4l2.V4L2_FIELD_NONE
vid_format.fmt.pix.colorspace = v4l2.V4L2_COLORSPACE_SRGB
ioctl(d, v4l2.VIDIOC_S_FMT, vid_format)
return d
def writer_thread(self):
while True:
elem = self.queue.get()
if elem is None:
break
image_data = cv2.resize(elem, (self.width, self.height)).tobytes()
try:
os.write(self.d, image_data)
except Exception:
break
def schedule_frame(self, image):
self.queue.put(image)
def __del__(self):
self.queue.put(None)
os.close(self.d)
if __name__ == "__main__":
camera_w, camera_h = 1280, 720
writer = AkvCameraWriter("/dev/video3", camera_w, camera_h)
image = cv2.imread("background.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
while True:
writer.schedule_frame(image)