Skip to content

Commit e2df710

Browse files
committed
Added the python scripts and template images set
1 parent 805faca commit e2df710

20 files changed

Lines changed: 495 additions & 1 deletion

Cards.py

Lines changed: 406 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# python-PlayingCardDetection-ImageProcessing_omi
2-
project to identify cards in a standard deck of plying cards using python opencv module to track a game of omi.
2+
Using python Opencv module for playing cards detection

omi.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
############## Python-OpenCV Playing Card Detector ###############
2+
#
3+
# Author: Ravindu Fernando
4+
# Description: Python script to detect and identify playing cards
5+
6+
7+
# Import necessary packages
8+
import cv2
9+
import numpy as np
10+
import matplotlib.pyplot as plt
11+
import Cards
12+
13+
14+
### ---- INITIALIZATION ---- ###
15+
# Define constants and initialize variables
16+
17+
## Define font to use
18+
font = cv2.FONT_HERSHEY_SIMPLEX
19+
20+
21+
train_ranks = Cards.load_ranks("suits_ranks")
22+
train_suits = Cards.load_suits("suits_ranks")
23+
24+
25+
# Grab the image
26+
image = cv2.imread(r'images/8cards_top.jpg',cv2.IMREAD_COLOR)
27+
img_copy_function = image.copy()
28+
29+
30+
# Pre-process image (gray, blur, and threshold it)
31+
pre_proc = Cards.preprocess_image(image)
32+
33+
34+
# Find and sort the contours of all cards in the image (query cards)
35+
cnts_sort, cnt_is_card = Cards.find_cards(pre_proc)
36+
37+
38+
# If there are no contours, do nothing
39+
if len(cnts_sort) != 0:
40+
41+
# Initialize a new "cards" list to assign the card objects.
42+
# k indexes the newly made array of cards.
43+
cards = []
44+
k = 0
45+
46+
# For each contour detected:
47+
for i in range(len(cnts_sort)):
48+
if (cnt_is_card[i] == 1):
49+
50+
# Create a card object from the contour and append it to the list of cards.
51+
# preprocess_card function takes the card contour and contour and
52+
# determines the cards properties (corner points, etc). It generates a
53+
# flattened 200x300 image of the card, and isolates the card's
54+
# suit and rank from the image.
55+
cards.append(Cards.preprocess_card(cnts_sort[i],image))
56+
57+
# Find the best rank and suit match for the card.
58+
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)
59+
60+
# Draw center point and match result on the image.
61+
image = Cards.draw_results(image, cards[k])
62+
63+
k = k + 1
64+
65+
66+
67+
68+
# Draw card contours on image (have to do contours all at once or
69+
# they do not show up properly for some reason)
70+
if (len(cards) != 0):
71+
temp_cnts = []
72+
for i in range(len(cards)):
73+
temp_cnts.append(cards[i].contour)
74+
cv2.drawContours(image,temp_cnts, -1, (255,0,0), 15)
75+
76+
77+
78+
imgRGB = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
79+
plt.imshow(imgRGB)
80+
plt.show()
81+
82+
83+
84+
85+
86+
87+
88+

suits_ranks/Ace.jpg

3.5 KB
Loading

suits_ranks/Clubs.jpg

2.47 KB
Loading

suits_ranks/Diamonds.jpg

2.37 KB
Loading

suits_ranks/Eight.jpg

4.69 KB
Loading

suits_ranks/Five.jpg

3.96 KB
Loading

suits_ranks/Four.jpg

3.16 KB
Loading

suits_ranks/Hearts.jpg

2.41 KB
Loading

0 commit comments

Comments
 (0)