-
Notifications
You must be signed in to change notification settings - Fork 226
/
Rank_Suit_Isolator.py
144 lines (109 loc) · 4.36 KB
/
Rank_Suit_Isolator.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
### Takes a card picture and creates a top-down 200x300 flattened image
### of it. Isolates the suit and rank and saves the isolated images.
### Runs through A - K ranks and then the 4 suits.
# Import necessary packages
import cv2
import numpy as np
import time
import Cards
import os
img_path = os.path.dirname(os.path.abspath(__file__)) + '/Card_Imgs/'
IM_WIDTH = 1280
IM_HEIGHT = 720
RANK_WIDTH = 70
RANK_HEIGHT = 125
SUIT_WIDTH = 70
SUIT_HEIGHT = 100
# If using a USB Camera instead of a PiCamera, change PiOrUSB to 2
PiOrUSB = 1
if PiOrUSB == 1:
# Import packages from picamera library
from picamera.array import PiRGBArray
from picamera import PiCamera
# Initialize PiCamera and grab reference to the raw capture
camera = PiCamera()
camera.resolution = (IM_WIDTH,IM_HEIGHT)
camera.framerate = 10
rawCapture = PiRGBArray(camera, size=(IM_WIDTH,IM_HEIGHT))
if PiOrUSB == 2:
# Initialize USB camera
cap = cv2.VideoCapture(0)
# Use counter variable to switch from isolating Rank to isolating Suit
i = 1
for Name in ['Ace','Two','Three','Four','Five','Six','Seven','Eight',
'Nine','Ten','Jack','Queen','King','Spades','Diamonds',
'Clubs','Hearts']:
filename = Name + '.jpg'
print('Press "p" to take a picture of ' + filename)
if PiOrUSB == 1: # PiCamera
rawCapture.truncate(0)
# Press 'p' to take a picture
for frame in camera.capture_continuous(rawCapture, format="bgr",use_video_port=True):
image = frame.array
cv2.imshow("Card",image)
key = cv2.waitKey(1) & 0xFF
if key == ord("p"):
break
rawCapture.truncate(0)
if PiOrUSB == 2: # USB camera
# Press 'p' to take a picture
while(True):
ret, frame = cap.read()
cv2.imshow("Card",frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("p"):
image = frame
break
# Pre-process image
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
retval, thresh = cv2.threshold(blur,100,255,cv2.THRESH_BINARY)
# Find contours and sort them by size
dummy,cnts,hier = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key=cv2.contourArea,reverse=True)
# Assume largest contour is the card. If there are no contours, print an error
flag = 0
image2 = image.copy()
if len(cnts) == 0:
print('No contours found!')
quit()
card = cnts[0]
# Approximate the corner points of the card
peri = cv2.arcLength(card,True)
approx = cv2.approxPolyDP(card,0.01*peri,True)
pts = np.float32(approx)
x,y,w,h = cv2.boundingRect(card)
# Flatten the card and convert it to 200x300
warp = Cards.flattener(image,pts,w,h)
# Grab corner of card image, zoom, and threshold
corner = warp[0:84, 0:32]
#corner_gray = cv2.cvtColor(corner,cv2.COLOR_BGR2GRAY)
corner_zoom = cv2.resize(corner, (0,0), fx=4, fy=4)
corner_blur = cv2.GaussianBlur(corner_zoom,(5,5),0)
retval, corner_thresh = cv2.threshold(corner_blur, 155, 255, cv2. THRESH_BINARY_INV)
# Isolate suit or rank
if i <= 13: # Isolate rank
rank = corner_thresh[20:185, 0:128] # Grabs portion of image that shows rank
dummy, rank_cnts, hier = cv2.findContours(rank, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
rank_cnts = sorted(rank_cnts, key=cv2.contourArea,reverse=True)
x,y,w,h = cv2.boundingRect(rank_cnts[0])
rank_roi = rank[y:y+h, x:x+w]
rank_sized = cv2.resize(rank_roi, (RANK_WIDTH, RANK_HEIGHT), 0, 0)
final_img = rank_sized
if i > 13: # Isolate suit
suit = corner_thresh[186:336, 0:128] # Grabs portion of image that shows suit
dummy, suit_cnts, hier = cv2.findContours(suit, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
suit_cnts = sorted(suit_cnts, key=cv2.contourArea,reverse=True)
x,y,w,h = cv2.boundingRect(suit_cnts[0])
suit_roi = suit[y:y+h, x:x+w]
suit_sized = cv2.resize(suit_roi, (SUIT_WIDTH, SUIT_HEIGHT), 0, 0)
final_img = suit_sized
cv2.imshow("Image",final_img)
# Save image
print('Press "c" to continue.')
key = cv2.waitKey(0) & 0xFF
if key == ord('c'):
cv2.imwrite(img_path+filename,final_img)
i = i + 1
cv2.destroyAllWindows()
camera.close()