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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import mmap
import ctypes
import win32event
import win32api
import cv2
import numpy as np
import win32con
import array
from math import pow, sqrt
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(1) & 0xFF == ord('q'):
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()
# Dark Magenta
H_LOW = 139
S_LOW = 96
V_LOW = 129
# Light Magenta
H_HIGH = 169
S_HIGH = 225
V_HIGH = 225
magenta_dark = np.array([H_LOW, S_LOW, V_LOW])
magenta_light = np.array([H_HIGH, S_HIGH, V_HIGH])
TARGET_SIZE = 400
SQUARE_SIZE = 600
MAX_TARGET_DISTANCE = sqrt(2 * pow(TARGET_SIZE, 2))
def mouse_move(x, y):
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, x, y, 0, 0)
def is_activated():
return win32api.GetAsyncKeyState(0x14) != 0
frame_width = 1
frame_height = 1
def contour_distance(ct):
moment = cv2.moments(ct)
if moment["m00"] == 0:
return -1, None
cx = int(moment["m10"] / moment["m00"])
cy = int(moment["m01"] / moment["m00"])
mid_x = frame_width / 2
mid_y = frame_height / 2
x = (mid_x - cx) if cx < mid_x else cx - mid_x
y = (mid_y - cy) if cy < mid_y else cy - mid_y
return [x, y]
def contour_filter(ct):
x, y = contour_distance(ct)
# Ensure that that the target is within the user defined max range
if x == -1 or x > TARGET_SIZE or y > TARGET_SIZE:
return False
# Attempt to filter out small contours
if cv2.contourArea(ct) < 1000:
return False
# Calculate the width and height of the contour
extreme_left = tuple(ct[ct[:, :, 0].argmin()][0])
extreme_right = tuple(ct[ct[:, :, 0].argmax()][0])
extreme_top = tuple(ct[ct[:, :, 1].argmin()][0])
extreme_bottom = tuple(ct[ct[:, :, 1].argmax()][0])
width = extreme_right[0] - extreme_left[0]
height = extreme_bottom[1] - extreme_top[1]
if width == 0 or height == 0:
return False
# Attempt to filter out wide objects
if abs(width / height) > 2.5:
return False
return True
def process(frame):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
magenta_color_range = cv2.inRange(hsv, magenta_dark, magenta_light)
res = cv2.bitwise_or(frame, frame, mask=magenta_color_range)
res = cv2.cvtColor(res, cv2.COLOR_RGB2GRAY)
# Extract magenta colors
thresh = cv2.adaptiveThreshold(res, 1, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, 1)
# Locate the objects with magenta outline
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Create a convex hull polygon around each extracted object
contours = map(lambda ct: cv2.convexHull(ct, False), contours)
# Sort the objects and extract the biggest
contours = sorted(contours, key=cv2.contourArea, reverse=True)
#if __debug__:
# Yellow contours are all contour matches, before filtering out
# cv2.drawContours(frame, contours, -1, (255, 255, 0), 1)
# Attempt to filter out extraneous contours, so that only characters exist
return list(filter(contour_filter, contours))
def locate_target(frame, target):
# compute the center of the contour
moment = cv2.moments(target)
if moment["m00"] == 0:
return
cx = int(moment["m10"] / moment["m00"])
cy = int(moment["m01"] / moment["m00"])
mid_x = frame_width / 2
mid_y = frame_height / 2
x = -(mid_x - cx) if cx < mid_x else cx - mid_x
y = -(mid_y - cy) if cy < mid_y else cy - mid_y
target_size = cv2.contourArea(target)
distance = sqrt(pow(x, 2) + pow(y, 2))
# There's definitely some sweet spot to be found here
# for the sensitivity in regards to the target's size
# and distance
slope = ((1.0 / 3.0) - 1.0) / (MAX_TARGET_DISTANCE / target_size)
multiplier = ((MAX_TARGET_DISTANCE - distance) / target_size) * slope + 1
if is_activated():
mouse_move(int(x * multiplier), int(y * multiplier))
if __debug__:
# draw the contour of the chosen target in green
cv2.drawContours(frame, [target], -1, (0, 255, 0), 2)
# draw a small white circle at their center of mass
cv2.circle(frame, (cx, cy), 7, (255, 255, 255), -1)
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
frame_height = height
frame_width = width
frame = cv2.cvtColor(data, cv2.COLOR_RGBA2BGR)
contours = process(frame)
if len(contours) > 1:
locate_target(frame, contours[1])
# 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(frame)
# 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()