def main(): random.seed() #bg_images = load_dtd() #bg = Backgrounds() #bg.get_random(display=True) card_pool = pd.DataFrame() for set_name in Config.all_set_list: df = fetch_data.load_all_cards_text('%s/csv/%s.csv' % (Config.data_dir, set_name)) #for _ in range(3): # card_info = df.iloc[random.randint(0, df.shape[0] - 1)] # # Currently ignoring planeswalker cards due to their different card layout # is_planeswalker = 'Planeswalker' in card_info['type_line'] # if not is_planeswalker: # card_pool = card_pool.append(card_info) card_pool = card_pool.append(df) ''' print(card_pool) mana_symbol_set = set() for _, card_info in card_pool.iterrows(): has_mana_cost = isinstance(card_info['mana_cost'], str) if has_mana_cost: mana_cost = re.findall('\{(.*?)\}', card_info['mana_cost']) for symbol in mana_cost: mana_symbol_set.add(symbol) print(mana_symbol_set) ''' for _, card_info in card_pool.iterrows(): img_name = '%s/card_img/png/%s/%s_%s.png' % ( Config.data_dir, card_info['set'], card_info['collector_number'], fetch_data.get_valid_filename(card_info['name'])) print(img_name) card_img = cv2.imread(img_name) 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) detected_object_list = apply_bounding_box(card_img, card_info, display=True) print(detected_object_list) return
def main(args): random.seed() ia.seed(random.randrange(10000)) bg_images = generate_data.load_dtd(dtd_dir='%s/dtd/images' % Config.data_dir, dump_it=False) background = generate_data.Backgrounds(images=bg_images) card_pool = pd.DataFrame() for set_name in Config.all_set_list: df = fetch_data.load_all_cards_text('%s/csv/%s.csv' % (Config.data_dir, set_name)) card_pool = card_pool.append(df) class_ids = {} with open('%s/obj.names' % Config.data_dir) as names_file: class_name_list = names_file.read().splitlines() for i in range(len(class_name_list)): class_ids[class_name_list[i]] = i for i in range(args.num_gen): # Arbitrarily select top left and right corners for perspective transformation # Since the training image are generated with random rotation, don't need to skew all four sides skew = [[random.uniform(0, 0.25), 0], [0, 1], [1, 1], [random.uniform(0.75, 1), 0]] generator = ImageGenerator(background.get_random(), class_ids, args.width, args.height, skew=skew) out_name = '' # Use 2 to 5 cards per generator for _, card_info in card_pool.sample(random.randint(2, 5)).iterrows(): img_name = '%s/card_img/png/%s/%s_%s.png' % ( Config.data_dir, card_info['set'], card_info['collector_number'], fetch_data.get_valid_filename(card_info['name'])) out_name += '%s%s_' % (card_info['set'], card_info['collector_number']) card_img = cv2.imread(img_name) if card_img is None: fetch_data.fetch_card_image( card_info, out_dir='%s/card_img/png/%s' % (Config.data_dir, card_info['set'])) card_img = cv2.imread(img_name) if card_img is None: print('WARNING: card %s is not found!' % img_name) detected_object_list = generate_data.apply_bounding_box( card_img, card_info) card = Card(card_img, card_info, detected_object_list) generator.add_card(card) for j in range(args.num_iter): seq = iaa.Sequential([ iaa.Multiply((0.8, 1.2)), # darken / brighten the whole image iaa.SimplexNoiseAlpha(first=iaa.Add(random.randrange(64)), per_channel=0.1, size_px_max=[3, 6], upscale_method="cubic"), # Lighting iaa.AdditiveGaussianNoise(scale=random.uniform(0, 0.05) * 255, per_channel=0.1), # Noises iaa.Dropout(p=[0, 0.05], per_channel=0.1) # Dropout ]) if i % 3 == 0: generator.generate_non_obstructive() generator.export_training_data( visibility=0.0, out_name='%s/train/non_obstructive_update/%s%d' % (Config.data_dir, out_name, j), aug=seq) elif i % 3 == 1: generator.generate_horizontal_span( theta=random.uniform(-math.pi, math.pi)) generator.export_training_data( visibility=0.0, out_name='%s/train/horizontal_span_update/%s%d' % (Config.data_dir, out_name, j), aug=seq) else: generator.generate_vertical_span( theta=random.uniform(-math.pi, math.pi)) generator.export_training_data( visibility=0.0, out_name='%s/train/vertical_span_update/%s%d' % (Config.data_dir, out_name, j), aug=seq) #generator.generate_horizontal_span(theta=random.uniform(-math.pi, math.pi)) #generator.render(display=True, aug=seq, debug=True) print('Generated %s%d' % (out_name, j)) generator.img_bg = background.get_random() pass
def main(args): # Specify paths for all necessary files pck_path = os.path.abspath('card_pool.pck') if os.path.isfile(pck_path): card_pool = pd.read_pickle(pck_path) else: print('Warning: pickle for card database %s is not found!' % pck_path) # Merge database for all cards, then calculate pHash values of each, store them df_list = [] for set_name in Config.all_set_list: csv_name = '%s/csv/%s.csv' % (Config.data_dir, set_name) df = fetch_data.load_all_cards_text(csv_name) df_list.append(df) card_pool = pd.concat(df_list, sort=True) card_pool.reset_index(drop=True, inplace=True) card_pool.drop('Unnamed: 0', axis=1, inplace=True, errors='ignore') calc_image_hashes(card_pool, save_to=pck_path) ch_key = 'card_hash_%d' % args.hash_size card_pool = card_pool[['name', 'set', 'collector_number', ch_key]] # Processing time is almost linear to the size of the database # Program can be much faster if the search scope for the card can be reduced card_pool = card_pool[card_pool['set'].isin(Config.set_2003_list)] # ImageHash is basically just one numpy.ndarray with (hash_size)^2 number of bits. pre-emptively flattening it # significantly increases speed for subtracting hashes in the future. card_pool[ch_key] = card_pool[ch_key].apply(lambda x: x.hash.flatten()) # If the test file isn't given, use webcam to capture video if args.in_path is None: capture = cv2.VideoCapture(0) detect_video(capture, card_pool, hash_size=args.hash_size, out_path='%s/result.avi' % args.out_path, display=args.display, show_graph=args.show_graph, debug=args.debug) capture.release() else: # Save the detection result if args.out_path is provided if args.out_path is None: out_path = None else: f_name = os.path.split(args.in_path)[1] out_path = '%s/%s.avi' % (args.out_path, f_name[:f_name.find('.')]) if not os.path.isfile(args.in_path): print('The test file %s doesn\'t exist!' % os.path.abspath(args.in_path)) return # Check if test file is image or video test_ext = args.in_path[args.in_path.find('.') + 1:] if test_ext in ['jpg', 'jpeg', 'bmp', 'png', 'tiff']: # Test file is an image img = cv2.imread(args.in_path) detect_frame(img, card_pool, hash_size=args.hash_size, out_path=out_path, display=args.display, debug=args.debug) else: # Test file is a video capture = cv2.VideoCapture(args.in_path) detect_video(capture, card_pool, hash_size=args.hash_size, out_path=out_path, display=args.display, show_graph=args.show_graph, debug=args.debug) capture.release() pass