def convert_to_frame_predictions(detections_dir): result = {} detection_files = helper.get_files(detections_dir) for file_name in detection_files: read_file = os.path.join(detections_dir, file_name) with open(read_file, 'r') as rf: file_info = {} width, height = [int(i) for i in rf.readline().split() ] # first line is image size file_info["image_height"] = height file_info["image_width"] = width file_info["detections"] = [] for line in rf: det = helper.Detection(line) detection = {} detection["bbox"] = [det.x1, det.y1, det.x2, det.y2] detection["prob"] = det.score detection["class"] = str(det.class_id) + " " + det.class_name file_info["detections"].append(detection) result[file_name] = file_info return result
def convert_to_kitti(detection_files, detections_dir, target_dir, model_dataset_type): for file_name in detection_files: read_file = os.path.join(detections_dir, file_name) write_file = os.path.join(target_dir, file_name) with open(read_file, 'r') as rf, open(write_file, 'w') as wf: rf.readline() # first line is image size for line in rf: det = helper.Detection(line) res = detection_to_kitti_string(det, model_dataset_type) if (res): wf.write(res) return target_dir
def convert_to_coco(detection_files, detections_dir, target_dir, dataset_type, model_dataset_type): res_array = [] for file_name in detection_files: read_file = os.path.join(detections_dir, file_name) file_id = helper.filename_to_id(file_name, dataset_type) with open(read_file, 'r') as rf: rf.readline() # first line is image size for line in rf: det = helper.Detection(line) res = detection_to_coco_object(det, model_dataset_type, file_id) if (res): res_array.append(res) results_file = os.path.join(target_dir, 'coco_results.json') with open(results_file, 'w') as f: f.write(json.dumps(res_array, indent=2, sort_keys=False)) return results_file
def fill_detection(detection, file_name): with open(file_name, 'r') as f: size = f.readline().split() x = float(size[0]) y = float(size[1]) cnt = 0 for line in f: det = ck_utils.Detection(line) cnt += 1 detection['detection_boxes'] = np.append(detection['detection_boxes'], [[det.x1/x, det.y1/y, det.x2/x, det.y2/y]], 0) detection['detection_classes'] = np.append(detection['detection_classes'], det.class_id) detection['detection_scores'] = np.append(detection['detection_scores'], det.score) # Detections dict can't be empty even there is not detection for this image if cnt == 0: detection['detection_boxes'] = np.array([[0.0, 0.0, 0.0, 0.0]]) detection['detection_classes'] = np.array([0]) detection['detection_scores'] = np.array([0.0]) return detection
def load_detections(file_path): boxes = [] scores = [] classes = [] with open(file_path, 'r') as f: f.readline() # first line is image size for line in f: det = ck_utils.Detection(line) boxes.append([det.x1, det.y1, det.x2, det.y2]) scores.append(det.score) classes.append(det.class_id) # Detections dict can't be empty even there is not detection for this image if not boxes: boxes = [[0, 0, 0, 0]] scores = [0] classes = [0] return { det_field.detection_boxes: np.array(boxes), det_field.detection_scores: np.array(scores), det_field.detection_classes: np.array(classes) }