C和python中%d %.2d %2d %02d的区别

%d就是普通的输出了

% 2d是将数字按宽度为2,采用右对齐方式输出,若数据位数不到2位,则左边补空格
% 02d,和% 2d差不多,只不过左边补0
%.2d 输出整形时最少输出2位,如不够前面以0占位。如输出2时变成02。200时只输出200;输出浮点型时(%.2f)小数点后强制2位输出

  • 17
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
def save_kitti_format(sample_id, calib, bbox3d, kitti_output_dir, scores, img_shape): corners3d = kitti_utils.boxes3d_to_corners3d(bbox3d) img_boxes, _ = calib.corners3d_to_img_boxes(角3d) img_boxes[:, 0] = np.clip(img_boxes[:, 0], 0, img_shape[1] - 1) img_boxes[:, 1] = np.clip(img_boxes[:, 1], 0, img_shape[0] - 1) img_boxes[:, 2] = np.clip(img_boxes[:, 2], 0, img_shape[1] - 1) img_boxes[:, 3] = np.clip(img_boxes[:, 3], 0, img_shape[0] - 1) img_boxes_w = img_boxes[:, 2] - img_boxes[:, 0] img_boxes_h = img_boxes[:, 3] - img_boxes[:, 1] box_valid_mask = np.logical_and(img_boxes_w < img_shape[1] * 0.8, img_boxes_h < img_shape[0] * 0.8) kitti_output_file = os.path.join(kitti_output_dir, '%06d.txt' % sample_id) with open(kitti_output_file, 'w') as f: for k in range(bbox3d.shape[0]): if box_valid_mask[k] == 0: continue x, z, ry = bbox3d[k, 0], bbox3d[k, 2], bbox3d[k, 6] beta = np.arctan2(z, x) alpha = -np.sign(beta) * np.pi / 2 + beta + ry print('%s -1 -1 %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f' % (cfg.CLASSES, alpha, img_boxes[k, 0], img_boxes[k, 1], img_boxes[k, 2], img_boxes[k, 3], bbox3d[k, 3], bbox3d[k, 4], bbox3d[k, 5], bbox3d[k, 0], bbox3d[k, 1], bbox3d[k, 2], bbox3d[k, 6], scores[k]), file=f)解释这段代码,并且根据已知的条件,已知sample_id, 点云的检测结果(x, y, z, w, h, l, yaw), kitti_output_dir, scores, img_shape,calib文件的路径且格式与 KITTI 数据集的标定文件格式相同,要求得到2D检测框的坐标,和alpha,仿写出Python函数,并给出示例
07-09
这段代码的功能是将检测结果保存为符合KITTI数据集格式的文件。 以下是根据已知条件编写的Python函数,用于获取2D检测框的坐标和alpha: ```python import os import numpy as np def save_kitti_format(sample_id, calib, detection_result, kitti_output_dir, scores, img_shape): bbox3d = np.array(detection_result) corners3d = kitti_utils.boxes3d_to_corners3d(bbox3d) img_boxes, _ = calib.corners3d_to_img_boxes(corners3d) img_boxes[:, 0] = np.clip(img_boxes[:, 0], 0, img_shape[1] - 1) img_boxes[:, 1] = np.clip(img_boxes[:, 1], 0, img_shape[0] - 1) img_boxes[:, 2] = np.clip(img_boxes[:, 2], 0, img_shape[1] - 1) img_boxes[:, 3] = np.clip(img_boxes[:, 3], 0, img_shape[0] - 1) img_boxes_w = img_boxes[:, 2] - img_boxes[:, 0] img_boxes_h = img_boxes[:, 3] - img_boxes[:, 1] box_valid_mask = np.logical_and(img_boxes_w < img_shape[1] * 0.8, img_boxes_h < img_shape[0] * 0.8) kitti_output_file = os.path.join(kitti_output_dir, '%06d.txt' % sample_id) with open(kitti_output_file, 'w') as f: for k in range(bbox3d.shape[0]): if box_valid_mask[k] == 0: continue x, z, ry = bbox3d[k, 0], bbox3d[k, 2], bbox3d[k, 6] beta = np.arctan2(z, x) alpha = -np.sign(beta) * np.pi / 2 + beta + ry print('%s -1 -1 %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f' % (cfg.CLASSES, alpha, img_boxes[k, 0], img_boxes[k, 1], img_boxes[k, 2], img_boxes[k, 3], bbox3d[k, 3], bbox3d[k, 4], bbox3d[k, 5], bbox3d[k, 0], bbox3d[k, 1], bbox3d[k, 2], bbox3d[k, 6], scores[k]), file=f) # Example usage sample_id = 1 calib = Calib() # Assuming Calib is a class for loading calibration matrix from file detection_result = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 0.785] # [x, y, z, w, h, l, yaw] kitti_output_dir = 'path/to/kitti_output' scores = [0.9, 0.8, 0.7] # Detection scores for each box img_shape = (640, 480) # Image shape (width, height) save_kitti_format(sample_id, calib, detection_result, kitti_output_dir, scores, img_shape) ``` 请注意,上述代码的 `Calib()` 类需要根据实际情况进行定义和实现,以便从标定文件加载校准矩阵。你还需要将 `cfg.CLASSES` 替换为相应的类别名称。另外,确保 `kitti_output_dir` 目录存在,否则需要先创建它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值