-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
581 lines (476 loc) · 16.3 KB
/
main.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
import cv2
import numpy as np
import pandas as pd
import os
import pyautogui
from time import time
from PIL import ImageGrab, Image
import win32gui, win32ui, win32con
import pytesseract
from pytesseract import Output
from datetime import datetime, timezone
import re
import string
import time
from sklearn.cluster import KMeans
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
import items
def listWindowNames():
def winEnumHandler(hwnd, ctx):
if win32gui.IsWindowVisible(hwnd):
print(hex(hwnd), win32gui.GetWindowText(hwnd))
win32gui.EnumWindows(winEnumHandler, None)
def windowCapture():
width = 3440
height = 1440
hwnd = None
# hwnd = win32gui.FindWindow(None, windowname)
wDC = win32gui.GetWindowDC(hwnd)
dcObj = win32ui.CreateDCFromHandle(wDC)
cDC = dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, width, height)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0,0), (width,height) , dcObj, (0,0), win32con.SRCCOPY)
# dataBitMap.SaveBitmapFile(cDC, 'debug.bmp')
signedIntsArray = dataBitMap.GetBitmapBits(True)
img = np.fromstring(signedIntsArray, dtype='uint8')
img.shape = ( height, width, 4 )
# Cleanup
dcObj.DeleteDC()
cDC.DeleteDC()
win32gui.ReleaseDC(hwnd, wDC)
win32gui.DeleteObject(dataBitMap.GetHandle())
img = img[...,:3]
img = np.ascontiguousarray(img)
return img
def windowCaptureRealtime():
loop_time = time()
while(True):
# screenshot = ImageGrab.grab()
screenshot = windowCapture()
# screenshot = np.array(screenshot)
# screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR)
cv2.imshow('Computer Vision', screenshot)
print('FPS {}'.format( 1 / (time() - loop_time)))
loop_time = time()
# press q to quit
if cv2.waitKey(1) == ord('q'):
cv2.destroyAllWindows()
break
def windowCaptureSave(filename):
# screenshot = windowCapture()
# im = Image.fromarray(screenshot).convert('RGB')
im = screenshot = ImageGrab.grab()
im.save(filename + '.jpeg')
# TODO Color channels are wrong currently, Orange = Blue
def whiteThreshold(image, vMin=78):
hMin = sMin = 0
vMin = vMin
hMax = 179
sMax = vMax = 255
# Set minimum and max HSV values to display
lower = np.array([hMin, sMin, vMin])
upper = np.array([hMax, sMax, vMax])
# Create HSV Image and threshold into a range.
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
output = cv2.bitwise_and(image,image, mask= mask)
return output
def binaryThreshold(img, threshold):
ret, img = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY_INV)
return img
def grayscale(img):
return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
def drawSquareOnImage(img, x1: int, y1: int, x2: int, y2: int):
green = (0, 255, 0)
thickness = 1
img = cv2.rectangle(img, (x1, y1), (x2, y2), green, thickness)
return img
def displayImage(img):
cv2.imshow('image', img)
cv2.waitKey(0)
def drawRows():
x1 = 3182-1920
y1 = 423
x2 = 4780-1920
y2 = 525
img = cv2.imread('soul_03.jpeg')
itemsOnScreen = 9
rowWidth = 103
offset = 5
#TODO might need to fix the offset eventually
for i in range(itemsOnScreen):
y1 += offset
y2 -= offset
img = drawSquareOnImage(img, x1, y1, x2, y2)
y1 += rowWidth - offset
y2 += rowWidth + offset
displayImage(img)
def crop(img, x1, y1, x2, y2):
return img[y1:y2, x1:x2]
def getRow(img, x1, y1, x2, y2):
return img[y1:y2, x1:x2]
def sliceRow(img, x1, x2):
return img[:, x1:x2]
def identifyText(img):
# data = pytesseract.image_to_data(img)
data = pytesseract.image_to_string(img)
return data
def preprocessItem(img, vMin):
img = whiteThreshold(img, vMin)
img = grayscale(img)
# img = cv2.GaussianBlur(img, (7, 7), 0)
# img = binaryThreshol d(img, 32)
return img
def getTextFromData(data):
text = ''
for i in range(len(data['level'])):
if float(data['conf'][i]) > 90.0: #TODO -1 in conf means multi-word....
text += (data["text"][i] + ' ')
text = str.strip(text)
return text
def identifyTextItem(original_image):
attempts = 0
max_attempts = 5
vMin_max = 80
vMin = vMin_max
while (attempts < max_attempts):
img = original_image.copy()
img = preprocessItem(img, vMin)
data = pytesseract.image_to_data(img, lang='eng',
config='--psm 7',output_type=Output.DICT)
# psm 7 - Treat the image as a single text line.
text = getTextFromData(data)
# print( repr(data))
attempts += 1
if text == "":
vMin -= 10
continue
else:
return text
return None
# n_boxes = len(d['level'])
# for i in range(n_boxes):
# (x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
# cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
def identifyTextPrice(original_image):
attempts = 0
max_attempts = 5
vMin_max = 80
vMin = vMin_max
pattern = re.compile(r"^\$?(([1-9]\d{0,2}(,\d{3})*)|0)?\.\d{1,2}$")
while (attempts < max_attempts):
img = original_image.copy()
img = preprocessItem(img, vMin)
data = pytesseract.image_to_data(img, lang='eng',
config='--psm 7 -c tessedit_char_whitelist=0123456789.,',output_type=Output.DICT)
# psm 7 - Treat the image as a single text line.
text = getTextFromData(data)
# print( repr(data))
attempts += 1
if (text == "") or ( not pattern.fullmatch(text)):
vMin -= 10
continue
else:
return text
return None
def identifyTextAvail(original_image):
attempts = 0
max_attempts = 5
vMin_max = 80
vMin = vMin_max
pattern = re.compile(r"^[0-9]+$")
while (attempts < max_attempts):
img = original_image.copy()
img = preprocessItem(img, vMin)
data = pytesseract.image_to_data(img, lang='eng',
config='--psm 7 -c tessedit_char_whitelist=0123456789',output_type=Output.DICT)
# psm 7 - Treat the image as a single text line.
text = getTextFromData(data)
attempts += 1
if (text == "") or ( not pattern.match(text)):
vMin -= 10
continue
else:
return text
return None
def identifyTextTime(original_image):
attempts = 0
max_attempts = 5
vMin_max = 80
vMin = vMin_max
# pattern = re.compile(r"^[0-9]+$") #TODO define a regex match
while (attempts < max_attempts):
img = original_image.copy()
img = preprocessItem(img, vMin)
data = pytesseract.image_to_data(img, lang='eng',
config='--psm 7 -c tessedit_char_whitelist=0123456789DdHhMmSs',output_type=Output.DICT)
# psm 7 - Treat the image as a single text line.
text = getTextFromData(data)
attempts += 1
# or ( not pattern.match(text))
if (text == "") :
vMin -= 10
continue
else:
return text
return None
def identifyTextLocation(original_image):
attempts = 0
max_attempts = 5
vMin_max = 80
vMin = vMin_max
pattern = ["Brightwood", "Cleave's Point", "Cutlass Keys",
"Eastburn", "Ebonscale Reach", "Everfall", "First Light",
"Last Stand", "Monarch's Bluffs", "Mountainhome",
"Mountainrise", "Mourningdale", "Reekwater", "Restless Shore"
"Valor Hold", "Weaver's Fen", "Windsward"]
while (attempts < max_attempts):
img = original_image.copy()
img = preprocessItem(img, vMin)
data = pytesseract.image_to_data(img, lang='eng',
config="--psm 7 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\\'",
output_type=Output.DICT)
# psm 7 - Treat the image as a single text line.
text = getTextFromData(data)
attempts += 1
# or ( not pattern.match(text))
if (text == "") or (text not in pattern):
vMin -= 10
continue
else:
return text
return None
def processRow(img):
row = []
x_offset = 3182
left_bound = 3278 - x_offset
right_bound = 3647 - x_offset
item = sliceRow(img, left_bound, right_bound)
row.append(identifyTextItem(item))
left_bound = 3650 - x_offset
right_bound = 3838 - x_offset
price = sliceRow(img, left_bound, right_bound)
row.append(identifyTextPrice(price))
left_bound = 4370 - x_offset
right_bound = 4452 - x_offset
quantity_available = sliceRow(img, left_bound, right_bound)
row.append(identifyTextAvail(quantity_available))
left_bound = 4545 - x_offset
right_bound = 4629 - x_offset
time_left = sliceRow(img, left_bound, right_bound)
row.append(identifyTextTime(time_left))
left_bound = 4632 - x_offset
right_bound = 4780 - x_offset
location = sliceRow(img, left_bound, right_bound)
row.append(identifyTextLocation(location))
return row
def processRowWithoutItem(img, item_name):
row = []
row.append(item_name)
x_offset = 3182
left_bound = 3650 - x_offset
right_bound = 3838 - x_offset
price = sliceRow(img, left_bound, right_bound)
row.append(identifyTextPrice(price))
left_bound = 4370 - x_offset
right_bound = 4452 - x_offset
quantity_available = sliceRow(img, left_bound, right_bound)
row.append(identifyTextAvail(quantity_available))
left_bound = 4545 - x_offset
right_bound = 4629 - x_offset
time_left = sliceRow(img, left_bound, right_bound)
row.append(identifyTextTime(time_left))
left_bound = 4632 - x_offset
right_bound = 4780 - x_offset
location = sliceRow(img, left_bound, right_bound)
row.append(identifyTextLocation(location))
return row
def defineRegularExpression():
price_pattern = re.compile("^\$?(([1-9]\d{0,2}(,\d{3})*)|0)?\.\d{1,2}$")
test = "0.01"
match = price_pattern.fullmatch(test)
if match:
print(match)
def getMeThatData(img):
x1 = 3182-1920
y1 = 423
x2 = 4780-1920
y2 = 525
# img = cv2.imread('soul_03.jpeg')
rowsData = []
itemsOnScreen = 9
rowWidth = 103
offset = 5
for i in range(itemsOnScreen):
y1 += offset
y2 -= offset
row = getRow(img, x1, y1, x2, y2)
rowData = processRow(row)
rowsData.append(rowData)
y1 += rowWidth - offset
y2 += rowWidth + offset
df = pd.DataFrame(rowsData, columns=["Name", "Price", "Amount Available", "Time Available", "Location"])
print(df)
def singleItemData(img):
x1 = 3182-1920
y1 = 423
x2 = 4780-1920
y2 = 525
# img = cv2.imread('soul_03.jpeg')
rowsData = []
itemsOnScreen = 9
rowWidth = 103
offset = 5
y1 += offset
y2 -= offset
row = getRow(img, x1, y1, x2, y2)
rowData = processRow(row)
item_name = rowData[0]
rowsData.append(rowData)
y1 += rowWidth - offset
y2 += rowWidth + offset
for i in range(itemsOnScreen-1):
y1 += offset
y2 -= offset
row = getRow(img, x1, y1, x2, y2)
rowData = processRowWithoutItem(row, item_name)
rowsData.append(rowData)
y1 += rowWidth - offset
y2 += rowWidth + offset
df = pd.DataFrame(rowsData, columns=["Name", "Price", "Amount Available", "Time Available", "Location"])
print(df)
def selectAllSettlements():
click_duration = 0.2
pyautogui.moveTo(2608, 193, duration=click_duration, tween=pyautogui.easeInOutQuad)
pyautogui.click(interval=click_duration)
pyautogui.moveTo(2892, 233, duration=click_duration, tween=pyautogui.easeInOutQuad)
pyautogui.click(interval=click_duration)
pyautogui.moveTo(2656, 253, duration=click_duration, tween=pyautogui.easeInOutQuad)
pyautogui.click(interval=click_duration)
pyautogui.moveTo(2608, 193, duration=click_duration, tween=pyautogui.easeInOutQuad)
pyautogui.click(interval=click_duration)
def getBuyOrderScreen(target_item):
getTradingPost()
getToItemScreen(target_item)
pyautogui.moveTo(908, 899, duration=0.2, tween=pyautogui.easeInOutQuad)
pyautogui.click()
def getSellOrderScreen(target_item):
getTradingPost()
getToItemScreen(target_item)
pyautogui.moveTo(927, 984, duration=0.2, tween=pyautogui.easeInOutQuad)
pyautogui.click()
def getToItemScreen(target_item):
# pyautogui.click(759,297,duration=0.3)
pyautogui.moveTo(759, 297, duration=0.2, tween=pyautogui.easeInOutQuad)
pyautogui.click()
pyautogui.click()
# time.sleep(0.53)
pyautogui.write(target_item,interval=0.1)
pyautogui.moveTo(721, 478, duration=0.2, tween=pyautogui.easeInOutQuad)
pyautogui.click(interval=0.3)
time.sleep(1.5) # need to wait for the screen to load
#TODO adjust this wait time
def getMinimumPrice(img):
x1 = 3182-1920
y1 = 423
x2 = 4780-1920
y2 = 525
# img = cv2.imread('soul_03.jpeg')
offset = 5
y1 += offset
y2 -= offset
row = getRow(img, x1, y1, x2, y2)
rowData = processRow(row)
return rowData
def getItemData(list_of_items, items_on_screen=9):
datetime_start = datetime.now(timezone.utc)
data_list = []
for item_name in list_of_items:
getTradingPost()
getToItemScreen(item_name)
datetime_of_image = datetime.now(timezone.utc)
image = windowCapture()
x1 = 1262
y1 = 423
x2 = 2860
y2 = 525
row_width = 103
offset = 5
for i in range(items_on_screen):
y1 += offset
y2 -= offset
row_image = getRow(image, x1, y1, x2, y2)
data_row = processRowMinimal(row_image)
data_row.insert(0,item_name)
data_row.insert(0,datetime_of_image)
data_row.insert(0,datetime_start)
data_list.append(data_row)
y1 += row_width - offset
y2 += row_width + offset
db = pd.DataFrame(data_list, columns=["Run Started", "Data Recorded", "Name", "Price", "Amount Available"])
return db
def processRowMinimal(img):
row = []
x_offset = 3182
left_bound = 468
right_bound = 656
price = sliceRow(img, left_bound, right_bound)
row.append(identifyTextPrice(price))
left_bound = 1188
right_bound = 1270
quantity_available = sliceRow(img, left_bound, right_bound)
row.append(identifyTextAvail(quantity_available))
return row
def isTradingPostOpen():
'''
The in-game header bar is blue when the Trading Post is open.
Returns True if the difference of the root-mean-square of the
average color is less than 1. This works because of how blue
the header bar is
'''
x1=1530
y1=14
x2=1994
y2=106
# cv2.imwrite('trading_post_header.jpeg',img)
img = windowCapture()
img = crop(img,x1,y1,x2,y2)
img_header = cv2.imread('trading_post_header.jpeg')
average = img.mean(axis=0).mean(axis=0)
average_header = img_header.mean(axis=0).mean(axis=0)
delta = 0
for c1, c2 in zip(average, average_header):
delta += (c2-c1)*(c2-c1)
delta = delta**0.5
print(delta)
if delta <= 3:
return True
else:
return False
def openTradingPost():
# pyautogui.click(50,50)
pyautogui.press('f')
def getTradingPost():
while True:
if isTradingPostOpen():
return
else:
openTradingPost()
time.sleep(2)
def loadDatabase(path_to_database="./database.pkl"):
#TODO transfer to JSON files
return pd.read_pickle(path_to_database)
def saveDatabase(database_dataframe, database_file_name):
database_dataframe.to_pickle("./" + database_file_name + ".pkl")
if __name__ == "__main__":
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# screenWidth, screenHeight = pyautogui.size() # Get the size of the primary monitor.
item_list = items.item_arcana
pyautogui.click(50,50)
getTradingPost()
item_list = items.item_arcana
db = getItemData(item_list,1)
saveDatabase(db,"item_prices_2")
# print(pyautogui.position())