|
|
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) |
|
|
|
|
|
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')) |