analysis_cloze.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # @Author : lightXu
  2. # @File : analysis_cloze.py
  3. import time
  4. import numpy as np
  5. import cv2
  6. from segment.sheet_resolve.lib.model.test import im_detect
  7. from segment.sheet_resolve.lib.model.nms_wrapper import nms
  8. from segment.sheet_resolve.lib.utils.timer import Timer
  9. from segment.sheet_resolve.tools import utils
  10. def analysis_single_image_with_regions(analysis_type, classes, sess, net,
  11. im, conf_thresh, mns_thresh,
  12. coordinate_bias_dict):
  13. """Detect object classes in an image using pre-computed object proposals."""
  14. size = im.shape
  15. # Detect all object classes and regress object bounds
  16. timer = Timer()
  17. timer.tic()
  18. im, ratio = utils.img_resize(analysis_type, im)
  19. scores, boxes = im_detect(analysis_type, sess, net, im)
  20. timer.toc()
  21. print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0]))
  22. content_list = []
  23. analysis_cls_list = []
  24. for cls_ind, cls in enumerate(classes[1:]): # classes
  25. cls_ind += 1 # because we skipped background
  26. cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
  27. cls_scores = scores[:, cls_ind]
  28. dets = np.hstack((cls_boxes,
  29. cls_scores[:, np.newaxis])).astype(np.float32)
  30. keep = nms(dets, mns_thresh)
  31. dets = dets[keep, :]
  32. # vis_detections(im, cls, dets, ax, thresh=conf_thresh)
  33. inds = np.where(dets[:, -1] >= conf_thresh)[0]
  34. if len(inds) > 0:
  35. if cls in list(coordinate_bias_dict.keys()):
  36. xmin_bias = coordinate_bias_dict[cls]['xmin_bias']
  37. ymin_bias = coordinate_bias_dict[cls]['ymin_bias']
  38. xmax_bias = coordinate_bias_dict[cls]['xmax_bias']
  39. ymax_bias = coordinate_bias_dict[cls]['ymax_bias']
  40. else:
  41. xmin_bias = 0
  42. ymin_bias = 0
  43. xmax_bias = 0
  44. ymax_bias = 0
  45. for i in inds:
  46. bbox = dets[i, :4]
  47. score = '{:.4f}'.format(dets[i, -1])
  48. xmin = int(int(bbox[0]) * ratio[0]) + xmin_bias
  49. ymin = int(int(bbox[1]) * ratio[1]) + ymin_bias
  50. xmax = int(int(bbox[2]) * ratio[0]) + xmax_bias
  51. ymax = int(int(bbox[3]) * ratio[1]) + ymax_bias
  52. if xmin_bias - xmax_bias >= xmax - xmin:
  53. print('{:s}, xmin_bias - xmax_bias >= region_width'.format(cls))
  54. continue
  55. if ymin_bias - ymax_bias >= ymax - ymin:
  56. print('{:s}, ymin_bias - ymax_bias >= region_width'.format(cls))
  57. continue
  58. # xmin >=1, ymin>=1, xmax <= size[0] - 1, ymax <= size[1] - 1
  59. xmin = (xmin if (xmin > 0) else 1)
  60. ymin = (ymin if (ymin > 0) else 1)
  61. xmax = (xmax if (xmax < size[1]) else size[1] - 1)
  62. ymax = (ymax if (ymax < size[0]) else size[0] - 1)
  63. bbox_dict = {"xmin": xmin, "ymin": ymin, "xmax": xmax, "ymax": ymax}
  64. class_dict = {"class_name": cls, "bounding_box": bbox_dict, "score": score}
  65. content_list.append(class_dict)
  66. analysis_cls_list.append(cls)
  67. return content_list, sorted(analysis_cls_list)
  68. def get_single_image_sheet_regions(analysis_type, im, classes,
  69. sess, net, conf_thresh, mns_thresh,
  70. coordinate_bias_dict):
  71. start_time = time.time()
  72. content, cls = analysis_single_image_with_regions(analysis_type, classes,
  73. sess, net,
  74. im, conf_thresh, mns_thresh,
  75. coordinate_bias_dict)
  76. img_dict = {"img_name": 'cloze',
  77. 'analysis_type': analysis_type,
  78. "regions": content,
  79. }
  80. end_time = time.time()
  81. print(end_time - start_time)
  82. return img_dict