old mode 100644
new mode 100755
| | |
| | | new_pool = pd.DataFrame(columns=list(card_pool.columns.values)) |
| | | for hs in hash_size: |
| | | new_pool['card_hash_%d' % hs] = np.NaN |
| | | new_pool['set_hash_%d' % hs] = np.NaN |
| | | new_pool['set_hash_%d' % 64] = np.NaN |
| | | #new_pool['art_hash_%d' % hs] = np.NaN |
| | | for ind, card_info in card_pool.iterrows(): |
| | | if ind % 100 == 0: |
| | |
| | | for card_name in card_names: |
| | | # Fetch the image - name can be found based on the card's information |
| | | card_info['name'] = card_name |
| | | cname = card_name |
| | | if cname == 'con': |
| | | cname == 'con__' |
| | | 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'])) |
| | | fetch_data.get_valid_filename(cname)) |
| | | card_img = cv2.imread(img_name) |
| | | |
| | | # If the image doesn't exist, download it from the URL |
| | | if card_img is None: |
| | | set_name = card_info['set'] |
| | | if set_name == 'con': |
| | | set_name = 'con__' |
| | | fetch_data.fetch_card_image(card_info, |
| | | out_dir='%s/card_img/png/%s' % (Config.data_dir, card_info['set'])) |
| | | out_dir='%s/card_img/png/%s' % (Config.data_dir, set_name)) |
| | | card_img = cv2.imread(img_name) |
| | | if card_img is None: |
| | | print('WARNING: card %s is not found!' % img_name) |
| | | continue |
| | | |
| | | set_img = card_img[575:638, 567:700] |
| | | """ |
| | | img_cc = cv2.cvtColor(card_img, cv2.COLOR_BGR2GRAY) |
| | | img_thresh = cv2.adaptiveThreshold(img_cc, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 5) |
| | | # Dilute the image, then erode them to remove minor noises |
| | | kernel = np.ones((3, 3), np.uint8) |
| | | img_dilate = cv2.dilate(img_thresh, kernel, iterations=1) |
| | | img_erode = cv2.erode(img_dilate, kernel, iterations=1) |
| | | cnts, hier = cv2.findContours(img_erode, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
| | | cnts2 = sorted(cnts, key=cv2.contourArea, reverse=True) |
| | | cnts2 = cnts2[:10] |
| | | if True: |
| | | cv2.drawContours(img_cc, cnts2, -1, (0, 255, 0), 3) |
| | | #cv2.imshow('Contours', card_img) |
| | | #cv2.waitKey(10000) |
| | | """ |
| | | set_img = card_img[595:635, 600:690] |
| | | #cv2.imshow(card_info['name'], set_img) |
| | | # Compute value of the card's perceptual hash, then store it to the database |
| | | #img_art = Image.fromarray(card_img[121:580, 63:685]) # For 745*1040 size card image |
| | | img_card = Image.fromarray(card_img) |
| | | img_set = Image.fromarray(set_img) |
| | | #cv2.imshow('Set' + card_names[0], set_img) |
| | | for hs in hash_size: |
| | | card_hash = ih.phash(img_card, hash_size=hs) |
| | | set_hash = ih.whash(img_set, hash_size=hs) |
| | | set_hash = ih.whash(img_set, hash_size=64) |
| | | card_info['card_hash_%d' % hs] = card_hash |
| | | card_info['set_hash_%d' % hs] = set_hash |
| | | card_info['set_hash_%d' % 64] = set_hash |
| | | #print('Setting set_hash_%d' % hs) |
| | | #art_hash = ih.phash(img_art, hash_size=hs) |
| | | #card_info['art_hash_%d' % hs] = art_hash |
| | |
| | | elif isinstance(hash_size, int): |
| | | hash_size = [hash_size] |
| | | |
| | | num_cores = 15 |
| | | num_partitions = round(card_pool.shape[0]/100) |
| | | num_cores = 16 |
| | | num_partitions = round(card_pool.shape[0]/1000) |
| | | if num_partitions < min(num_cores, card_pool.shape[0]): |
| | | num_partitions = min(num_cores, card_pool.shape[0]) |
| | | pool = Pool(num_cores) |
| | |
| | | # Find the contour |
| | | cnts, hier = cv2.findContours(img_erode, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
| | | if len(cnts) == 0: |
| | | #print('no contours') |
| | | print('no contours') |
| | | return [] |
| | | img_cont = cv2.cvtColor(img_erode, cv2.COLOR_GRAY2BGR) |
| | | img_cont_base = img_cont.copy() |
| | |
| | | size = cv2.contourArea(cnt) |
| | | peri = cv2.arcLength(cnt, True) |
| | | approx = cv2.approxPolyDP(cnt, 0.04 * peri, True) |
| | | print('Base Size:', size) |
| | | print('Len Approx:', len(approx)) |
| | | if size >= size_thresh and len(approx) == 4: |
| | | # lets see if we got a contour very close in size as child |
| | | if i_child != -1: |
| | |
| | | c_cnt = c_list[0] # the biggest child |
| | | if debug: |
| | | cv2.drawContours(img_ccont, c_list[:1], -1, (0, 255, 0), 1) |
| | | cv2.imshow('CCont %d' % i_cnt, img_ccont) |
| | | cv2.imshow('CCont', img_ccont) |
| | | c_size = cv2.contourArea(c_cnt) |
| | | c_approx = cv2.approxPolyDP(c_cnt, 0.04 * peri, True) |
| | | if len(c_approx) == 4 and (c_size/size) > 0.85: |
| | |
| | | return img_graph |
| | | |
| | | |
| | | def detect_frame(img, card_pool, hash_size=32, size_thresh=100000, |
| | | def detect_frame(img, card_pool, hash_size=32, size_thresh=10000, |
| | | out_path=None, display=True, debug=False): |
| | | """ |
| | | Identify all cards in the input frame, display or save the frame if needed |
| | |
| | | det_cards = [] |
| | | # Detect contours of all cards in the image |
| | | cnts = find_card(img_result, size_thresh=size_thresh, debug=debug) |
| | | print('Countours:', len(cnts)) |
| | | for i in range(len(cnts)): |
| | | print('Contour', i) |
| | | cnt = cnts[i] |
| | | # For the region of the image covered by the contour, transform them into a rectangular image |
| | | pts = np.float32([p[0] for p in cnt]) |
| | |
| | | img_set_part = img_warp[cut[0]:cut[1], cut[2]:cut[3]] |
| | | print(img_set_part.shape) |
| | | img_set = Image.fromarray(img_set_part.astype('uint8'), 'RGB') |
| | | print('img set') |
| | | if debug: |
| | | cv2.imshow("Set Img#%d" % i, img_set_part) |
| | | |
| | |
| | | print('Idx:', ix, 'Name:', cd['name'], 'Set:', cd['set'], 'Diff:', top_matches[ix]) |
| | | |
| | | |
| | | cd_data['set_hash_diff'] = cd_data['set_hash_%d' % hash_size] |
| | | cd_data['set_hash_diff'] = cd_data['set_hash_%d' % 64] |
| | | cd_data['set_hash_diff'] = cd_data['set_hash_diff'].apply(lambda x: np.count_nonzero(x != set_img_hash)) |
| | | conf = sorted(cd_data['set_hash_diff']) |
| | | print('Confs:', conf) |
| | |
| | | cv2.imshow('card#%d' % i, img_warp) |
| | | if display: |
| | | cv2.imshow('Result', img_result) |
| | | cv2.waitKey(0) |
| | | inp = cv2.waitKey(0) |
| | | |
| | | if out_path is not None: |
| | | print(out_path) |
| | | cv2.imwrite(out_path, img_result.astype(np.uint8)) |
| | | return det_cards, img_result |
| | | |
| | |
| | | print('Elapsed time: %.2f ms' % elapsed_ms) |
| | | if out_path is not None: |
| | | vid_writer.write(img_save.astype(np.uint8)) |
| | | cv2.waitKey(1) |
| | | inp = cv2.waitKey(0) |
| | | if 'q' == chr(inp & 255): |
| | | break |
| | | except KeyboardInterrupt: |
| | | capture.release() |
| | | if out_path is not None: |
| | |
| | | # Merge database for all cards, then calculate pHash values of each, store them |
| | | df_list = [] |
| | | for set_name in Config.all_set_list: |
| | | if set_name == 'con': |
| | | set_name = 'con__' |
| | | 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.drop('Unnamed: 0', axis=1, inplace=True, errors='ignore') |
| | | card_pool = calc_image_hashes(card_pool, save_to=pck_path, hash_size=hash_sizes) |
| | | ch_key = 'card_hash_%d' % args.hash_size |
| | | set_key = 'set_hash_%d' % args.hash_size |
| | | set_key = 'set_hash_%d' % 64 |
| | | if ch_key not in card_pool.columns: |
| | | # we did not generate this hash_size yet |
| | | print('We need to add hash_size=%d' % (args.hash_size,)) |
| | |
| | | capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG")) |
| | | capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) |
| | | capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) |
| | | |
| | | thres = int(((1920-2*500)*(1080-2*200)*0.3)) |
| | | print('Threshold:', thres) |
| | | 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, crop_x=500, crop_y=200) |
| | | display=args.display, show_graph=args.show_graph, debug=args.debug, crop_x=500, crop_y=200, size_thresh=thres) |
| | | capture.release() |
| | | else: |
| | | # Save the detection result if args.out_path is provided |
| | |
| | | if test_ext in ['jpg', 'jpeg', 'bmp', 'png', 'tiff']: |
| | | # Test file is an image |
| | | img = cv2.imread(args.in_path) |
| | | if img is None: |
| | | print('Could not read', args.in_path) |
| | | detect_frame(img, card_pool, hash_size=args.hash_size, out_path=out_path, display=args.display, |
| | | debug=args.debug) |
| | | else: |