pcb defect detetcion application
[ealt-edge.git] / example-apps / PDD / pcb-defect-detection / libs / box_utils / boxes_utils_backup.py
1 # -*- coding: utf-8 -*-
2
3 from __future__ import absolute_import
4 from __future__ import division
5 from __future__ import print_function
6
7 import tensorflow as tf
8
9 def ious_calu(boxes_1, boxes_2):
10     '''
11
12     :param boxes_1: [N, 4] [xmin, ymin, xmax, ymax]
13     :param boxes_2: [M, 4] [xmin, ymin. xmax, ymax]
14     :return:
15     '''
16     boxes_1 = tf.cast(boxes_1, tf.float32)
17     boxes_2 = tf.cast(boxes_2, tf.float32)
18     xmin_1, ymin_1, xmax_1, ymax_1 = tf.split(boxes_1, 4, axis=1)  # xmin_1 shape is [N, 1]..
19     xmin_2, ymin_2, xmax_2, ymax_2 = tf.unstack(boxes_2, axis=1)  # xmin_2 shape is [M, ]..
20
21     max_xmin = tf.maximum(xmin_1, xmin_2)
22     min_xmax = tf.minimum(xmax_1, xmax_2)
23
24     max_ymin = tf.maximum(ymin_1, ymin_2)
25     min_ymax = tf.minimum(ymax_1, ymax_2)
26
27     overlap_h = tf.maximum(0., min_ymax - max_ymin)  # avoid h < 0
28     overlap_w = tf.maximum(0., min_xmax - max_xmin)
29
30     overlaps = overlap_h * overlap_w
31
32     area_1 = (xmax_1 - xmin_1) * (ymax_1 - ymin_1)  # [N, 1]
33     area_2 = (xmax_2 - xmin_2) * (ymax_2 - ymin_2)  # [M, ]
34
35     ious = overlaps / (area_1 + area_2 - overlaps)
36
37     return ious
38
39
40 def clip_boxes_to_img_boundaries(decode_boxes, img_shape):
41     '''
42
43     :param decode_boxes:
44     :return: decode boxes, and already clip to boundaries
45     '''
46
47     with tf.name_scope('clip_boxes_to_img_boundaries'):
48
49         # xmin, ymin, xmax, ymax = tf.unstack(decode_boxes, axis=1)
50         xmin = decode_boxes[:, 0]
51         ymin = decode_boxes[:, 1]
52         xmax = decode_boxes[:, 2]
53         ymax = decode_boxes[:, 3]
54         img_h, img_w = img_shape[1], img_shape[2]
55
56         img_h, img_w = tf.cast(img_h, tf.float32), tf.cast(img_w, tf.float32)
57
58         xmin = tf.maximum(tf.minimum(xmin, img_w-1.), 0.)
59         ymin = tf.maximum(tf.minimum(ymin, img_h-1.), 0.)
60
61         xmax = tf.maximum(tf.minimum(xmax, img_w-1.), 0.)
62         ymax = tf.maximum(tf.minimum(ymax, img_h-1.), 0.)
63
64         return tf.transpose(tf.stack([xmin, ymin, xmax, ymax]))
65
66
67 def filter_outside_boxes(boxes, img_h, img_w):
68     '''
69     :param anchors:boxes with format [xmin, ymin, xmax, ymax]
70     :param img_h: height of image
71     :param img_w: width of image
72     :return: indices of anchors that inside the image boundary
73     '''
74
75     with tf.name_scope('filter_outside_boxes'):
76         xmin, ymin, xmax, ymax = tf.unstack(boxes, axis=1)
77
78         xmin_index = tf.greater_equal(xmin, 0)
79         ymin_index = tf.greater_equal(ymin, 0)
80         xmax_index = tf.less_equal(xmax, tf.cast(img_w, tf.float32))
81         ymax_index = tf.less_equal(ymax, tf.cast(img_h, tf.float32))
82
83         indices = tf.transpose(tf.stack([xmin_index, ymin_index, xmax_index, ymax_index]))
84         indices = tf.cast(indices, dtype=tf.int32)
85         indices = tf.reduce_sum(indices, axis=1)
86         indices = tf.where(tf.equal(indices, 4))
87         # indices = tf.equal(indices, 4)
88         return tf.reshape(indices, [-1])
89
90
91 def padd_boxes_with_zeros(boxes, scores, max_num_of_boxes):
92
93     '''
94     num of boxes less than max num of boxes, so it need to pad with zeros[0, 0, 0, 0]
95     :param boxes:
96     :param scores: [-1]
97     :param max_num_of_boxes:
98     :return:
99     '''
100
101     pad_num = tf.cast(max_num_of_boxes, tf.int32) - tf.shape(boxes)[0]
102
103     zero_boxes = tf.zeros(shape=[pad_num, 4], dtype=boxes.dtype)
104     zero_scores = tf.zeros(shape=[pad_num], dtype=scores.dtype)
105
106     final_boxes = tf.concat([boxes, zero_boxes], axis=0)
107
108     final_scores = tf.concat([scores, zero_scores], axis=0)
109
110     return final_boxes, final_scores