-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_test.py
69 lines (59 loc) · 2.82 KB
/
draw_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import json
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
from tqdm import tqdm
def draw_bbox(ax, bbox, label, color):
# bbox 是 xyxy 形式的
x_min, y_min, x_max, y_max = bbox
width = x_max - x_min
height = y_max - y_min
rect = patches.Rectangle((x_min, y_min), width, height, linewidth=2, edgecolor=color, facecolor='none')
ax.add_patch(rect)
ax.text(x_min, y_min, label, verticalalignment='top', color=color, fontsize=8, weight='bold', bbox=dict(facecolor='white', alpha=0.5))
def draw_line_between_centers(ax, sub_bbox, obj_bbox, label, color):
# 计算边界框的中心点
sub_center = ((sub_bbox[0] + sub_bbox[2]) / 2, (sub_bbox[1] + sub_bbox[3]) / 2)
obj_center = ((obj_bbox[0] + obj_bbox[2]) / 2, (obj_bbox[1] + obj_bbox[3]) / 2)
# 绘制连接线
ax.plot([sub_center[0], obj_center[0]], [sub_center[1], obj_center[1]], color=color, linewidth=2, linestyle='--')
# 在连线中间标注动作类别
mid_point = ((sub_center[0] + obj_center[0]) / 2, (sub_center[1] + obj_center[1]) / 2)
ax.text(mid_point[0], mid_point[1], label, verticalalignment='top', color=color, fontsize=8, weight='bold', bbox=dict(facecolor='white', alpha=0.5))
def visualize_annotations(info, output_dir):
filename = info['file_name']
image_path = os.path.join('/bd_byt4090i1/users/clin/RLIPv2/hico_20160224_det/images/test2015', filename)
image = Image.open(image_path).convert('RGB')
fig, ax = plt.subplots(1, figsize=(12, 9))
ax.imshow(image)
# 绘制所有注释中的边界框和类别标签
annotations = info['annotations']
for annotation in annotations:
bbox = annotation['bbox']
category_id = annotation['category_id']
draw_bbox(ax, bbox, f'Cat: {category_id}', 'green')
# 绘制 HOI 信息,通过连线和动作标签表示
hois = info['hoi_annotation']
for hoi in hois:
sub_idx = hoi['subject_id']
obj_idx = hoi['object_id']
category_id = hoi['category_id']
sub_bbox = annotations[sub_idx]['bbox']
obj_bbox = annotations[obj_idx]['bbox']
draw_line_between_centers(ax, sub_bbox, obj_bbox, f'Action: {category_id}', 'blue')
# 保存输出图像
output_path = os.path.join(output_dir, filename)
plt.savefig(output_path)
plt.close()
def load_and_visualize(json_file, output_dir):
with open(json_file, 'r') as f:
data = json.load(f)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for info in tqdm(data, desc="Processing images"):
visualize_annotations(info, output_dir)
# 使用示例
json_file = '/bd_byt4090i1/users/clin/RLIPv2/hico_20160224_det/annotations/test_hico.json'
output_dir = '/bd_byt4090i1/users/clin/relation/results/test_annotations'
load_and_visualize(json_file, output_dir)