给我一个封装好的opencv python的VOC标签可视化的代码
时间: 2024-04-15 17:30:27 浏览: 13
当然,这是一个封装好的 OpenCV Python 的 VOC 标签可视化代码示例:
```python
import cv2
def visualize_voc_labels(image_path, annotation_path, class_names):
image = cv2.imread(image_path)
with open(annotation_path, 'r') as f:
lines = f.readlines()
for line in lines:
label_info = line.strip().split()
class_id = int(label_info[0])
x_min = int(float(label_info[1]))
y_min = int(float(label_info[2]))
x_max = int(float(label_info[3]))
y_max = int(float(label_info[4]))
class_name = class_names[class_id]
color = (0, 255, 0) # Green color for bounding boxes
thickness = 2
cv2.rectangle(image, (x_min, y_min), (x_max, y_max), color, thickness)
cv2.putText(image, class_name, (x_min, y_min-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, thickness)
cv2.imshow('VOC Labels Visualization', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Example usage
image_path = 'path/to/image.jpg'
annotation_path = 'path/to/annotation.txt'
class_names = ['background', 'cat', 'dog', 'person'] # Replace with your VOC class names
visualize_voc_labels(image_path, annotation_path, class_names)
```
请将 `image_path` 替换为图像文件的路径,`annotation_path` 替换为包含 VOC 标签信息的文件路径(每行一个标签),`class_names` 替换为 VOC 类别的名称列表。运行代码后会显示带有边界框和类别标签的图像窗口。
希望对你有所帮助!如有其他问题,请随时提问。