-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.py
274 lines (254 loc) · 7.19 KB
/
Test.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
import cv2
import pdb
from cv2 import cv
import math
import random as rnd
import numpy as np
import autopy
import os
import subprocess
import time
import ImageGrab
import Image
import timeit
import pdb
import scipy as sp
from scipy import signal
#import matplotlib.pyplot as plt
#import matplotlib.image as mpimg
import sys
global Screen
global blobs
#@profile
def cvInRangeTest():
#start=time.time()
lowbound=[8, 32, 165]
upbound=[12, 45, 220]
image=ImageGrab.grab()
diver=np.array(image)
equals=[[(lowbound<=pixels).all()&(pixels<=upbound).all() for pixels in row] for row in diver]
sum1=np.sum(equals)
#lowbound=[165,32,8]
#upbound=[220,45,12]
diver=cv2.inRange(diver,np.array(lowbound),np.array(upbound))
sum2=np.sum(diver)/255
print(sum1)
print(sum2)
#print('time taken is:'+str(time.time()-start))
print(sum1==sum2)
assert(sum1==sum2)
def RGBConvMethodOne():
global Screen
test=np.array(Screen)
test = cv2.cvtColor(test, cv2.COLOR_RGB2GRAY )
def RGBConvMethodTwo():#This is faster.
global Screen
blobs=Screen.convert('L')
blobs=np.array(blobs)
def findSharks():
bmp= ImageGrab.grab()
#rage=np.array(bmp)
#bmp.format='png'
#plt.imshow(rage)
bmp.save('rage.bmp')
bmp.show()
def saveOne():#Faster
global blobs
cv2.imwrite('wordsDetected.png',blobs)
def saveTwo():
global blobs
Image.fromarray(blobs).save('wordsDetected.png')
def getWordsTest():#TODO
'''Get the words from the screen. (This will be the hard part.)'''
global Screen
screen=Screen.copy()
cnt=0
cnt=cnt+1
#screen.show()
(width,height)=screen.size
blobs=screen.convert('L')
blobs=np.array(blobs)
(x,blobs)=(cv2.threshold(blobs, 20, 255, cv2.THRESH_BINARY_INV))
leftmost=0
for y in range(width):
if(blobs[:,y].any()):
leftmost=y
break
return leftmost
def getWordsOld():#TODO
'''Get the words from the screen. (This will be the hard part.)'''
global Screen
screen=Screen.copy()
cnt=0
cnt=cnt+1
#screen.show()
(width,height)=screen.size
blobs=screen.convert('L')
blobs=np.array(blobs)
(x,blobs)=(cv2.threshold(blobs, 20, 255, cv2.THRESH_BINARY_INV))
leftmost=0
for y in range(width):
for x in range(height):
if(blobs[x,y]==255):#&(x==0 or y==0 or x>=height-1 or y>=width-1 or blobs[x-1,y]==255 or blobs[x+1,y]<20 or blobs[x,y-1]==255 or blobs[x,y+1]<20)):
leftmost=y
break
if(leftmost!=0):
break
return leftmost
def matchIms():
bmp=Image.open('C:/Copy/workspace/TyperSharkAI/Images/Play4.png').convert('L')
shark=Image.open('basic_template.gif')
shark.load()
#shark.show()
bmp=np.array(bmp)
shark=np.array(shark)
#Image.fromarray(shark).show()
Image.fromarray(bmp).show()
#bmp.show()
print(bmp.shape)
print(shark.shape)
signal.correlate2d(shark,bmp)
def splitScreen(screen):
#Split the screen into multiple parts, so we can have other methods focus on them.
start=time.time()
x=0
y=0
(width,height)=screen.size
midscreen = screen.crop((100,25,width, height-75))#100, 25, width-100, height-75);
altscreen = screen.crop((x,y,x+25,height))#x, y, 100, height);
bottscreen = screen.crop((x,height-38,width,height))#x, height-50, width, 50);
diverscreen = screen.crop((32,height/3+75,width/7-15,3*height/4-45))
(mwidth,mheight)=midscreen.size
centScreen=midscreen.crop((width/2-125,height/2-50,width/2-100,height/2-10))
screens=[screen,altscreen,bottscreen,midscreen,diverscreen,centScreen]
return screens
def teethFinder():
bmp=Image.open('C:/Copy/workspace/TyperSharkAI/Images/Play4.png')
bmp=splitScreen(bmp)[3]
(width,height)=bmp.size
bmp=np.array(bmp)
blobs=np.zeros((height,width),dtype=np.uint8)
for y in range(width-1):
for x in range(height-1):
#print(type(screen[x,y]))
s=sum(bmp[x,y])
if(s<60):
blobs[x,y]=1
return blobs
def teethFinder2():
bmp=Image.open('C:/Copy/workspace/TyperSharkAI/Images/Play4.png')
bmp=splitScreen(bmp)[3]
(width,height)=bmp.size
bmp=np.array(bmp)
blobs=bmp[:,:,0]
leftmost=0
for y in range(width):
for x in range(height):
if(blobs[x,y]<20):
blobs[x,y]=255
if leftmost==0:
leftmost=y
else:
blobs[x,y]=0
blobs=blobs[:,(20+leftmost):]
Image.fromarray(blobs).save('wordsDetected.png')
return blobs
def getTargets(screen):#TODO
'''Get the words from the screen. (This will be the hard part.)'''
(width,height)=screen.size
#screen.save('screen'+str(cnt)+'.png')
blobs=screen.convert('L')
blobs=np.array(blobs)
#blobs=screen.
leftmost=0
targets=[]
for y in range(width):
for x in range(height):
if(blobs[x,y]<20):
print(x,y)
if(y+200<=width):
targets.append(np.copy(blobs[x-20:x+20,y:y+200]))
blobs[x-30:x+30,y:y+200]=255
#Image.fromarray(blobs).show()
#raw_input('')
if leftmost==0:
leftmost=y
cnt=0
for target in targets:
Image.fromarray(target).save('target'+str(cnt)+'.png')
cnt=cnt+1
return targets
'''words=[]
if(leftmost+80<=width):
blobs=blobs[:,(20+leftmost):]
#print(type(blobs))
Image.fromarray(blobs).save('wordsDetected'+str(cnt)+'.png')
os.system('tesseract wordsDetected'+str(cnt)+'.png results'+str(cnt)+' nobatch letters.txt')
# lines=open('results'+str(cnt)+'.txt').readlines()
# #Pass wordsDetected to tesseract and parse output.
# words = [line.strip() for line in lines]
# print(len(words))
#return words
'''
def determineDead():
bmp=Image.open('C:/Copy/workspace/TyperSharkAI/Images/Play4.png')
screens=splitScreen(bmp)
print(determineDeadHelper(screens[4]))
def determineDeadHelper():#diver):
global Diver
(width,height)=Diver.size
diver=np.array(Diver)
pixel1=[220, 170, 0]
pixel2=[260, 210, 20]
cnt=0
#equals=[[(pixel1<=pixels).all()&(pixels<=pixel2).all() for pixels in row] for row in diver]
for y in range(width):
for x in range(height):
pixels=diver[x,y]
if((pixel1<=pixels).all()&(pixels<=pixel2).all()):
cnt=cnt+1
return cnt>650
def inThreshold(pixel,lowbound,upbound):
return (lowbound<=pixel).all()&(pixel<=upbound).all()
def determineDeadHelperNew():#diver):
global Diver
(width,height)=Diver.size
diver=np.array(Diver)
pixel1=[220, 170, 0]
pixel2=[260, 210, 20]
cnt=0
equals=[[(pixel1<=pixels).all()&(pixels<=pixel2).all() for pixels in row] for row in diver]
sumz=np.sum(equals)
return sumz>650
def determineNotFighting(centscreen):
#start=time.time()
lowbound=[8, 32, 165]
upbound=[12, 45, 220]
diver=np.array(centscreen)
equals=[[(lowbound<=pixels).all()&(pixels<=upbound).all() for pixels in row] for row in diver]
sumz=np.sum(equals)
#print('time taken is:'+str(time.time()-start))
return sumz>250
def preparingToDive(midscreen):
start=time.time()
(width,height)=midscreen.size
lowbound=[175, 115, 70]
upbound=[240, 150, 110]
diver=np.array(midscreen)
equals=[[(lowbound<=pixels).all()&(pixels<=upbound).all() for pixels in row] for row in diver]
sumz=np.sum(equals)
print('time taken is:'+str(time.time()-start))
return sumz>300
def bonustest():
bmp= ImageGrab.grab()
bmp.show()
for i in xrange(100):
cvInRangeTest()
#Screen=ImageGrab.grab()
#blobs=np.array(Screen)
#print(timeit.timeit(saveOne,number=100))
#print(timeit.timeit(saveTwo,number=100))
#Screen=splitScreen(Image.open('C:/Copy/George Mason Classes/2013 3Winter/Typer Shark AIwc/Typer Shark AI/trunk/Play0.png'))[3]
#print(timeit.timeit(getWordsTest,number=10))
#print(timeit.timeit(getWordsOld,number=10))
#assert(getWordsTest()==getWordsOld())