def get_bb(fp, frame, toolName): undo_pos = fp.tell() line = fp.readline() # create default invalid bbox (aibu format for no bbx frames) no_annot_bb = BoundingBox("", "", frame) # aibu if toolName == config.Tool.AIBU: # no need for line check return BoundingBox(line, toolName, frame) # darklabel if toolName == config.Tool.DARKLABEL: parts = line.split(",") if parts[0] == str(frame): return BoundingBox(line, toolName) # vatic if toolName == config.Tool.VATIC: if "<annotation>" in line: undo_pos = fp.tell() line = fp.readline() if "<object>" in line: undo_pos = fp.tell() line = fp.readline() if "<t>" + str(frame) + "</t>" in line: return BoundingBox(line, toolName) # wrong framenumber or other error fp.seek(undo_pos) # undo last read return no_annot_bb
def default_value(nameOfImage, idClass, coordType, imgSize, isGT, bbFormat): idClass = '0' # class x = 0 #x_topleft y = 0 #y_topleft w = 0 h = 0 confidence = 0 if isGT: bb = BoundingBox(nameOfImage, idClass, x, y, w, h, coordType, imgSize, BBType.GroundTruth, format=bbFormat) else: bb = BoundingBox(nameOfImage, idClass, x, y, w, h, coordType, imgSize, BBType.Detected, confidence, format=bbFormat) return bb
def _extract_bounding_boxes(self, gt, ignore_grade: bool=False): boundingBoxes = BoundingBoxes() # gt boxes for image_id in gt.images: for x_min, y_min, x_max, y_max, class_id in gt.images[image_id].labels: if ignore_grade: class_id = 1 temp = BoundingBox(imageName=image_id, classId=class_id, x=max(x_min, 0), y=max(y_min, 0), w=min(256, x_max - x_min), h=min(256, y_max - y_min), typeCoordinates=CoordinatesType.Absolute, bbType=BBType.GroundTruth, format=BBFormat.XYWH, imgSize=(256, 256)) boundingBoxes.addBoundingBox(temp) for x_min, y_min, x_max, y_max, class_id in self.images[image_id].labels: if ignore_grade: class_id = 1 temp = BoundingBox(imageName=image_id, classId=class_id, x=max(x_min, 0), y=max(y_min, 0), w=min(256, x_max - x_min), h=min(256, y_max - y_min), typeCoordinates=CoordinatesType.Absolute, classConfidence=1, bbType=BBType.Detected, format=BBFormat.XYWH, imgSize=(256, 256)) boundingBoxes.addBoundingBox(temp) return boundingBoxes
def add(self, item): if self.depth < 8 and self.ne == None: cx = self.bbox.center.x cy = self.bbox.center.y self.ne = QuadNode( self.depth + 1, BoundingBox(cx, cy, self.bbox.maxX, self.bbox.maxY)) self.se = QuadNode( self.depth + 1, BoundingBox(cx, self.bbox.minY, self.bbox.maxX, cy)) self.sw = QuadNode( self.depth + 1, BoundingBox(self.bbox.minX, self.bbox.minY, cx, cy)) self.nw = QuadNode( self.depth + 1, BoundingBox(self.bbox.minX, cy, cx, self.bbox.maxY)) if self.depth == 8: self.items.append(item) return if self.ne.bbox.overlaps(item.bbox): self.ne.add(item) if self.se.bbox.overlaps(item.bbox): self.se.add(item) if self.sw.bbox.overlaps(item.bbox): self.sw.add(item) if self.nw.bbox.overlaps(item.bbox): self.nw.add(item)
def get_all_bbox(gt_json, det_json): allBoundingBoxes = BoundingBoxes() det = json.load(open(det_json,'r')) for k, v in det.items(): nameOfImage = k for obj in v: idClass = obj[5] # class confidence = float(obj[4]) # confidence x1 = float(obj[0]) y1 = float(obj[1]) x2 = float(obj[2]) y2 = float(obj[3]) bb = BoundingBox(nameOfImage, idClass, x1, y1, x2, y2, bbType=BBType.Detected, classConfidence=confidence, format=BBFormat.XYX2Y2) allBoundingBoxes.addBoundingBox(bb) gt = pd.read_json(gt_json, lines=True) for i in range(gt.shape[0]): url = gt.loc[i]['url'] filename = url.split('/')[-1] if filename in imgs: if len(gt.loc[i]['label'][0]['data']) > 0: for ix, data in enumerate(gt.loc[i]['label'][0]['data']): x1, y1 = data['bbox'][0] x2, y2 = data['bbox'][2] idClass = data['class'] bb = BoundingBox(filename, idClass,x1,y1,x2,y2, bbType=BBType.GroundTruth, classConfidence=confidence, format=BBFormat.XYX2Y2) allBoundingBoxes.addBoundingBox(bb) return allBoundingBoxes
def test_intersection(): ridx = RTreeIndex() ridx.insert(Point(10, 10)) ridx.insert(Point(100, 100)) q = ridx.intersection(BoundingBox(0, 0, 100, 100)) print(q) q = ridx.intersection(BoundingBox(0, 0, 90, 90)) print(q)
def iou(self, x1, y1, x2, y2): bounding_box1 = BoundingBox(x1 * self.stride - self.half_box_length, x1 * self.stride + self.half_box_length, y1 * self.stride - self.half_box_length, y1 * self.stride + self.half_box_length) bounding_box2 = BoundingBox(x2 * self.stride - self.half_box_length, x2 * self.stride + self.half_box_length, y2 * self.stride - self.half_box_length, y2 * self.stride + self.half_box_length) assert (bounding_box1.iou(bounding_box2) == bounding_box2.iou( bounding_box1)) return bounding_box1.iou(bounding_box2)
def getBoundingBoxes(directory, isGT, bbFormat, allBoundingBoxes=None, allClasses=None): """Read txt files containing bounding boxes (ground truth and detections).""" if allBoundingBoxes == None: allBoundingBoxes = BoundingBoxes() if allClasses == None: allClasses = [] # Read ground truths os.chdir(directory) files = glob.glob("*.txt") files.sort() # Read GT detections from txt file # Each line of the files in the groundtruths folder represents a ground truth bounding box (bounding boxes that a detector should detect) # Each value of each line is "class_id, x, y, width, height" respectively # Class_id represents the class of the bounding box # x, y represents the most top-left coordinates of the bounding box # x2, y2 represents the most bottom-right coordinates of the bounding box for f in files: nameOfImage = f.replace(".txt", "") fh1 = open(f, "r") for line in fh1: line = line.replace("\n", "") if line.replace(' ', '') == '': continue splitLine = line.split(" ") if isGT: # idClass = int(splitLine[0]) #class idClass = (splitLine[0]) # class x = float(splitLine[1]) y = float(splitLine[2]) w = float(splitLine[3]) h = float(splitLine[4]) bb = BoundingBox(nameOfImage, idClass, x, y, w, h, CoordinatesType.Absolute, (0, 0), BBType.GroundTruth, format=bbFormat) else: # idClass = int(splitLine[0]) #class idClass = (splitLine[0]) # class confidence = float(splitLine[1]) x = float(splitLine[2]) y = float(splitLine[3]) w = float(splitLine[4]) h = float(splitLine[5]) bb = BoundingBox(nameOfImage, idClass, x, y, w, h, CoordinatesType.Absolute, (0, 0), BBType.Detected, confidence, format=bbFormat) allBoundingBoxes.addBoundingBox(bb) if idClass not in allClasses: allClasses.append(idClass) fh1.close() return allBoundingBoxes, allClasses
def getBoundingBoxes(dets, isGT, bbFormat, coordType, allBoundingBoxes=None, allClasses=None, imgSize=(0, 0)): """Read txt files containing bounding boxes (ground truth and detections).""" if allBoundingBoxes is None: allBoundingBoxes = BoundingBoxes() if allClasses is None: allClasses = [] # Read GT detections from txt file # Each line of the files in the groundtruths folder represents a ground truth bounding box # (bounding boxes that a detector should detect) # Each value of each line is "class_id, x, y, width, height" respectively # Class_id represents the class of the bounding box # x, y represents the most top-left coordinates of the bounding box # x2, y2 represents the most bottom-right coordinates of the bounding box #nameOfImage = dets[0] for i in range(len(dets)): dets1 = dets[i] nameOfImage = dets1[0] if isGT: # idClass = int(splitLine[0]) #class idClass = 'person' # class x = dets1[1] y = dets1[2] w = dets1[3] h = dets1[4] bb = BoundingBox(nameOfImage, idClass, x, y, w, h, coordType, imgSize, BBType.GroundTruth, format=bbFormat) else: # idClass = int(splitLine[0]) #class idClass = 'person' # class confidence = dets1[2] x = dets1[3] y = dets1[4] w = dets1[5] h = dets1[6] bb = BoundingBox(nameOfImage, idClass, x, y, w, h, coordType, imgSize, BBType.Detected, confidence, format=bbFormat) allBoundingBoxes.addBoundingBox(bb) if idClass not in allClasses: allClasses.append(idClass) return allBoundingBoxes, allClasses
def filter(self, frame): current = self.preprocess_frame(frame) if self.background_age > self.max_background_age: self.background_frame = current self.background_age = 0 if self.last_frame is not None: current = self.last_frame # prevent comparing background with itself self.last_frame = current self.background_age += 1 frame_delta = cv2.absdiff(self.background_frame, current) # show(frame_delta, 'delta') result = cv2.threshold(frame_delta, 30, 255, cv2.THRESH_BINARY)[1] # show(result, 'threshold') result = cv2.dilate(result, None, iterations=20) # show(result, 'dilated') img, contours, _ = cv2.findContours(result.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) bounding_boxes = [] for contour in contours: if self.min_contour_area < cv2.contourArea( contour) < self.max_contour_area: bounding_boxes.append(BoundingBox(*cv2.boundingRect(contour))) # cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) return result, bounding_boxes
def computeCropPadImageLocation(bbox_tight, image): """TODO: Docstring for computeCropPadImageLocation. :returns: TODO """ # Center of the bounding box bbox_center_x = bbox_tight.get_center_x() bbox_center_y = bbox_tight.get_center_y() image_height = image.shape[0] image_width = image.shape[1] # Padded output width and height output_width = bbox_tight.compute_output_width() output_height = bbox_tight.compute_output_height() roi_left = max(0.0, bbox_center_x - (output_width / 2.)) roi_bottom = max(0.0, bbox_center_y - (output_height / 2.)) # Padded roi width left_half = min(output_width / 2., bbox_center_x) right_half = min(output_width / 2., image_width - bbox_center_x) roi_width = max(1.0, left_half + right_half) # Padded roi height top_half = min(output_height / 2., bbox_center_y) bottom_half = min(output_height / 2., image_height - bbox_center_y) roi_height = max(1.0, top_half + bottom_half) # Padded image location in the original image objPadImageLocation = BoundingBox(roi_left, roi_bottom, roi_left + roi_width, roi_bottom + roi_height) return objPadImageLocation
def subdivide(self): #print "self:",self.bbox l = self.bbox.ul.x r = self.bbox.lr.x t = self.bbox.ul.y b = self.bbox.lr.y mX = (l + r) / 2 mY = (t + b) / 2 self.northEast = QuadTree(BoundingBox(Point(mX, t), Point(r, mY)), self.maxPoints) self.southEast = QuadTree(BoundingBox(Point(mX, mY), Point(r, b)), self.maxPoints) self.southWest = QuadTree(BoundingBox(Point(l, mY), Point(mX, b)), self.maxPoints) self.northWest = QuadTree(BoundingBox(Point(l, t), Point(mX, mY)), self.maxPoints)
def __init__(self, presFile, startDate, endDate, species, inDir = '.', outDir = '.', numProcs = 10, numTrials = 10, logger = None): # Create the MmxConfig object. mmxConfig = MmxConfig() mmxConfig.initializeFromValues(presFile, startDate, endDate, species, inDir, outDir, numProcs, numTrials) # Define the bounding box. presPts = PresencePoints(presFile, species) bbox = BoundingBox(presPts.points, presPts.epsg) mmxConfig.setUlx(bbox.getUlx()) mmxConfig.setUly(bbox.getUly()) mmxConfig.setLrx(bbox.getLrx()) mmxConfig.setLry(bbox.getLry()) mmxConfig.setEPSG(bbox.getEpsg()) super(ConfigureMmxRun, self).__init__(mmxConfig, 'ConfigureMmxRun', logger) # Log what we have so far. self.logHeader() self.logger.info(str(self.config))
def get_bounding_boxes_from_xml(xml_path, coordType, bbFormat, allBoundingBoxes, allClasses, isGT): assert coordType == CoordinatesType.Absolute assert bbFormat == BBFormat.XYX2Y2 assert isGT is True tree = ET.parse(xml_path) root = tree.getroot() size = root.find('size') w = int(size.find('width').text) h = int(size.find('height').text) nameOfImage = xml_path.replace(".xml", "") for obj in root.iter('object'): difficult = obj.find('difficult').text cls_name = obj.find('name').text if int(difficult) == 1: continue xmlbox = obj.find('bndbox') b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text)) bb = BoundingBox(nameOfImage, cls_name, float(xmlbox.find('xmin').text), float(xmlbox.find('ymin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymax').text), coordType, (w, h), BBType.GroundTruth, format=bbFormat) allBoundingBoxes.addBoundingBox(bb) if cls_name not in allClasses: allClasses.append(cls_name)
def testInit(self): print 'testInit ...' validFile = 'tests/WLBG-geog.csv' presPts = PresencePoints(validFile, 'WLBG') bbox = BoundingBox(presPts.points, presPts.epsg)
def setup(self): self.q = QuadTree( BoundingBox(Point(0, 0), Point(self.width, self.height)), 3) self.points = [] self.colors = [ "#000", "#F00", "#0f0", "#00f", "#ff0", "#0ff", "#0f0" ]
def get_videos(self): """Docstring for get_videos. :returns: returns video frames in each sub folder of vot directory """ vot_folder = self.vot_folder sub_vot_dirs = self.find_subfolders(vot_folder) for vot_sub_dir in sub_vot_dirs: list_of_frames = sorted( glob.glob(os.path.join(vot_folder, vot_sub_dir, '*.jpg'))) if not list_of_frames: logger.error('vot folders should contain only .jpg images') self.videos[vot_sub_dir] = list_of_frames bbox_gt_file = os.path.join(vot_folder, vot_sub_dir, 'groundtruth.txt') bbox_gt_coords = [] with open(bbox_gt_file, 'r') as f: for line in f: co_ords = line.strip().split(',') co_ords = [int(float(co_ord)) for co_ord in co_ords] ax, ay, bx, by, cx, cy, dx, dy = co_ords x1 = min(ax, min(bx, min(cx, dx))) - 1 y1 = min(ay, min(by, min(cy, dy))) - 1 x2 = max(ax, max(bx, max(cx, dx))) - 1 y2 = max(ay, max(by, max(cy, dy))) - 1 bbox = BoundingBox(x1, y1, x2, y2) bbox_gt_coords.append(bbox) self.annotations[vot_sub_dir] = bbox_gt_coords return self.videos, self.annotations
def createEvalBBoxes(imgNames): gtBBoxes = BoundingBoxes() for name in imgNames: path = os.path.join('../VOCdevkit/VOC2007/Annotations', name + '.xml') root = ET.parse(path).getroot() height = _findNode(root.find('size'), 'height', parse=int) width = _findNode(root.find('size'), 'width', parse=int) for element in root.iter('object'): box, class_name, difficult = parseBBoxes(element) if difficult: continue gtBBoxes.addBoundingBox( BoundingBox(imageName=name, classId=class_name, x=box[0, 0], y=box[0, 1], w=box[0, 2], h=box[0, 3], typeCoordinates=CoordinatesType.Absolute, bbType=BBType.GroundTruth, format=BBFormat.XYX2Y2, imgSize=(width, height))) with open('validationBBoxes.pickle', 'wb') as f: pickle.dump(gtBBoxes, f) print('create EvalBBoxes over!')
def view(self, window): fov = 0.14 # Hack: Roughly the factor needed to account for the FOV w = window.width * fov h = window.height * fov bbox = BoundingBox(self.location.x - w / 2, self.location.y - h / 2, self.location.x + w / 2, self.location.y + h / 2) # bbox.draw() return bbox
def computeBBfromBB(bb_coords): """ Method that converts np array of coords (xmin,xmax,ymin,ymax) into a bounding box. Args: bb_coords: the bounding box coordinates """ return BoundingBox(bb_coords[0], bb_coords[1], bb_coords[2], bb_coords[3])
def testGetBoundingBox(self): validFile = 'tests/WLBG-geog.csv' presPts = PresencePoints(validFile, 'WLBG') bbox = BoundingBox(presPts.points, presPts.epsg) expected = (-76.8933333297525, -76.8579444453814, 39.3381111142888, 39.4326111107889) self.assertEqual(expected, bbox.getBoundingBox())
def updateLocation(self, newLocation): self.previousLocation = self.location self.location = newLocation self.bbox = BoundingBox(self.location.x - self.width / 2, self.location.y - self.height / 2, self.location.x + self.width / 2, self.location.y + self.height / 2) self.polygon = Polygon.createBoundingBoxPolygon( Vector(self.location.x - self.width / 2, self.location.y - self.height / 2), Vector(self.location.x + self.width / 2, self.location.y + self.height / 2)) self.polygon.convex = True
def locate_templates(img, templates, start, stop, threshold): #print('Locating Templates...') #print(f'templates in locate: {templates}') locations, scale = match(img, templates, start, stop, threshold) img_locations = [] for i in range(len(templates)): w, h = templates[i].shape[::-1] w *= scale h *= scale img_locations.append([ BoundingBox(pt[0], pt[1], w, h) for pt in zip(*locations[i][::-1]) ]) return img_locations
def __init__(self, depth=0, bbox=None): self.ne = self.se = self.sw = self.nw = None self.depth = depth if depth == 0: # Let's say our world can only be a third of the maximal float value max = 1500 #sys.float_info.max/3 self.bbox = BoundingBox(-max, -max, max, max) else: if not bbox: raise ValueError("No bounding box given") self.bbox = bbox self.items = []
def create_particles(num_particles: int, max_x: int, max_y: int, min_x: int, min_y: int, width: int, height: int, image) -> List[Particles]: particles = [] for i in range(0, num_particles): x = random.randint(min_y, max_x) y = random.randint(min_y, max_y) bbox = BoundingBox(x, y, width, height, 640, 480, 0, 0) histogram = generate_histograms(bbox, image) particle = Particles(bbox, histogram, 1) particles.append(particle) return particles
def computeBBfromSM(segMask): """ Method that computes bounding box from a binary segmentation mask with only a single object present Arguments: segMask: 2D binary image """ if len(segMask.shape) != 2: raise ValueError('Cannot compute bounding box from non-2D image') x_coords, y_coords = np.where(segMask > 0) return BoundingBox(np.min(y_coords), np.max(y_coords), np.min(x_coords), np.max(x_coords))
def get_image_bounding_box(self) -> BoundingBox: mx, my = self._lat_lon_to_meters(self.center_lat, self.center_lng) pixel_x, pixel_y = self._meters_to_pixels(mx, my) w_pixel_x, e_pixel_x = pixel_x - self.image_w / 2, pixel_x + self.image_w / 2 s_pixel_y, n_pixel_y = pixel_y - self.image_h / 2, pixel_y + self.image_h / 2 w_meter_x = self._pixels_to_meters(w_pixel_x, self.zoom) e_meter_x = self._pixels_to_meters(e_pixel_x, self.zoom) s_meter_y = self._pixels_to_meters(s_pixel_y, self.zoom) n_meter_y = self._pixels_to_meters(n_pixel_y, self.zoom) s_lat, w_lng = self._meters_to_lat_lon(w_meter_x, s_meter_y) n_lat, e_lng = self._meters_to_lat_lon(e_meter_x, n_meter_y) return BoundingBox(s_lat, w_lng, n_lat, e_lng)
def _generate_lib_bboxes(bb_type, bbox_map, confidence=None): boxes = [] for image_idx, bboxes in bbox_map.items(): image_name = dataset.get_image_name(image_idx) for bbox in bboxes: box = BoundingBox( image_name, 'text', # object class name int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]), typeCoordinates=CoordinatesType.Absolute, classConfidence=confidence, bbType=bb_type, format=BBFormat.XYX2Y2 ) boxes.append(box) return boxes
def start(): a = BoundingBox(90, 66, 191, 201, "8") b = BoundingBox(214, 102, 308, 132, "-") c = BoundingBox(322, 65, 423, 196, "4") d = BoundingBox(83, 222, 431, 271, "/") e = BoundingBox(97, 312, 190, 402, "2") f = BoundingBox(217, 319, 293, 378, "+") g = BoundingBox(318, 305, 391, 447, "3") testBoxList = [a, b, c, d, e, f, g] testDetection = DetectionOutput(testBoxList) result = testDetection.combineAll() #result = testDetection.getFirst((0,0)) result.display()
def main(video_file, bboxes): video = cv2.VideoCapture(video_file) # get the information of the video file num_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) rate = video.get(cv2.CAP_PROP_FPS) count = 0 while video.isOpened(): ret, frame = video.read() boxes = [] if not ret: print 'No frame read!' break if count == 0: PreviousFrame = frame.copy() # cv2.rectangle(FirstFrame, (int(bbox[0]),int(bbox[1])), (int(bbox[2]),int(bbox[3])), (255,255,0), 2) # target_pad, _, _, _ = cropPadImage(bbox, FirstFrame) else: for bbox in bboxes: target_pad, _, _, _ = cropPadImage(bbox, PreviousFrame) cur_search_region, search_location, edge_spacing_x, edge_spacing_y = cropPadImage( bbox, frame) bbox = track(cur_search_region, target_pad) bbox = BoundingBox(bbox[0, 0], bbox[0, 1], bbox[0, 2], bbox[0, 3]) bbox.unscale(cur_search_region) bbox.uncenter(frame, search_location, edge_spacing_x, edge_spacing_y) boxes.append(bbox) PreviousFrame = frame.copy() frame = draw_fancybbox(frame, boxes) count += 1 cv2.imshow('Result', frame) # cv2.waitKey() if cv2.waitKey(25) == 27: break