def process_detections(base_data_dir, detections): # detections = list(detections) # print('\n' + str(len(detections)) + '\n') # sem.acquire() imgpath = Path(base_data_dir) / Path(detections[0]['runguid']) / Path( detections[0]['imagepath'], mode='r') if not imgpath.exists(): print(imgpath) return (None, None) tiffimg = TIFF.open(str(imgpath)) img = Image.open(str(imgpath)) w = img.width h = img.height del img image = np.empty((h, w, 4), dtype=np.uint8) depth = np.empty((h, w), dtype=np.float32) stencil = np.empty((h, w), dtype=np.uint8) TIFF.setdirectory(tiffimg, 0) TIFF.readencodedstrip(tiffimg, 0, image.ctypes.data, -1) lastdir = num_directories(tiffimg) - 1 TIFF.setdirectory(tiffimg, lastdir - 1) TIFF.readencodedstrip(tiffimg, 0, depth.ctypes.data, -1) TIFF.setdirectory(tiffimg, lastdir) TIFF.readencodedstrip(tiffimg, 0, stencil.ctypes.data, -1) affines = [ create_affine(loads(x['rot']), loads(x['pos'])) for x in detections ] bboxes_geom = [loads(x['fullbox']) for x in detections] bboxes = [(np.array(loads(x['bbox3d_min'])), np.array(loads(x['bbox3d_max']))) for x in detections] handles = [x['handle'] for x in detections] view = np.array(detections[0]['view_matrix'], dtype=np.float64) proj = np.array(detections[0]['proj_matrix'], dtype=np.float64) world_space = to_world_space(image, depth, proj, view, w, h) output = np.zeros((h, w), dtype=np.uint32) max = math.pow(2, 24) - 1 step = max // len(handles) i = step for box, xform, val in zip(bboxes, affines, handles): output += paint_pixels(world_space, xform, box, val) i += step output_cars = stencil_cull(output, stencil, StenCode.car, w, h) # output_peds = stencil_cull(output, stencil, StenCode.person, w, h) # draw_bboxes(image, depth, proj, view, bboxes_geom, affines) # colorout = cv2.applyColorMap(output.astype(np.uint8), cv2.COLORMAP_JET) output_col = output_cars.view(np.uint8).reshape(output_cars.shape + (4, ))[..., 1:4] #cv2.imshow("color", output_col) #cv2.waitKey(0) result = get_bboxes(output_cars, detections, w, h) # result += get_bboxes(output_peds, detections) return (result, output_cars)
def get_img_from_directory(tiff_tmp, dir_num, dst_img): TIFF.setdirectory(tiff_tmp, dir_num) TIFF.readencodedstrip(tiff_tmp, 0, dst_img.ctypes.data, -1)