File size: 1,815 Bytes
ccda6ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from PIL import Image, ImageDraw, ImageFont
import json
import os
from diffusers.utils import make_image_grid
import pdb

def draw_bbox_on_image(path, bbox_normalized):
    # 打开图像文件
    img = Image.open(path)
    width, height = img.size
    x1 = int(bbox_normalized[0] * width)
    y1 = int(bbox_normalized[1] * height)
    x2 = int(bbox_normalized[2] * width)
    y2 = int(bbox_normalized[3] * height)

    draw = ImageDraw.Draw(img)
    draw.rectangle([(x1, y1), (x2, y2)], outline='red', width=2)
    return img

result_file = 'results/scanrefer/prompt-llavanext-qwen-uniform-16bs-scanrefer-wosam-uniform.jsonl'
visual_num = 1000

with open(result_file, 'r') as f:
    i = 0
    for line in f:
        i += 1
        if i >= visual_num:
            break
        
        item = json.loads(line)
        output_dir = result_file.replace('.jsonl', '')
        os.makedirs(output_dir, exist_ok=True)

        pred_response = item['pred_response']
        description = item['prompt'].split('\n')[-1]
        sample_id = str(item['sample_id'])
        sample_dir = os.path.join(output_dir, sample_id)

        image_list = []
        for path, box in pred_response.items():
            try:
                drawn_image = draw_bbox_on_image(path, box)
                image_list.append(drawn_image)
                # drawn_image.save(os.path.join(sample_dir, path.split('/')[-1]))
            except:
                continue
        
        image_num = min(5, len(image_list))
        image_list = image_list[:image_num]
        concat_image = make_image_grid(image_list, 1, image_num)
        draw = ImageDraw.Draw(concat_image)
        font = ImageFont.load_default(size=80)
        draw.text((10, 10), description, fill='green', font=font)
        concat_image.save(os.path.join(sample_dir+'.jpg'))