def create_composite_image_from_filedata(crop_dir, data_dir, crop_size, rows, cols, filedata): html_dir = data_dir + 'html/' images = [] for image_id, filename, angle_offset in filedata: crop = ci.get_rotated_crop(crop_dir, image_id, crop_size, angle_offset) images.append(crop) composite_image = ci.get_composite_image(images, rows, cols) cv2.imwrite(html_dir + 'composite_image.png', composite_image)
def create_composite_image_ground_truth_dates(crop_dir, data_dir, crop_size, rows, cols, coin_angles, ground_truth_dates): html_dir = data_dir + 'html/' clean_dir(html_dir) images_by_seed = {} image_ids = [] for seed_id, coin_id, result, labeled_date, bad_angle, bad_image in ground_truth_dates: image_id = (coin_id * 100) + 54 if not seed_id in images_by_seed.iterkeys(): images_by_seed[seed_id] = [] angle = 0 if coin_id in coin_angles.iterkeys(): angle = coin_angles[coin_id] images_by_seed[seed_id].append([image_id, result, angle]) for seed_image_id, values in images_by_seed.iteritems(): images = [] rotated_crop = ci.get_rotated_crop(crop_dir, get_image_id(seed_image_id, False), crop_size, 0) if rotated_crop is None: continue images.append(rotated_crop) sorted_results = sorted(values, key=lambda result: result[1], reverse=True) for image_id, max_value, angle in sorted_results: crop = ci.get_rotated_crop(crop_dir, get_image_id(image_id, False), crop_size, angle) font = cv2.FONT_HERSHEY_SIMPLEX if (image_id / 100) * 100 == seed_image_id: cv2.putText(crop, 'SEED', (4, 55), font, .7, (0, 255, 0), 2) cv2.putText(crop, str(max_value)[0:5], (4, 20), font, .7, (0, 255, 0), 2) cv2.putText(crop, str(get_image_id(image_id, False) / 100)[0:4], (4, 90), font, .7, (0, 255, 0), 2) images.append(crop) calc_rows = int(len(images) / cols) + 1 if rows >= calc_rows: rows_to_pass = calc_rows else: rows_to_pass = rows composite_image = ci.get_composite_image(images, rows_to_pass, cols) cv2.imwrite(html_dir + str(seed_image_id).zfill(8) + '.png', composite_image) image_ids.append(str(seed_image_id).zfill(8)) create_html(sorted(image_ids), html_dir + 'index.html') return
def create_composite_image(crop_dir, data_dir, crop_size, rows, cols, seed_image_ids): html_dir = data_dir + 'html/' images = [] for seed_image_id in seed_image_ids: crop = ci.get_rotated_crop(crop_dir, seed_image_id, crop_size, 0) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(crop, str(seed_image_id)[0:5], (10, 90), font, .7, (0, 255, 0), 2) cv2.circle(crop, (crop_size / 2, crop_size / 2), 2, (0, 0, 255), 1) images.append(crop) composite_image = ci.get_composite_image(images, rows, cols) cv2.imwrite(html_dir + 'composite_image.png', composite_image)
def create_composite_image_coin_id(coin_id, crop_dir, data_dir): images = [] images_gif = [] for id in range(0, 56): image_id = coin_id * 100 + id crop = ci.get_rotated_crop(crop_dir, image_id, 56, 0) images.append(crop) filename = ci.get_filename_from(image_id, crop_dir) images_gif.append(imageio.imread(filename)) composite_image = ci.get_composite_image(images, 8, 8) cv2.imwrite(data_dir + str(coin_id) + '.png', composite_image) imageio.mimsave(data_dir + str(coin_id) + '.gif', images_gif) return
def create_composite_image_total_result(crop_dir, data_dir, crop_size, rows, cols, coin_angles, total_coin_results): html_dir = data_dir + 'html/' clean_dir(html_dir) results = results_dict coin_id_seed_id = {} for seed_image_id, seed_values in results.iteritems(): images = [] results = [] coin_results = {} seed_coin_results = total_coin_results[seed_image_id] for coin_id in seed_coin_results.iterkeys(): image_id = (coin_id * 100) + 54 if coin_id in seed_coin_results.iterkeys(): if coin_id in coin_angles.iterkeys(): results.append([ image_id, seed_coin_results[coin_id], coin_angles[coin_id] ]) sorted_results = sorted(results, key=lambda result: result[1], reverse=True) for image_id, max_value, angle in sorted_results: crop = ci.get_rotated_crop(crop_dir, image_id, crop_size, angle) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(crop, str(max_value)[0:5], (4, 20), font, .7, (0, 255, 0), 2) cv2.putText(crop, str(image_id)[0:6], (4, 90), font, .7, (0, 255, 0), 2) images.append(crop) calc_rows = int(len(images) / cols) + 1 if rows >= calc_rows: rows_to_pass = calc_rows else: rows_to_pass = rows composite_image = ci.get_composite_image(images, rows, cols) cv2.imwrite(html_dir + str(seed_image_id).zfill(5) + '.png', composite_image) return
def create_date_composite_image(crop_dir, data_dir, seed_image_id, max_images, coin_angles, ground_truth_designs, only_show_one_image, save_files): #todo:OK how are heads_date_angle and date_center_offset releated? #I think this is only one angle. html_dir = data_dir + 'html/' image_size = 448 crop_radius = 28 heads_date_angle = 220 #date_center_offset = 166 date_center_offset = 172 # This is the angle seed 100 was imaged at: seed_id_100_angle = 75 # closer up for translation: # crop_radius = 28 # heads_date_angle = 158 # date_center_offset = 166 # This is the angle seed 100 was imaged at: # seed_id_100_angle = 148 seed_values = results_dict[seed_image_id] images = [] results = [] if save_files: sorted_results = [] for coin_id, angle in coin_angles.iteritems(): if ground_truth_designs[coin_id][0] == seed_image_id: for image_id in range(0, 57): sorted_results.append([(coin_id * 100) + image_id, 0, angle]) else: for image_id, values in seed_values.iteritems(): max_value, angle = values coin_id = image_id / 100 if coin_id in coin_angles: results.append([image_id, max_value, angle]) sorted_results = sorted(results, key=lambda result: result[0], reverse=False) count = 0 for image_id, max_value, angle in sorted_results: if only_show_one_image: if image_id % 54 != 0: continue if count >= max_images: continue coin_id = image_id / 100 coin_angle = coin_angles[coin_id] #print angle, coin_angle filename = ci.get_filename_from(image_id, crop_dir) crop = cv2.imread(filename) if crop == None: print 'None?:', filename continue center_x = image_size / 2 + math.cos( math.radians(heads_date_angle + coin_angle)) * date_center_offset center_y = image_size / 2 - math.sin( math.radians(heads_date_angle + coin_angle)) * date_center_offset #print image_id, max_value, coin_angle, center_x, center_y date_crop = crop[center_x - crop_radius:center_x + crop_radius, center_y - crop_radius:center_y + crop_radius] rotated_date_crop = ci.rotate(date_crop, coin_angle - seed_id_100_angle, crop_radius, crop_radius, crop_radius * 2, crop_radius * 2) crop_rows, crop_cols, channels = rotated_date_crop.shape if crop_rows != crop_cols: print image_id, crop_rows, crop_cols rotated_date_crop = cv2.resize(rotated_date_crop, (crop_radius * 2, crop_radius * 2), interpolation=cv2.INTER_AREA) if save_files: dir = '/home/pkrush/cent-dates/' + str(coin_id / 100) + '/' if not os.path.exists(dir): os.makedirs(dir) cv2.imwrite(dir + str(image_id).zfill(7) + '.png', rotated_date_crop) else: font = cv2.FONT_HERSHEY_SIMPLEX #cv2.putText(rotated_date_crop, str(image_id)[0:5], (10, 90), font, .7, (0, 255, 0), 2) cv2.circle(rotated_date_crop, (crop_radius, crop_radius), 1, (0, 0, 255), 1) images.append(rotated_date_crop) count += 1 if not save_files: calc_rows = int(len(images) / 10) + 1 composite_image = ci.get_composite_image(images, calc_rows, 20) cv2.imwrite(html_dir + 'dates.png', composite_image) print count, 'Date images written'
def create_composite_images(crop_dir, data_dir, crop_size, rows, cols, seed_image_ids=None, remove_image_ids=None, use_only_best_coin_image=False): html_dir = data_dir + 'html/' clean_dir(html_dir) if seed_image_ids is None: results = results_dict else: results = { seed_image_id: results_dict[seed_image_id] for seed_image_id in seed_image_ids } if remove_image_ids is None: remove_image_ids = [] coin_id_seed_id = {} for seed_image_id, seed_values in results.iteritems(): images = [] #Get the seed image first: rotated_crop = ci.get_rotated_crop(crop_dir, seed_image_id, crop_size, 0) if rotated_crop is None: continue images.append(rotated_crop) results = [] coin_results = {} if use_only_best_coin_image: for image_id, values in seed_values.iteritems(): max_value, angle = values if image_id not in remove_image_ids: coin_id = image_id / 100 if coin_id not in coin_results.iterkeys(): coin_results[coin_id] = [] coin_results[coin_id].append([image_id, max_value, angle]) for coin_id, values in coin_results.iteritems(): image_id, max_value, angle = max(values, key=lambda item: item[1]) # for Dates: if 10 > angle or 350 < angle: results.append([image_id, max_value, angle]) coin_id_seed_id[coin_id] = [ seed_image_id / 100, image_id, max_value, angle ] else: for image_id, values in seed_values.iteritems(): max_value, angle = values if image_id not in remove_image_ids: results.append([image_id, max_value, angle]) sorted_results = sorted(results, key=lambda result: result[1], reverse=True) for image_id, max_value, angle in sorted_results: crop = ci.get_rotated_crop(crop_dir, image_id, crop_size, angle) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(crop, str(max_value)[0:5], (4, 20), font, .7, (0, 255, 0), 2) cv2.putText(crop, str(image_id)[0:6], (4, 90), font, .7, (0, 255, 0), 2) images.append(crop) calc_rows = int(len(images) / cols) + 1 if rows >= calc_rows: rows_to_pass = calc_rows else: rows_to_pass = rows composite_image = ci.get_composite_image(images, rows, cols) cv2.imwrite(html_dir + str(seed_image_id).zfill(5) + '.png', composite_image) return #todo ground_truth should be somewhere else: ground_truth_coin_ids = pickle.load( open(data_dir + 'ground_truth_coin_ids.pickle', "rb")) misclassify_count = 0 seeds = [] seed_totals = {} predicted_totals = {} for coin_id in range(0, 306): if ground_truth_coin_ids[coin_id][0] not in seeds: seeds.append(ground_truth_coin_ids[coin_id][0]) seed_totals[ground_truth_coin_ids[coin_id][0]] = 0 predicted_totals[ground_truth_coin_ids[coin_id][0]] = 0 if coin_id not in coin_id_seed_id.iterkeys(): # 204 is just the bad dump coin_id for now. # Wow this is sloppy code! It's hard when you can't plan it out. coin_id_seed_id[coin_id] = [204, coin_id * 100, 0, 0] print 'Coin Missing', coin_id misclassify_count += 1 else: if coin_id_seed_id[coin_id][0] <> ground_truth_coin_ids[coin_id][0]: print 'Wrong Seed for ', coin_id misclassify_count += 1 seeds.sort() print 'Misclassify Count:', misclassify_count confusion_matrix = '\t\t\t' for ground_truth_seed_id in seeds: confusion_matrix += str(ground_truth_seed_id) + '\t' confusion_matrix += 'Actual\n' for ground_truth_seed_id in seeds: total = 0 line = 'Actual:\t' + str(ground_truth_seed_id) for coin_id in range(0, 306): if ground_truth_coin_ids[coin_id][0] == ground_truth_seed_id: seed_totals[coin_id_seed_id[coin_id][0]] += 1 predicted_totals[coin_id_seed_id[coin_id][0]] += 1 total += 1 for coin_id in seeds: line += '\t' + str(seed_totals[coin_id]) seed_totals[coin_id] = 0 confusion_matrix += line + '\t\t' + str(total) + '\n' total = 0 line = 'Pred Total:' for coin_id in seeds: line += '\t' + str(predicted_totals[coin_id]) total += predicted_totals[coin_id] line += '\t\t' + str(total) print confusion_matrix + line