import mmap import ctypes import win32event import win32api import cv2

  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
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import mmap
import ctypes
import win32event
import win32api
import cv2
import numpy as np
import array
import threading
import queue
title = 'KEK PEREKEK'
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():
while True:
img = drawtask.get()
cv2.imshow(title, img)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
def getdata(memory, offset, size, height, width):
return np.frombuffer(memory[offset:offset + size], np.uint8).reshape(height, width, 4)
def getmeta(memory):
return array.array('L', memory[0:16])
window_job = threading.Thread(target=show_window)
window_job.start()
def main():
STANDARD_RIGHTS_REQUIRED = 0xF0000
SYNCHRONIZE = 0x100000
MUTANT_QUERY_STATE = 0x1
target_center = [0, 0]
diff = [0, 0]
target_center_frame_before = [0, 0]
sharedmemory = mmap.mmap(0, SHARED_MEMORY_SIZE, shared_memory_name, mmap.ACCESS_READ)
texture1_mutex = win32event.OpenMutex(SYNCHRONIZE, False, texture1_mutex_name)
texture2_mutex = win32event.OpenMutex(SYNCHRONIZE, False, texture2_mutex_name)
texture_ready_event = win32event.OpenEvent(SYNCHRONIZE, False, texture_ready_name)
while True:
if win32event.WAIT_OBJECT_0 == win32event.WaitForSingleObject(texture_ready_event, -1):
meta = getmeta(sharedmemory)
data = np.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:
# print('texture 1')
data = getdata(sharedmemory, TEXTURE_1_OFFSET, data_size, height, width)
win32event.ReleaseMutex(texture1_mutex)
elif win32event.WaitForSingleObject(texture2_mutex, 0) == win32event.WAIT_OBJECT_0:
# print('texture 2')
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:
# print('texture 1')
data = getdata(sharedmemory, TEXTURE_2_OFFSET, data_size, height, width)
win32event.ReleaseMutex(texture2_mutex)
elif win32event.WaitForSingleObject(texture1_mutex, 0) == win32event.WAIT_OBJECT_0:
# print('texture 2')
data = getdata(sharedmemory, TEXTURE_1_OFFSET, data_size, height, width)
win32event.ReleaseMutex(texture1_mutex)
else:
continue
else:
continue
img = cv2.cvtColor(data, cv2.COLOR_RGBA2BGR)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_red = np.array([22, 70, 65], np.uint8)
upper_red = np.array([106, 255, 255], np.uint8)
red = cv2.inRange(hsv, lower_red, upper_red)
# res = cv2.bitwise_and(img, img, mask=red)
# # Filters
# kernel = np.ones((5, 5), np.uint8)
# erosion = cv2.erode(red, kernel, iterations=1)
# dilation = cv2.dilate(red, kernel, iterations=1)
# -- Tracking the Red Color --
contours, hierarchy = cv2.findContours(red, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if area > 1200:
x, y, w, h = cv2.boundingRect(contour)
target_center = (x + w // 2, y + h // 2)
diff = (target_center[0] - win32api.GetCursorPos()[0],
target_center[1] - win32api.GetCursorPos()[1])
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
# res = cv2.rectangle(res, (x, y), (x + w, y + h), (0, 255, 0), 2)
# cv2.putText(img, "target", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255))
if target_center and diff:
if -100 < diff[0] < 100 and -100 < diff[1] < 100:
print('diff = ', diff)
#раскомментируй чтоб двигать мышью
# ctypes.windll.user32.mouse_event(1, diff[0]//3, diff[1]//3, 0, 0)
else:
pass
# print(area)
# print(target_center_frame_before, target_center)
# print(diff)
# ctypes.windll.user32.mouse_event(1, diff[0], diff[1], 0, 0)
# ctypes.windll.user32.mouse_event(1, -(target_center_frame_before[0] - target_center[0]), -(target_center_frame_before[1] - target_center[1]), 0, 0)
else:
pass
try:
drawtask.put_nowait(img)
# drawtask.put_nowait(erosion)
# drawtask.put_nowait(dilation)
# drawtask.put_nowait(red)
# cv2.imshow('erosion', erosion)
# cv2.imshow('dilation', dilation)
# cv2.imshow('red', red)
except:
pass
if __name__ == '__main__':
main()