File size: 5,391 Bytes
c138d1f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri May 20 15:16:56 2022
@author: dinesh
"""
import json
import cv2
from PIL import Image
import numpy as np
from dateutil.parser import parse
def bb_intersection_over_union(box1, box2):
#print(box1, box2)
boxA = box1.copy()
boxB = box2.copy()
boxA[2] = boxA[0]+boxA[2]
boxA[3] = boxA[1]+boxA[3]
boxB[2] = boxB[0]+boxB[2]
boxB[3] = boxB[1]+boxB[3]
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
# compute the area of intersection rectangle
interArea = abs(max((xB - xA, 0)) * max((yB - yA), 0))
if interArea == 0:
return 0
# compute the area of both the prediction and ground-truth
# rectangles
boxAArea = abs((boxA[2] - boxA[0]) * (boxA[3] - boxA[1]))
boxBArea = abs((boxB[2] - boxB[0]) * (boxB[3] - boxB[1]))
# compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the interesection area
iou = interArea / float(boxAArea + boxBArea - interArea)
return iou
def bb_intersection_over_union_unoccluded(box1, box2, threshold=0.01):
#print(box1, box2)
boxA = box1.copy()
boxB = box2.copy()
boxA[2] = boxA[0]+boxA[2]
boxA[3] = boxA[1]+boxA[3]
boxB[2] = boxB[0]+boxB[2]
boxB[3] = boxB[1]+boxB[3]
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
# compute the area of intersection rectangle
interArea = abs(max((xB - xA, 0)) * max((yB - yA), 0))
if interArea == 0:
return 0
# compute the area of both the prediction and ground-truth
# rectangles
boxAArea = abs((boxA[2] - boxA[0]) * (boxA[3] - boxA[1]))
boxBArea = abs((boxB[2] - boxB[0]) * (boxB[3] - boxB[1]))
# compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the interesection area
iou = interArea / float(boxAArea + boxBArea - interArea)
#print(iou)
# return the intersection over union value
occlusion = False
if iou > threshold and iou < 1:
#print(boxA[3], boxB[3], boxB[1])
if boxA[3] < boxB[3]:# and boxA[3] > boxB[1]:
if boxB[2] > boxA[0]:# and boxB[2] < boxA[2]:
#print('first', (boxB[2] - boxA[0])/(boxA[2] - boxA[0]))
if (min(boxB[2],boxA[2]) - boxA[0])/(boxA[2] - boxA[0]) > threshold:
occlusion = True
if boxB[0] < boxA[2]: # boxB[0] > boxA[0] and
#print('second', (boxA[2] - boxB[0])/(boxA[2] - boxA[0]))
if (boxA[2] - max(boxB[0],boxA[0]))/(boxA[2] - boxA[0]) > threshold:
occlusion = True
if occlusion == False:
iou = iou*0
#asas
# asas
#iou = 0.9 #iou*0
#print(box1, box2, iou, occlusion)
return iou
def draw_tracks(image, tracks):
"""
Draw on input image.
Args:
image (numpy.ndarray): image
tracks (list): list of tracks to be drawn on the image.
Returns:
numpy.ndarray: image with the track-ids drawn on it.
"""
for trk in tracks:
trk_id = trk[1]
xmin = trk[2]
ymin = trk[3]
width = trk[4]
height = trk[5]
xcentroid, ycentroid = int(xmin + 0.5*width), int(ymin + 0.5*height)
text = "ID {}".format(trk_id)
cv2.putText(image, text, (xcentroid - 10, ycentroid - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv2.circle(image, (xcentroid, ycentroid), 4, (0, 255, 0), -1)
return image
def draw_bboxes(image, tracks):
"""
Draw the bounding boxes about detected objects in the image.
Args:
image (numpy.ndarray): Image or video frame.
bboxes (numpy.ndarray): Bounding boxes pixel coordinates as (xmin, ymin, width, height)
confidences (numpy.ndarray): Detection confidence or detection probability.
class_ids (numpy.ndarray): Array containing class ids (aka label ids) of each detected object.
Returns:
numpy.ndarray: image with the bounding boxes drawn on it.
"""
for trk in tracks:
xmin = int(trk[2])
ymin = int(trk[3])
width = int(trk[4])
height = int(trk[5])
clr = (np.random.randint(0, 255), np.random.randint(0, 255), np.random.randint(0, 255))
cv2.rectangle(image, (xmin, ymin), (xmin + width, ymin + height), clr, 2)
return image
def num(v):
number_as_float = float(v)
number_as_int = int(number_as_float)
return number_as_int if number_as_float == number_as_int else number_as_float
def parse_bbox(bbox_str):
bbox_list = bbox_str.strip('{').strip('}').split(',')
bbox_list = [num(elem) for elem in bbox_list]
return bbox_list
def parse_seg(bbox_str):
bbox_list = bbox_str.strip('{').strip('}').split(',')
bbox_list = [num(elem) for elem in bbox_list]
ret = bbox_list # []
# for i in range(0, len(bbox_list) - 1, 2):
# ret.append((bbox_list[i], bbox_list[i + 1]))
return ret
|