I generated images containing playing cards and trained using Yolov7. I borrowed some ideas of data generation from geaxgx. I used the trained model to detect cards on the videos containing playing cards in poker.
- pytorch
- yolov7
- cv2
In the working directory, download and go in to the repo.
git clone [email protected]:masarunakajima/playing_card_detection.git
cd playing_card_detectionRun data generation script. You can change the numbers of images for train, valid, and test. For my project, I used 40000, 2000, 100, respectively.
python generate_train_data.py --ntrain 1000 --nvalid 100 --ntest 10
The generated directory (default name data) contains the train, valid, and test images and labels as well as the data.yaml file to be used by yolov7.
Get yolov7 in the working directory.
git clone https://github.com/WongKinYiu/yolov7.git
cd yolov7Run the training script.
batch_size=128
data_dir=data
python train.py --workers 8 --epochs 50 --device 0 --batch-size ${batch_size}\
--data ../${data_dir}/data.yaml --img 600 600 --cfg cfg/training/yolov7.yaml\
--weights '' --name wsop --hyp data/hyp.scratch.p5.yamlIn this project, I used the cards used in WSOP Poker, as shown below.
Note that the red is shown in blue.

First I detected the region of interest in each card, namely the rank and suit at the corner of the card.

I then randomly scaled and rotated the cards and placed them on a background images, keeping track of their positions. When placing the cards, I made sure that the rank-suit portion of each card is not hidden by other cards.

