-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeCode
More file actions
197 lines (167 loc) · 6.45 KB
/
MergeCode
File metadata and controls
197 lines (167 loc) · 6.45 KB
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
import cv2
import random
import numpy as np
from tensorflow.keras import layers, models
# create model
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(200, 200, 3)))
model.add(layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Dropout(0.2))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu', kernel_initializer='he_uniform'))
model.add(layers.Dropout(0.2))
model.add(layers.Dense(10, activation='softmax'))
model.summary()
filepath = "weights.hdf5"
model.load_weights(filepath)
spriteA = cv2.imread("C:\\Users\\nadee\\Downloads\\pictures\\spriteLeft.png")
spriteW = cv2.imread("C:\\Users\\nadee\\Downloads\\pictures\\spriteForward.png")
spriteS = cv2.imread("C:\\Users\\nadee\\Downloads\\pictures\\spriteBackward.png")
spriteD = cv2.imread("C:\\Users\\nadee\\Downloads\\pictures\\spriteRight.png")
#class
class Maze():
def __init__(self):
self.faceROI = 0
self.project = False
self.maze0 = cv2.imread("C:\\Users\\nadee\\Downloads\\pictures\\Maze.png")
self.maze = cv2.resize(self.maze0, (0, 0), fx=.5, fy=.5)
#roi sprite
def redraw(self,image,x,y):
sprite0 = image
sprite = cv2.resize(sprite0, (0, 0), fx=.4, fy=.4)
cv2.waitKey(33)
spriteCopy = sprite.copy()
(hgt, wid, _) = sprite.shape
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255),
(255, 255, 0), (255, 0, 255), (0, 255, 255),
(128, 0, 0), (0, 128, 0), (0, 0, 128)]
# contouring
gSprite = cv2.cvtColor(sprite, cv2.COLOR_BGR2GRAY)
res, threshed = cv2.threshold(gSprite, 95, 255, cv2.THRESH_BINARY)
conts, hier = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(spriteCopy, conts, -1, random.choice(colors), 2)
# ROI
maze = self.maze.copy()
self.faceROI = maze[y:y + hgt, x:x + wid, :]
# masking p1
mask1 = np.zeros((hgt, wid), sprite.dtype)
cv2.drawContours(mask1, conts, -1, 255, -1)
mask2 = 255 - mask1
# masking p2
part1 = cv2.bitwise_and(sprite, sprite, mask=mask1)
part2 = cv2.bitwise_and(self.faceROI, self.faceROI, mask=mask2)
# final
self.faceROI[:, :, :] = part1 + part2
cv2.imshow("Sprite", maze)
#cv2.waitKey()
# #restricting
def restrict(self,image,newx,newy):
sprite0 = image
sprite = cv2.resize(sprite0, (0, 0), fx=.4, fy=.4)
cv2.waitKey(33)
spriteCopy = sprite.copy()
(hgt, wid, _) = sprite.shape
self.faceROI2 = self.maze[newy:newy + hgt, newx:newx + wid, :]
print(self.faceROI2.shape)
gROI = cv2.cvtColor(self.faceROI2, cv2.COLOR_BGR2GRAY)
res, threshed = cv2.threshold(gROI, 10, 255, cv2.THRESH_BINARY_INV)
if threshed.sum() > 0:
self.project = False
else:
self.project = True
return(self.project)
#keyclick
def keyclick():
key = cv2.waitKey(33)
return key
def make_pred(img2):
cv2.resize(img2, (200, 200))
img3 = img2.reshape((1, 200, 200, 3))
frame = np.array(img3) / 255
predictions = model.predict(img3)
maxIndex = -1
maxNum = -1
for i in range(4):
print(predictions[0, i])
if predictions[0, i] > maxNum:
maxNum = predictions[0, i]
maxIndex = i
cv2.imshow("img", img2)
cv2.waitKey()
if maxIndex == 0:
print("A")
return "A"
elif maxIndex == 1:
print("D")
return "D"
elif maxIndex == 2:
print("S")
return "S"
elif maxIndex == 3:
print("W")
return "W"
else:
print("Unable to identify image")
return "U"
def main():
Maze1 = Maze()
#game start
startScreen = cv2.imread("C:\\Users\\nadee\\Downloads\\pictures\\accMaze.png")
startScreen1 = cv2.resize(startScreen, (0, 0), fx=.5, fy=.5)
cv2.imshow("Sprite", startScreen1)
cv2.waitKey()
currentX = 82
currentY = 75
Maze1.redraw(spriteW,currentX,currentY)
while (True):
x = keyclick()
if x == ord('a'):
Maze1.restrict(spriteA,currentX-5, currentY)
if Maze1.project == False:
Maze1.redraw(spriteA,currentX,currentY)
else:
currentX = currentX - 5
Maze1.redraw(spriteA,currentX,currentY)
elif x == ord('w'):
Maze1.restrict(spriteW,currentX, currentY+5)
if Maze1.project == False:
Maze1.redraw(spriteW, currentX, currentY)
else:
currentY = currentY + 5
Maze1.redraw(spriteW, currentX, currentY)
elif x == ord('d'):
Maze1.restrict(spriteD,currentX+5, currentY)
if Maze1.project == False:
Maze1.redraw(spriteD, currentX, currentY)
else:
currentX = currentX + 5
Maze1.redraw(spriteD, currentX, currentY)
elif x == ord('s'):
Maze1.restrict(spriteA,currentX, currentY-5)
if Maze1.project == False:
Maze1.redraw(spriteS, currentX, currentY)
else:
currentY = currentY - 5
Maze1.redraw(spriteS, currentX, currentY)
elif x == ord(' '):
break
if (currentY >= 380 and currentY <= 415) and (currentX >= 725 and currentX <= 862):
finishScreen = cv2.imread("C:\\Users\\nadee\\Downloads\\pictures\\finishScreen.png")
finishScreen1 = cv2.resize(finishScreen, (0, 0), fx=.5, fy=.5)
cv2.imshow("Sprite" , finishScreen1)
x = cv2.waitKey(33)
if x == ord('r'):
currentX = 82
currentY = 75
Maze1.redraw(spriteW, currentX, currentY)
if __name__ == '__main__':
main()