analysis_choice.py 3.8 KB

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