Repoaner commited on
Commit
ccda6ac
·
verified ·
1 Parent(s): dd68b7c

Upload LLaVA-Next-3D/data_precessing/box_visualization.py with huggingface_hub

Browse files
LLaVA-Next-3D/data_precessing/box_visualization.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image, ImageDraw, ImageFont
2
+ import json
3
+ import os
4
+ from diffusers.utils import make_image_grid
5
+ import pdb
6
+
7
+ def draw_bbox_on_image(path, bbox_normalized):
8
+ # 打开图像文件
9
+ img = Image.open(path)
10
+ width, height = img.size
11
+ x1 = int(bbox_normalized[0] * width)
12
+ y1 = int(bbox_normalized[1] * height)
13
+ x2 = int(bbox_normalized[2] * width)
14
+ y2 = int(bbox_normalized[3] * height)
15
+
16
+ draw = ImageDraw.Draw(img)
17
+ draw.rectangle([(x1, y1), (x2, y2)], outline='red', width=2)
18
+ return img
19
+
20
+ result_file = 'results/scanrefer/prompt-llavanext-qwen-uniform-16bs-scanrefer-wosam-uniform.jsonl'
21
+ visual_num = 1000
22
+
23
+ with open(result_file, 'r') as f:
24
+ i = 0
25
+ for line in f:
26
+ i += 1
27
+ if i >= visual_num:
28
+ break
29
+
30
+ item = json.loads(line)
31
+ output_dir = result_file.replace('.jsonl', '')
32
+ os.makedirs(output_dir, exist_ok=True)
33
+
34
+ pred_response = item['pred_response']
35
+ description = item['prompt'].split('\n')[-1]
36
+ sample_id = str(item['sample_id'])
37
+ sample_dir = os.path.join(output_dir, sample_id)
38
+
39
+ image_list = []
40
+ for path, box in pred_response.items():
41
+ try:
42
+ drawn_image = draw_bbox_on_image(path, box)
43
+ image_list.append(drawn_image)
44
+ # drawn_image.save(os.path.join(sample_dir, path.split('/')[-1]))
45
+ except:
46
+ continue
47
+
48
+ image_num = min(5, len(image_list))
49
+ image_list = image_list[:image_num]
50
+ concat_image = make_image_grid(image_list, 1, image_num)
51
+ draw = ImageDraw.Draw(concat_image)
52
+ font = ImageFont.load_default(size=80)
53
+ draw.text((10, 10), description, fill='green', font=font)
54
+ concat_image.save(os.path.join(sample_dir+'.jpg'))