From 97508385ab53249a0a8a524d05f1dbe259d9300e Mon Sep 17 00:00:00 2001
From: Edmond Yoo <hj3yoo@uwaterloo.ca>
Date: Mon, 27 Aug 2018 20:46:13 +0000
Subject: [PATCH] bounding box for mana symbol - only tested for RTR set

---
 fetch_data.py    |    9 +++++++--
 generate_data.py |   47 ++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 51 insertions(+), 5 deletions(-)

diff --git a/fetch_data.py b/fetch_data.py
index 6fc526a..2be9fe6 100644
--- a/fetch_data.py
+++ b/fetch_data.py
@@ -32,6 +32,11 @@
     return df
 
 
+def load_all_cards_text(csv_name):
+    df = pd.read_csv(csv_name, sep=';')
+    return df
+
+
 # Pulled from Django framework (https://github.com/django/django/blob/master/django/utils/text.py)
 def get_valid_filename(s):
     """
@@ -50,7 +55,7 @@
     for ind, row in df.iterrows():
         png_url = row['image_uris'][size]
         if out_dir == '':
-            out_dir = 'data/png/%s' % row['set']
+            out_dir = 'data/%s/%s' % (size, row['set'])
         if not os.path.exists(out_dir):
             os.makedirs(out_dir)
         img_name = '%s/%s_%s.png' % (out_dir, row['collector_number'], get_valid_filename(row['name']))
@@ -62,7 +67,7 @@
 def main():
     df = fetch_all_cards_text(url='https://api.scryfall.com/cards/search?q=layout:normal+set:rtr+lang:en',
                               csv_name='data/all_cards.csv')
-    fetch_cards_image(df)
+    #fetch_cards_image(df)
     pass
 
 
diff --git a/generate_data.py b/generate_data.py
index a6f1f26..37a25c5 100644
--- a/generate_data.py
+++ b/generate_data.py
@@ -5,7 +5,11 @@
 import math
 import random
 import os
-
+import re
+import cv2
+import fetch_data
+import sys
+import numpy as np
 
 # Referenced from geaxgx's playing-card-detection: https://github.com/geaxgx/playing-card-detection
 class Backgrounds:
@@ -57,10 +61,47 @@
     return bg_images
 
 
+def apply_bounding_box(img, card_info, display=False):
+    # Mana symbol - They are located on the top right side of the card, next to the name.
+    # Their position is stationary, and is right-aligned.
+    has_mana_cost = isinstance(card_info['mana_cost'], str)  # Cards with no mana cost will have nan
+    is_planeswalker = 'Planeswalker' in card_info['type_line']
+    if has_mana_cost:
+        mana_cost = re.findall('\{(.*?)\}', card_info['mana_cost'])
+        x2 = 683
+        if is_planeswalker:
+            y1 = 50
+        else:
+            y1 = 67
+        for i in reversed(range(len(mana_cost))):
+            is_hybrid = '/' in mana_cost[i]
+            if is_hybrid:
+                box = [(x2 - 47, y1 - 8), (x2 + 2, y1 + 43)]  # (x1, y1), (x2, y2)
+                x2 -= 45
+            else:
+                box = [(x2 - 39, y1), (x2, y1 + 41)]  # (x1, y1), (x2, y2)
+                x2 -= 37
+
+            img_symbol = img[box[0][1]:box[1][1], box[0][0]:box[1][0]]
+            if display:
+                cv2.imshow('symbol', img_symbol)
+                cv2.waitKey(0)
+
+
 def main():
     #bg_images = load_dtd()
-    bg = Backgrounds()
-    bg.get_random(display=True)
+    #bg = Backgrounds()
+    #bg.get_random(display=True)
+    df = fetch_data.load_all_cards_text('data/all_cards.csv')
+    #repeat = 'y'
+    while True:
+        rand_card = df.iloc[random.randint(0, df.shape[0] - 1)]
+        card_img = cv2.imread('data/png/%s/%s_%s.png' % (rand_card['set'], rand_card['collector_number'],
+                                                         fetch_data.get_valid_filename(rand_card['name'])))
+        print(rand_card['name'])
+        sys.stdout.flush()
+        apply_bounding_box(card_img, rand_card, display=True)
+        #repeat = input('y to repeat, n to finish')
     return
 
 

--
Gitblit v1.10.0