From f176d3da3d4dd7a337187739cf9446417b523156 Mon Sep 17 00:00:00 2001
From: Edmond Yoo <hj3yoo@uwaterloo.ca>
Date: Tue, 28 Aug 2018 23:35:47 +0000
Subject: [PATCH] skeleton for data transformation
---
transform_data.py | 112 +++++++++++++++++++++++++++++++++++++
generate_data.py | 16 +++--
2 files changed, 121 insertions(+), 7 deletions(-)
diff --git a/generate_data.py b/generate_data.py
index b7525dd..d3e7d4e 100644
--- a/generate_data.py
+++ b/generate_data.py
@@ -11,6 +11,7 @@
import sys
import numpy as np
import pandas as pd
+from transform_data import ExtractedObject
# Referenced from geaxgx's playing-card-detection: https://github.com/geaxgx/playing-card-detection
class Backgrounds:
@@ -63,8 +64,9 @@
def apply_bounding_box(img, card_info, display=False):
- # List of (object class, bounding box pts) pair of each objects
- object_info_list = []
+ # List of detected objects to be fed into the neural net
+ # The first object is the entire card
+ detected_object_list = [ExtractedObject('card', [(0, 0), (len(img[0]), 0), (len(img[0]), len(img)), (0, len(img))])]
# 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
@@ -95,7 +97,7 @@
# Append them to the list of bounding box with the appropriate label
symbol_name = 'mana_symbol:' + mana_cost[i]
key_pts = [(x1, y1), (x2, y1), (x2, y2), (x1, y2)]
- object_info_list.append((symbol_name, key_pts))
+ detected_object_list.append(ExtractedObject(symbol_name, key_pts))
if display:
img_symbol = img[y1:y2, x1:x2]
@@ -160,7 +162,7 @@
# Append them to the list of bounding box with the appropriate label
symbol_name = 'set_symbol:' + card_info['set']
key_pts = [(x1, y1), (x2, y1), (x2, y2), (x1, y2)]
- object_info_list.append((symbol_name, key_pts))
+ detected_object_list.append(ExtractedObject(symbol_name, key_pts))
if display:
img_symbol = img[y1:y2, x1:x2]
@@ -175,7 +177,7 @@
# Image box - the large image on the top half of the card
# TODO
- return object_info_list
+ return detected_object_list
def main():
@@ -204,8 +206,8 @@
if card_img is None:
fetch_data.fetch_card_image(card_info, out_dir='../usb/data/png/%s' % card_info['set'])
card_img = cv2.imread(img_name)
- object_list_info = apply_bounding_box(card_img, card_info, display=True)
- print(object_list_info)
+ detected_object_list = apply_bounding_box(card_img, card_info, display=True)
+ print(detected_object_list)
return
diff --git a/transform_data.py b/transform_data.py
new file mode 100644
index 0000000..3923cac
--- /dev/null
+++ b/transform_data.py
@@ -0,0 +1,112 @@
+
+
+class ImageGenerator:
+ """
+ A template for generating a training image.
+ """
+ def __init__(self, img_bg, cards):
+ """
+ :param img_bg: background (textile) image
+ :param cards: list of Card objects
+ """
+ self._img_bg = img_bg
+ self._cards = cards
+ self._img_result = None
+ pass
+
+ def generate_horizontal_span(self):
+ """
+ Generating the first scenario where the cards are laid out in a straight horizontal line
+ :return: none
+ """
+ pass
+
+ def generate_vertical_span(self):
+ """
+ Generating the second scenario where the cards are laid out in a straight vertical line
+ :return: none
+ """
+ pass
+
+ def generate_fan_out(self):
+ """
+ Generating the third scenario where the cards are laid out in a fan shape
+ :return: none
+ """
+ pass
+
+ def export_training_data(self, out_dir):
+ """
+ Export the generated training image along with the txt file for all bounding boxes
+ :return: none
+ """
+ pass
+
+
+class Card:
+ """
+ A class for storing required information about a card in relation to the ImageGenerator
+ """
+ def __init__(self, img, card_info, objects, generator=None, x=None, y=None, theta=None):
+ """
+ :param img: image of the card
+ :param card_info: details like name, mana cost, type, set, etc
+ :param objects: list of ExtractedObjects like mana & set symbol, etc
+ :param generator: ImageGenerator object that the card is bound to
+ :param x: X-coordinate of the card's centre in relation to the generator
+ :param y: Y-coordinate of the card's centre in relation to the generator
+ :param theta: angle of rotation of the card in relation to the generator
+ """
+ self._img = img
+ self._info = card_info
+ self._objects = objects
+ self._x = x
+ self._y = y
+ self._theta = theta
+ pass
+
+ def bind_to_generator(self, generator, x=0, y=0, theta=0):
+ """
+ Bind this card to an ImageGenerator object.
+ :param generator: generator to be bound with
+ :param x: new X-coordinate for the centre of the card
+ :param y: new Y-coordinate for the centre of the card
+ :param theta: new angle for the card
+ :return: none
+ """
+ pass
+
+ def shift(self, x=None, y=None):
+ """
+ Apply a X/Y translation on this image
+ :param x: amount of X-translation. If range is given, translate by a random amount within that range
+ :param y: amount of Y-translation. Refer to x when a range is given.
+ :return: none
+ """
+ pass
+
+ def rotate(self, centre, theta=None):
+ """
+ Apply a rotation on this image with a centre
+ :param centre: coordinate of the centre of the rotation in relation to the centre of this card (self._x, self._y)
+ :param theta: amount of rotation in radian. If a range is given, rotate by a random amount within that range
+ :return: none
+ """
+ pass
+
+
+class ExtractedObject:
+ """
+ Simple struct to hold information about an extracted object
+ """
+ def __init__(self, label, key_pts):
+ self._label = label
+ self._key_pts = key_pts
+
+
+def main():
+ pass
+
+
+if __name__ == '__main__':
+ main()
\ No newline at end of file
--
Gitblit v1.10.0