def to_txt(txt_path, image_name, image_data, pixel_pos_scores, link_pos_scores): # write detection result as txt files def write_result_as_txt(image_name, bboxes, path): filename = util.io.join_path(path, 'res_%s.txt'%(image_name)) lines = [] for b_idx, bbox in enumerate(bboxes): values = [int(v) for v in bbox] line = "%d, %d, %d, %d, %d, %d, %d, %d\n"%tuple(values) lines.append(line) util.io.write_lines(filename, lines) mask = pixel_link.decode_batch(pixel_pos_scores, link_pos_scores)[0, ...] bboxes = pixel_link.mask_to_bboxes(mask, image_data.shape) write_result_as_txt(image_name, bboxes, txt_path)
def to_txt(txt_path, image_name, image_data, pixel_pos_scores, link_pos_scores): # write detection result as txt files def write_result_as_txt(image_name, bboxes, path): filename = util.io.join_path(path, 'res_%s.txt'%(image_name)) lines = [] for b_idx, bbox in enumerate(bboxes): values = [int(v) for v in bbox] line = "%d, %d, %d, %d, %d, %d, %d, %d\n"%tuple(values) lines.append(line) util.io.write_lines(filename, lines) print 'result has been written to:', filename mask = pixel_link.decode_batch(pixel_pos_scores, link_pos_scores)[0, ...] bboxes = pixel_link.mask_to_bboxes(mask, image_data.shape) write_result_as_txt(image_name, bboxes, txt_path)
def to_txt(txt_path, image_name, image_data, pixel_pos_scores, link_pos_scores): # write detection result as txt files def write_result_as_pred_txt(image_name, bboxes, bboxes_score): filename = util.io.join_path(FLAGS.pred_path, '%s.txt' % image_name) lines = [] for b_idx, (bbox, bbox_score) in enumerate(zip(bboxes, bboxes_score)): min_x = np.min([bbox[0], bbox[2], bbox[4], bbox[6]]) max_x = np.max([bbox[0], bbox[2], bbox[4], bbox[6]]) min_y = np.min([bbox[1], bbox[3], bbox[5], bbox[7]]) max_y = np.max([bbox[1], bbox[3], bbox[5], bbox[7]]) lines.append('CYST %.4f %d %d %d %d\n' % (bbox_score, min_x, min_y, max_x, max_y)) util.io.write_lines(filename, lines) print('result has been written to: ', filename) def write_result_as_txt(image_name, bboxes, path): filename = util.io.join_path(path, 'res_%s.txt' % (image_name)) lines = [] pred_lines = [] for b_idx, bbox in enumerate(bboxes): values = [int(v) for v in bbox] line = "%d, %d, %d, %d, %d, %d, %d, %d\n" % tuple(values) lines.append(line) util.io.write_lines(filename, lines) print('result has been written to:', filename) # 其实只有一个image, [1, W, H, C] print('the shape of pixel_pos_scores is ', np.shape(pixel_pos_scores), np.min(pixel_pos_scores), np.max(pixel_pos_scores)) mask = pixel_link.decode_batch(pixel_pos_scores, link_pos_scores)[0, ...] bboxes, bboxes_score, pixel_pos_scores = pixel_link.mask_to_bboxes( mask, pixel_pos_scores, image_data.shape) print('the shape of pixel_pos_scores is ', np.shape(pixel_pos_scores), np.min(pixel_pos_scores), np.max(pixel_pos_scores)) score_map_path = util.io.join_path(FLAGS.score_map_path, '%s.jpg' % image_name) cv2.imwrite(score_map_path, np.asarray(pixel_pos_scores * 255, np.uint8)) print('score will be written in ', score_map_path) write_result_as_txt(image_name, bboxes, txt_path) write_result_as_pred_txt(image_name, bboxes, bboxes_score)
def main(_): pb_file = 'e:/ocr/pixel_link.pb' image_path = 'd:/tt/TextVOC/JPEGImages/000000.jpg' with tf.Graph().as_default(): graph_def = tf.GraphDef() with tf.gfile.FastGFile(pb_file, mode='rb') as f: graph_def.ParseFromString(f.read()) tf.import_graph_def(graph_def) config.init_config((512, 512)) with tf.Session() as sess: # with tf.summary.FileWriter(logdir='e:/ocr/logdir', graph=sess.graph) as w: # w.flush() input_image = sess.graph.get_tensor_by_name('import/input_image:0') pixel_pos_scores = sess.graph.get_tensor_by_name( 'import/pixel_pos_scores:0') link_pos_scores = sess.graph.get_tensor_by_name( 'import/link_pos_scores:0') img = cv2.imread(image_path) pps, lps = sess.run([pixel_pos_scores, link_pos_scores], feed_dict={input_image: img}) mask = pixel_link.decode_batch(pps, lps)[0, ...] bboxes = pixel_link.mask_to_bboxes(mask, img.shape) print(bboxes) for points in bboxes: points = np.reshape(points, (4, 2)) cnts = util.img.points_to_contours(points) util.img.draw_contours(img, cnts, -1, color=util.img.COLOR_GREEN, border_width=3) cv2.imwrite('d:/tt/000000.jpg', img)
def to_txt(txt_path, image_name, image_data, pixel_pos_scores, link_pos_scores): # write detection result as txt files def write_result_as_txt(image_name, bboxes, path): filename = util.io.join_path(path, 'res_%s.txt' % (image_name)) lines = [] for b_idx, bbox in enumerate(bboxes): values = [int(v) for v in bbox] line = "%d, %d, %d, %d, %d, %d, %d, %d\n" % tuple(values) lines.append(line) util.io.write_lines(filename, lines) print 'result has been written to:', filename start_time = time() mask = pixel_link.decode_batch(pixel_pos_scores, link_pos_scores)[0, ...] bboxes = pixel_link.mask_to_bboxes(mask, image_data.shape) end_time = time() print(image_name, ' cost time: ', end_time - start_time) # ## # output_path = os.path.join(os.getcwd(), 'vis_result') # to_mask(image_name, mask, output_path) # ## write_result_as_txt(image_name, bboxes, txt_path)
def get_bboxes(mask): return pixel_link.mask_to_bboxes(mask, image_data.shape)
def to_txt_mask(txt_path, image_name, image_data, pixel_pos_scores, link_pos_scores, pixel_seg_score): # write detection result as txt files def write_result_as_pred_txt(image_name, bboxes, bboxes_score, pixel_wise_category): from config import pixel2type def compute_the_category(pixel_wise_category, min_x, max_x, min_y, max_y, class_num=5): pixel_num = [] cropped = pixel_wise_category[min_x:max_x, min_y:max_y] for i in range(1, class_num + 1): pixel_num.append(np.sum(cropped == i)) return np.argmax(pixel_num) + 1 filename = util.io.join_path(FLAGS.pred_path, '%s.txt' % image_name) lines = [] for b_idx, (bbox, bbox_score) in enumerate(zip(bboxes, bboxes_score)): min_x = np.min([bbox[0], bbox[2], bbox[4], bbox[6]]) max_x = np.max([bbox[0], bbox[2], bbox[4], bbox[6]]) min_y = np.min([bbox[1], bbox[3], bbox[5], bbox[7]]) max_y = np.max([bbox[1], bbox[3], bbox[5], bbox[7]]) label_idx = compute_the_category(pixel_wise_category, min_x, max_x, min_y, max_y) label_name = pixel2type[label_idx * 50] lines.append('%s %.4f %d %d %d %d\n' % (label_name, bbox_score, min_x, min_y, max_x, max_y)) util.io.write_lines(filename, lines) print('result has been written to: ', filename) def write_result_as_txt(image_name, bboxes, path): filename = util.io.join_path(path, 'res_%s.txt' % (image_name)) lines = [] pred_lines = [] for b_idx, bbox in enumerate(bboxes): values = [int(v) for v in bbox] line = "%d, %d, %d, %d, %d, %d, %d, %d\n" % tuple(values) lines.append(line) util.io.write_lines(filename, lines) print('result has been written to:', filename) pixel_seg_score = util.img.resize(img=pixel_seg_score[0], size=image_data.shape[:2]) pixel_wise_category = np.argmax(pixel_seg_score, axis=-1) # 其实只有一个image, [1, W, H, C] mask = pixel_link.decode_batch(pixel_pos_scores, link_pos_scores)[0, ...] bboxes, bboxes_score, pixel_pos_scores = pixel_link.mask_to_bboxes( mask, pixel_pos_scores, image_data.shape) print('the shape of pixel_pos_scores is ', np.shape(pixel_pos_scores), np.min(pixel_pos_scores), np.max(pixel_pos_scores)) print('the shape of pixel_wise_category is ', np.shape(pixel_wise_category), np.min(pixel_wise_category), np.max(pixel_wise_category)) score_map_path = util.io.join_path(FLAGS.score_map_path, '%s.jpg' % image_name) seg_map_path = util.io.join_path(FLAGS.seg_map_path, '%s.png' % image_name) cv2.imwrite(score_map_path, np.asarray(pixel_pos_scores * 255, np.uint8)) cv2.imwrite(seg_map_path, np.asarray(pixel_wise_category * 50, np.uint8)) print('score will be written in ', score_map_path) write_result_as_txt(image_name, bboxes, txt_path) write_result_as_pred_txt(image_name, bboxes, bboxes_score, pixel_wise_category)
def to_bboxes(image_data, pixel_pos_scores, link_pos_scores): link_pos_scores=np.transpose(link_pos_scores,(0,2,3,1)) mask = decode_batch(pixel_pos_scores, link_pos_scores,0.6,0.9)[0, ...] bboxes = mask_to_bboxes(mask, image_data.shape) return mask,bboxes
def _get_bbox(self, image_data, pixel_pos_scores, link_pos_scores): mask = pixel_link.decode_batch(pixel_pos_scores, link_pos_scores)[0, ...] bboxes = pixel_link.mask_to_bboxes(mask, image_data.shape) return bboxes