Skip to content

Commit 6641d64

Browse files
committed
step2 riconosce ma meno di 4 su 10
1 parent 1cb0fdb commit 6641d64

4 files changed

Lines changed: 81 additions & 31 deletions

File tree

Cards.py

Lines changed: 76 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import cv2
33
import matplotlib.pyplot as plt
44
import math
5-
5+
import pytesseract
66

77

88

@@ -13,8 +13,10 @@
1313
CARD_THRESH = 30
1414

1515
# Width and height of card corner, where rank and suit are
16-
CORNER_WIDTH = 32
17-
CORNER_HEIGHT = 84
16+
CORNER_WIDTH = 90
17+
CORNER_HEIGHT = 170
18+
# CORNER_WIDTH = 32
19+
# CORNER_HEIGHT = 84
1820

1921
# Dimensions of rank train images
2022
RANK_WIDTH = 70
@@ -24,8 +26,10 @@
2426
SUIT_WIDTH = 70
2527
SUIT_HEIGHT = 100
2628

27-
RANK_DIFF_MAX = 2000
28-
SUIT_DIFF_MAX = 700
29+
RANK_DIFF_MAX = 4000
30+
SUIT_DIFF_MAX = 1000
31+
# RANK_DIFF_MAX = 2000
32+
# SUIT_DIFF_MAX = 700
2933

3034

3135
CARD_MAX_AREA = 500000
@@ -88,8 +92,6 @@ def load_ranks(filepath):
8892

8993
return train_ranks
9094

91-
92-
9395
def load_suits(filepath):
9496
"""Loads suit images from directory specified by filepath. Stores
9597
them in a list of Train_suits objects."""
@@ -256,24 +258,38 @@ def preprocess_card(contour, image):
256258
# cv2.waitKey(0)
257259
# cv2.destroyAllWindows()
258260

259-
cv2.imshow('wrap',qCard.warp)
260-
cv2.waitKey(0)
261-
cv2.destroyAllWindows()
261+
# Mostra le singole carte isolate e rettificate
262+
# cv2.imshow('wrap',qCard.warp)
263+
# cv2.waitKey(0)
264+
# cv2.destroyAllWindows()
262265

263266
# Grab corner of warped card image and do a 4x zoom
264267
Qcorner = qCard.warp[0:CORNER_HEIGHT, 0:CORNER_WIDTH]
265268
Qcorner_zoom = cv2.resize(Qcorner, (0,0), fx=4, fy=4)
269+
# cv2.imshow('angolo Top_Left', Qcorner_zoom)
270+
# cv2.waitKey(0)
271+
# cv2.destroyAllWindows()
266272

267273
# Sample known white pixel intensity to determine good threshold level
268-
white_level = Qcorner_zoom[15,int((CORNER_WIDTH*4)/2)]
269-
thresh_level = white_level - CARD_THRESH
270-
if (thresh_level <= 0):
271-
thresh_level = 1
272-
retval, query_thresh = cv2.threshold(Qcorner_zoom, thresh_level, 255, cv2. THRESH_BINARY_INV)
273-
274+
# white_level = Qcorner_zoom[15,int((CORNER_WIDTH*4)/2)]
275+
# thresh_level = white_level - CARD_THRESH
276+
# if (thresh_level <= 0):
277+
# thresh_level = 1
278+
# retval, query_thresh = cv2.threshold(Qcorner_zoom, thresh_level, 255, cv2. THRESH_BINARY_INV)
279+
query_thresh = Qcorner_zoom
280+
# cv2.imshow('???', query_thresh)
281+
# cv2.waitKey(0)
282+
# cv2.destroyAllWindows()
283+
284+
# Split in to top and bottom half (top shows rank, bottom shows suit)
285+
# Qrank = query_thresh[20:185, 0:128]
286+
# Qsuit = query_thresh[186:336, 0:128]
274287
# Split in to top and bottom half (top shows rank, bottom shows suit)
275-
Qrank = query_thresh[20:185, 0:128]
276-
Qsuit = query_thresh[186:336, 0:128]
288+
# Qrank = query_thresh[20:20+CORNER_HEIGHT, 0:0+CORNER_WIDTH]
289+
# Qsuit = query_thresh[20+CORNER_HEIGHT:20+2*CORNER_HEIGHT, 0:0+CORNER_WIDTH]
290+
Qrank = query_thresh[0:CORNER_HEIGHT*2, 0:CORNER_WIDTH*4]
291+
Qsuit = query_thresh[CORNER_HEIGHT*2:CORNER_HEIGHT*4, 0:CORNER_WIDTH*4]
292+
277293

278294
# Find rank contour and bounding rectangle, isolate and find largest contour
279295
Qrank_cnts, hier = cv2.findContours(Qrank, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
@@ -283,9 +299,13 @@ def preprocess_card(contour, image):
283299
# image to match dimensions of the train rank image
284300
if len(Qrank_cnts) != 0:
285301
x1,y1,w1,h1 = cv2.boundingRect(Qrank_cnts[0])
302+
# print(x1,y1,w1,h1)
286303
Qrank_roi = Qrank[y1:y1+h1, x1:x1+w1]
287304
Qrank_sized = cv2.resize(Qrank_roi, (RANK_WIDTH,RANK_HEIGHT), 0, 0)
288305
qCard.rank_img = Qrank_sized
306+
cv2.imshow('split rank', qCard.rank_img)
307+
cv2.waitKey(0)
308+
cv2.destroyAllWindows()
289309

290310
# Find suit contour and bounding rectangle, isolate and find largest contour
291311
Qsuit_cnts, hier = cv2.findContours(Qsuit, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
@@ -298,10 +318,12 @@ def preprocess_card(contour, image):
298318
Qsuit_roi = Qsuit[y2:y2+h2, x2:x2+w2]
299319
Qsuit_sized = cv2.resize(Qsuit_roi, (SUIT_WIDTH, SUIT_HEIGHT), 0, 0)
300320
qCard.suit_img = Qsuit_sized
321+
# cv2.imshow('split suit', qCard.suit_img)
322+
# cv2.waitKey(0)
323+
# cv2.destroyAllWindows()
301324

302325
return qCard
303326

304-
305327
def match_card(qCard, train_ranks, train_suits):
306328
"""Finds best rank and suit matches for the query card. Differences
307329
the query card rank and suit images with the train rank and suit images.
@@ -321,27 +343,34 @@ def match_card(qCard, train_ranks, train_suits):
321343
# Difference the query card rank image from each of the train rank images,
322344
# and store the result with the least difference
323345
for Trank in train_ranks:
324-
325-
326-
327346
diff_img = cv2.absdiff(qCard.rank_img,Trank.img)
328347
rank_diff = int(np.sum(diff_img)/255)
329-
348+
349+
# cv2.imshow('diff', diff_img)
350+
# cv2.waitKey(0)
351+
# cv2.destroyAllWindows()
352+
print(rank_diff, best_rank_match_diff)
330353
if rank_diff < best_rank_match_diff:
331354
best_rank_diff_img = diff_img
332355
best_rank_match_diff = rank_diff
333356
best_rank_name = Trank.name
357+
print(best_rank_name)
334358

335359
# Same process with suit images
336360
for Tsuit in train_suits:
337-
338361
diff_img = cv2.absdiff(qCard.suit_img, Tsuit.img)
339362
suit_diff = int(np.sum(diff_img)/255)
340-
363+
364+
# cv2.imshow('diff', diff_img)
365+
# cv2.waitKey(0)
366+
# cv2.destroyAllWindows()
367+
print(suit_diff, best_suit_match_diff)
368+
341369
if suit_diff < best_suit_match_diff:
342370
best_suit_diff_img = diff_img
343371
best_suit_match_diff = suit_diff
344372
best_suit_name = Tsuit.name
373+
# print(best_suit_name)
345374

346375
# Combine best rank match and best suit match to get query card's identity.
347376
# If the best matches have too high of a difference value, card identity
@@ -355,7 +384,6 @@ def match_card(qCard, train_ranks, train_suits):
355384
# Return the identiy of the card and the quality of the suit and rank match
356385
return best_rank_match_name, best_suit_match_name, best_rank_match_diff, best_suit_match_diff
357386

358-
359387
def draw_results(image, qCard):
360388
"""Draw the card name, center point, and contour on the camera image."""
361389

@@ -448,10 +476,27 @@ def flattener(image, pts, w, h):
448476
M = cv2.getPerspectiveTransform(temp_rect,dst)
449477
warp = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
450478
warp = cv2.cvtColor(warp,cv2.COLOR_BGR2GRAY)
451-
452-
453-
479+
454480
return warp
455481

456-
457-
482+
def match_rank(qCard):
483+
best_rank_match_name = "Unknown"
484+
image = qCard.rank_img
485+
if len(image) != 0:
486+
# Usa Tesseract per riconoscere il testo nell'immagine
487+
text = pytesseract.image_to_string(image, config='--psm 10 --oem 3 -c tessedit_char_whitelist=234567890AJQK')
488+
# Rimuovi spazi bianchi inutili
489+
text = text.strip()
490+
491+
# Verifica se il testo riconosciuto è uno dei caratteri validi
492+
validi = ['2', '3', '4', '5', '6', '7', '8', '9', '0', 'A', 'J', 'Q', 'K']
493+
if text in validi:
494+
return text
495+
else:
496+
return "nd"
497+
498+
499+
def match_suit(qCard, train_suits):
500+
best_suit_match_name = "Unknown"
501+
502+
return

__pycache__/Cards.cpython-311.pyc

670 Bytes
Binary file not shown.

omi.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
# Find the best rank and suit match for the card.
5959
cards[k].best_rank_match,cards[k].best_suit_match,cards[k].rank_diff,cards[k].suit_diff = Cards.match_card(cards[k],train_ranks,train_suits)
6060

61+
cards[k].best_rank_match = Cards.match_rank(cards[k])
62+
print(cards[k].best_rank_match)
63+
# cards[k].best_suit_match = Cards.match_suit(cards[k], train_suits)
64+
6165
# Draw center point and match result on the image.
6266
image = Cards.draw_results(image, cards[k])
6367

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ opencv-python==4.9.0.80
88
packaging==23.2
99
pillow==10.2.0
1010
pyparsing==3.1.1
11+
pytesseract==0.3.10
1112
python-dateutil==2.8.2
1213
six==1.16.0

0 commit comments

Comments
 (0)