import win32event import mmap import array import numpy import cv2 imp

 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import win32event
import mmap
import array
import numpy
import cv2
import threading
import queue
shared_memory_name = "Global\\OBSFRAME"
texture1_mutex_name = "Global\\OBSTEXTURE1_MUTEX"
texture2_mutex_name = "Global\\OBSTEXTURE2_MUTEX"
texture_ready_name = "Global\\OBSTEXTURE_READY"
META_INFO_SIZE=(1024)
TEXTURE_SIZE=(45 * 1024 * 1024)
TEXTURE_1_OFFSET=META_INFO_SIZE
TEXTURE_2_OFFSET=(TEXTURE_SIZE + TEXTURE_1_OFFSET)
SHARED_MEMORY_SIZE = TEXTURE_2_OFFSET + TEXTURE_SIZE
drawtask = queue.Queue(maxsize=2)
def show_window():
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
while True:
data = drawtask.get()
cv2.imshow('title', data)
cv2.waitKey(1)
def getdata(memory, offset, size, height, width):
return numpy.frombuffer(memory[offset:offset+size], numpy.uint8).reshape(height, width, 4)
def getmeta(memory):
return array.array('L', memory[0:16])
INFINITY = 4294967295
sharedmemory = mmap.mmap(0, SHARED_MEMORY_SIZE, shared_memory_name, mmap.ACCESS_READ)
texture1_mutex = win32event.OpenMutex(win32event.SYNCHRONIZE, False, texture1_mutex_name)
texture2_mutex = win32event.OpenMutex(win32event.SYNCHRONIZE, False, texture2_mutex_name)
texture_ready_event = win32event.OpenEvent(win32event.SYNCHRONIZE, False, texture_ready_name);
print('test')
window_job= threading.Thread(target=show_window)
window_job.start()
while True:
if win32event.WAIT_OBJECT_0 == win32event.WaitForSingleObject(texture_ready_event, -1):
meta = getmeta(sharedmemory)
data = numpy.array([])
data_size = meta[0]*meta[1]*4
height = meta[1]
width = meta[0]
if meta[3] == 1:
if win32event.WaitForSingleObject(texture1_mutex, 0) == win32event.WAIT_OBJECT_0:
data = getdata(sharedmemory, TEXTURE_1_OFFSET, data_size, height, width)
win32event.ReleaseMutex(texture1_mutex)
elif win32event.WaitForSingleObject(texture2_mutex, 0) == win32event.WAIT_OBJECT_0:
data = getdata(sharedmemory, TEXTURE_2_OFFSET, data_size, height, width)
win32event.ReleaseMutex(texture2_mutex)
else:
continue
elif meta[3] == 2:
if win32event.WaitForSingleObject(texture2_mutex, 0) == win32event.WAIT_OBJECT_0:
data = getdata(sharedmemory, TEXTURE_2_OFFSET, data_size, height, width)
win32event.ReleaseMutex(texture2_mutex)
elif win32event.WaitForSingleObject(texture1_mutex, 0) == win32event.WAIT_OBJECT_0:
data = getdata(sharedmemory, TEXTURE_1_OFFSET, data_size, height, width)
win32event.ReleaseMutex(texture1_mutex)
else:
continue
else:
continue
data = cv2.cvtColor(data, cv2.COLOR_RGBA2BGR)
try:
drawtask.put_nowait(data)
except:
pass