-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
199 lines (137 loc) · 5.78 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
import cv2
import numpy as np
from gameworkflow import *
import time
top, right, bottom, left = 10, 100, 300, 400
def calibrateWithNewImages():
rock = False
paper = False
scissor = False
rockPrinted = False
paperPrinted = False
scissorsPrinted = False
rockCount = 0
scissorCount = 0
paperCount = 0
time.sleep(1)
cap = cv2.VideoCapture(0)
while (cap.isOpened()):
ret, img = cap.read()
cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.imshow('input', img)
roi = img[top:bottom, right:left]
k = cv2.waitKey(10)
if (rock and paper and scissor):
cap.release()
elif (not rock and not rockPrinted):
print ("press c key to take a picture of rock!")
rockPrinted = True
elif (rock and not paper and not paperPrinted):
print ("press c key to take a picture of paper!")
paperPrinted = True
elif (rock and paper and not scissor and not scissorsPrinted):
print ("press c key to take a picture of scissors!")
scissorsPrinted = True
if k == 99:
if (not rock and rockCount != 7):
cv2.imwrite("./rock/" + str(rockCount) +".jpg", roi)
rockCount += 1
print ("Picture " + str(rockCount) + " taken!")
if rockCount == 7:
rock = True
elif (not paper and paperCount != 7):
cv2.imwrite("./paper/" + str(paperCount) + ".jpg", roi)
paperCount += 1
print ("Picture " + str(paperCount) + " taken!")
if paperCount == 7:
paper = 7
elif (not scissor and scissorsPrinted != 7):
cv2.imwrite("./scissors/" + str(scissorCount) + ".jpg", roi)
scissorCount += 1
print ("Picture " + str(scissorsPrinted) + " taken!")
if scissorCount == 7:
scissor = True
elif k == 27:
break
def getTotalConfidence(src, imageDir):
totalImages = []
totalMatches = []
totalConfidence = 0
for i in range(5):
tempImg = cv2.imread(imageDir + str(i) +".jpg")
totalImages.append(tempImg)
'''cv2.imshow('fuck',totalImages[1])
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()'''
for image in totalImages:
tempMatch = cv2.matchTemplate(src, image, cv2.TM_CCOEFF_NORMED)
totalMatches.append(tempMatch)
for match in totalMatches:
_, tempConfidence, _, _ = cv2.minMaxLoc(match)
totalConfidence += tempConfidence
return totalConfidence
def templateMatch(gray, scissorGray, rockGray, paperGray, ambientGray):
#ret, thresh1 = cv2.threshold(blur, 70, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
paperMatch = cv2.matchTemplate(gray, paperGray, cv2.TM_CCOEFF_NORMED)
rockMatch = cv2.matchTemplate(gray, rockGray, cv2.TM_CCOEFF_NORMED)
scissorsMatch = cv2.matchTemplate(gray, scissorGray, cv2.TM_CCOEFF_NORMED)
ambientMatch = cv2.matchTemplate(gray, ambientGray, cv2.TM_CCOEFF_NORMED)
_, paperConfidence, _, _ = cv2.minMaxLoc(paperMatch)
_, rockConfidence, _, _ = cv2.minMaxLoc(rockMatch)
_, scissorsConfidence, _, _ = cv2.minMaxLoc(scissorsMatch)
_, ambientConfidence, _, _ = cv2.minMaxLoc(ambientMatch)
#print ("paper: " + str(paperConfidence) + " rock: " + str(rockConfidence) + " scissors: " + str(scissorsConfidence))
if (paperConfidence > rockConfidence and paperConfidence > scissorsConfidence and paperConfidence > ambientConfidence):
#print ("Paper!")
return "paper"
elif (rockConfidence > paperConfidence and rockConfidence > scissorsConfidence and rockConfidence > ambientConfidence):
#print ("Rock!")
return "rock"
elif (scissorsConfidence > rockConfidence and scissorsConfidence > paperConfidence and scissorsConfidence > ambientConfidence):
#print ("Scissors!")
return "scissors"
else:
#print ("Nothing there!")
return ""
def determineMaxConfidence(rockConfidence, paperConfidence, scissorsConfidence):
if (paperConfidence > rockConfidence and paperConfidence > scissorsConfidence):
#print ("Paper!")
return "paper"
elif (rockConfidence > paperConfidence and rockConfidence > scissorsConfidence):
#print ("Rock!")
return "rock"
elif (scissorsConfidence > rockConfidence and scissorsConfidence > paperConfidence):
#print ("Scissors!")
return "scissors"
else:
#print ("Nothing there!")
return ""
def main():
cap = cv2.VideoCapture(0)
print("Press 'c' to calibrate with new images, 'e' to play the game and 'esc' to quit")
while (cap.isOpened()):
ret, img = cap.read()
cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.imshow("input", img)
k = cv2.waitKey(10)
if k == 27:
break
elif k == 99:
cap.release()
cv2.destroyAllWindows()
print("Calibrate with new images")
calibrateWithNewImages()
print("Press 'c' to calibrate with new images, 'e' to play the game and 'esc' to quit")
cap = cv2.VideoCapture(0)
elif k == 101:
x = getTotalConfidence(img, "./rock/")
y = getTotalConfidence(img, "./paper/")
z = getTotalConfidence(img, "./scissors/")
result = determineMaxConfidence(x, y, z)
computerChoice = runGame()
print("Player chooses: " + result)
print("Computer chooses: " + computerChoice)
winner = handleWinner(result, computerChoice)
print(winner + "\n")
main()