-
Notifications
You must be signed in to change notification settings - Fork 0
/
cardsDetection_V4.py
149 lines (111 loc) · 5.02 KB
/
cardsDetection_V4.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
import tensorflow.keras
from PIL import Image
import cv2
import numpy as np
import findSet as f
import re
cap = cv2.VideoCapture(0)
model = tensorflow.keras.models.load_model('SetSolverModel_v3_final.h5')
CLASS_NAMES = ['twoMediumPurpleOval', 'twoFullRedWave','twoMediumRedOval',
'twoMediumGreenWave', 'twoMediumPurpleWave', 'twoMediumGreenDiamond',
'twoMediumRedDiamond', 'twoMediumGreenOval', 'twoMediumPurpleDiamond',
'twoMediumRedWave', 'twoFullRedOval', 'twoEmptyRedOval' ,'twoFullRedDiamond',
'twoFullGreenOval', 'twoFullPurpleDiamond', 'twoFullGreenWave',
'twoFullGreenDiamond', 'twoFullPurpleOval' ,'twoEmptyRedWave',
'twoFullPurpleWave', 'twoEmptyGreenWave' ,'twoEmptyGreenOval',
'threeMediumRedWave', 'threeMediumRedDiamond' ,'twoEmptyGreenDiamond',
'twoEmptyPurpleOval', 'threeMediumRedOval', 'twoEmptyPurpleDiamond',
'twoEmptyRedDiamond', 'twoEmptyPurpleWave', 'threeFullRedDiamond',
'threeMediumGreenOval', 'threeFullPurpleWave', 'threeMediumGreenWave',
'threeMediumPurpleOval', 'threeFullRedOval' ,'threeFullRedWave',
'threeMediumPurpleWave', 'threeMediumGreenDiamond',
'threeMediumPurpleDiamond', 'threeFullGreenDiamond' ,'threeEmptyPurpleOval',
'threeEmptyRedDiamon', 'threeEmptyPurpleWave' ,'threeEmptyRedWave',
'threeFullPurpleDiamond', 'threefullGreenWave' ,'threeFullPurpleOval',
'threeEmptyRedOval', 'threeFullGreenOval', 'threeEmptyGreenDiamond',
'threeEmptyPurpleDiamond', 'oneMediumPurpleOval', 'oneMediumRedDiamond',
'threeEmptyGreenWave', 'oneMediumPurpleDiamond', 'oneMediumRedWave',
'oneMediumRedOval', 'threeEmptyGreenOval', 'oneMediumPurpleWave',
'oneMediumGreenOval', 'oneMediumGreenWave' ,'oneFullPurpleDiamond',
'oneFullRedDiamond', 'oneFullGreenWave' ,'oneFullPurpleOval',
'oneFullPurpleWave', 'oneFullRedWave', 'oneMediumGreenDiamond',
'oneFullRedOval', 'oneEmptyPurpleDiamond', 'oneEmptyRedOval',
'oneEmptyRedWave', 'oneEmptyPurpleWave', 'oneFullGreenDiamond',
'oneEmptyGreenOval', 'oneEmptyPurpleOval', 'oneFullGreenOval',
'oneEmptyRedDiamond' ,'oneEmptyGreenWave', 'oneEmptyGreenDiamond']
while True:
_, frame = cap.read()
bluerred_frame = cv2.GaussianBlur(frame,(5,5),0)
hsv = cv2.cvtColor(bluerred_frame, cv2.COLOR_BGR2HSV)
# whole card
sensitivity = 80
lower_white = np.array([0,0,255-sensitivity])
upper_white = np.array([255,sensitivity,255])
mask = cv2.inRange(hsv, lower_white, upper_white)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)
ret,thresh = cv2.threshold(mask, 40, 255, 0)
# Contours
contours, _= cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
# print("the contours are : " , contours)
data = np.ndarray(shape=(1, 160, 160, 3), dtype=np.float32)
cards=[]
n=0
if len(contours) !=0:
for contour in contours:
area = cv2.contourArea(contour)
if area > 10000 :
n=n+1
x,y,w,h = cv2.boundingRect(contour)
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,0),2)
# Extract the frame of each card
roi = frame[y:y+h,x:x+w]
# Save cards
cv2.imwrite(f"cardsE/roi{n}.jpg", roi)
# print("hmm:", n)
image = Image.open(f'cardsE/roi{n}.jpg')
image = image.resize((160, 160))
image_array = np.asarray(image)
# # Normalize the image
# normalized_image_array = image_array.astype(np.float32) / 255.0
normalized_image_array = (image_array.astype(np.float32) / 400.0)
# Load the image into the array
data[0] = normalized_image_array
# run the inference
prediction = model.predict(data)
# print(f'Our Model Predicttion : {prediction}')
pred_id=np.argmax(prediction,axis=-1)
# print(pred_id)
pred_label=CLASS_NAMES[int(pred_id)]
#Save cards name
cards.append(pred_label)
#print the results of detection
PC=prediction[0][pred_id]*100
# print(f'The result is : {pred_label} with {float(PC)} Accuracy %')
name= str(pred_label) + ":" + str(int(PC))
cv2.putText(frame,name,(x,y),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),0)
#print("cards: ",cards)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
def extractCards(cards):
collection=[]
for card in cards:
#Split 'oneFullPurpleOval' to ['one', 'full', 'purple', 'oval']
s=[]
p=re.findall('^[a-z]+|[A-Z][^A-Z]*', card)
[s.append(i.lower()) if not i.islower() else s.append(i) for i in p]
k=["id","color","shape","fill","number"]
# val=["one_blue_empty_diamond","blue","diamonds","empty","one"]
gId=s[0]+"_"+s[2]+"_"+s[1]+"_"+s[3]
val=[gId,s[2],s[3],s[1],s[0]]
zipObj = zip(k,val)
mydict = dict(zipObj)
collection.append(mydict)
return collection
collection=extractCards(cards)
print(f"We found {len(f.getSets(collection))} sets :",f.getSets(collection))
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()