|
| 1 | +from __future__ import division |
| 2 | + |
| 3 | +import torch |
| 4 | +import random |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import cv2 |
| 8 | + |
| 9 | +def confidence_filter(result, confidence): |
| 10 | + conf_mask = (result[:,:,4] > confidence).float().unsqueeze(2) |
| 11 | + result = result*conf_mask |
| 12 | + |
| 13 | + return result |
| 14 | + |
| 15 | +def confidence_filter_cls(result, confidence): |
| 16 | + max_scores = torch.max(result[:,:,5:25], 2)[0] |
| 17 | + res = torch.cat((result, max_scores),2) |
| 18 | + print(res.shape) |
| 19 | + |
| 20 | + |
| 21 | + cond_1 = (res[:,:,4] > confidence).float() |
| 22 | + cond_2 = (res[:,:,25] > 0.995).float() |
| 23 | + |
| 24 | + conf = cond_1 + cond_2 |
| 25 | + conf = torch.clamp(conf, 0.0, 1.0) |
| 26 | + conf = conf.unsqueeze(2) |
| 27 | + result = result*conf |
| 28 | + return result |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +def get_abs_coord(box): |
| 33 | + box[2], box[3] = abs(box[2]), abs(box[3]) |
| 34 | + x1 = (box[0] - box[2]/2) - 1 |
| 35 | + y1 = (box[1] - box[3]/2) - 1 |
| 36 | + x2 = (box[0] + box[2]/2) - 1 |
| 37 | + y2 = (box[1] + box[3]/2) - 1 |
| 38 | + return x1, y1, x2, y2 |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +def sanity_fix(box): |
| 43 | + if (box[0] > box[2]): |
| 44 | + box[0], box[2] = box[2], box[0] |
| 45 | + |
| 46 | + if (box[1] > box[3]): |
| 47 | + box[1], box[3] = box[3], box[1] |
| 48 | + |
| 49 | + return box |
| 50 | + |
| 51 | +def bbox_iou(box1, box2): |
| 52 | + """ |
| 53 | + Returns the IoU of two bounding boxes |
| 54 | + |
| 55 | + |
| 56 | + """ |
| 57 | + #Get the coordinates of bounding boxes |
| 58 | + b1_x1, b1_y1, b1_x2, b1_y2 = box1[:,0], box1[:,1], box1[:,2], box1[:,3] |
| 59 | + b2_x1, b2_y1, b2_x2, b2_y2 = box2[:,0], box2[:,1], box2[:,2], box2[:,3] |
| 60 | + |
| 61 | + #get the corrdinates of the intersection rectangle |
| 62 | + inter_rect_x1 = torch.max(b1_x1, b2_x1) |
| 63 | + inter_rect_y1 = torch.max(b1_y1, b2_y1) |
| 64 | + inter_rect_x2 = torch.min(b1_x2, b2_x2) |
| 65 | + inter_rect_y2 = torch.min(b1_y2, b2_y2) |
| 66 | + |
| 67 | + #Intersection area |
| 68 | + if torch.cuda.is_available(): |
| 69 | + inter_area = torch.max(inter_rect_x2 - inter_rect_x1 + 1,torch.zeros(inter_rect_x2.shape).cuda())*torch.max(inter_rect_y2 - inter_rect_y1 + 1, torch.zeros(inter_rect_x2.shape).cuda()) |
| 70 | + else: |
| 71 | + inter_area = torch.max(inter_rect_x2 - inter_rect_x1 + 1,torch.zeros(inter_rect_x2.shape))*torch.max(inter_rect_y2 - inter_rect_y1 + 1, torch.zeros(inter_rect_x2.shape)) |
| 72 | + |
| 73 | + #Union Area |
| 74 | + b1_area = (b1_x2 - b1_x1 + 1)*(b1_y2 - b1_y1 + 1) |
| 75 | + b2_area = (b2_x2 - b2_x1 + 1)*(b2_y2 - b2_y1 + 1) |
| 76 | + |
| 77 | + iou = inter_area / (b1_area + b2_area - inter_area) |
| 78 | + |
| 79 | + return iou |
| 80 | + |
| 81 | + |
| 82 | +def pred_corner_coord(prediction): |
| 83 | + #Get indices of non-zero confidence bboxes |
| 84 | + ind_nz = torch.nonzero(prediction[:,:,4]).transpose(0,1).contiguous() |
| 85 | + |
| 86 | + box = prediction[ind_nz[0], ind_nz[1]] |
| 87 | + |
| 88 | + |
| 89 | + box_a = box.new(box.shape) |
| 90 | + box_a[:,0] = (box[:,0] - box[:,2]/2) |
| 91 | + box_a[:,1] = (box[:,1] - box[:,3]/2) |
| 92 | + box_a[:,2] = (box[:,0] + box[:,2]/2) |
| 93 | + box_a[:,3] = (box[:,1] + box[:,3]/2) |
| 94 | + box[:,:4] = box_a[:,:4] |
| 95 | + |
| 96 | + prediction[ind_nz[0], ind_nz[1]] = box |
| 97 | + |
| 98 | + return prediction |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +def write(x, batches, results, colors, classes): |
| 104 | + c1 = tuple(x[1:3].int()) |
| 105 | + c2 = tuple(x[3:5].int()) |
| 106 | + img = results[int(x[0])] |
| 107 | + cls = int(x[-1]) |
| 108 | + label = "{0}".format(classes[cls]) |
| 109 | + color = random.choice(colors) |
| 110 | + cv2.rectangle(img, c1, c2,color, 1) |
| 111 | + t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 1 , 1)[0] |
| 112 | + c2 = c1[0] + t_size[0] + 3, c1[1] + t_size[1] + 4 |
| 113 | + cv2.rectangle(img, c1, c2,color, -1) |
| 114 | + cv2.putText(img, label, (c1[0], c1[1] + t_size[1] + 4), cv2.FONT_HERSHEY_PLAIN, 1, [225,255,255], 1); |
| 115 | + return img |
0 commit comments