|
|
import os |
|
|
import json |
|
|
|
|
|
def read_jsonl(file_path): |
|
|
"""Read a JSONL file and return a dictionary.""" |
|
|
data = {} |
|
|
with open(file_path, 'r') as file: |
|
|
for line in file: |
|
|
item = json.loads(line.strip()) |
|
|
image_path = item.get('image') |
|
|
text = item.get('text') |
|
|
if image_path and text: |
|
|
data[image_path] = text |
|
|
return data |
|
|
|
|
|
def get_text_from_jsonl(file_path): |
|
|
texts = [] |
|
|
with open(file_path, 'r') as f: |
|
|
for line in f: |
|
|
|
|
|
data = json.loads(line.strip()) |
|
|
|
|
|
if "text" in data: |
|
|
texts.append(data["text"]) |
|
|
return texts |
|
|
|
|
|
def generate_html(image_dir, question_file, answer_file, output_file): |
|
|
|
|
|
questions = read_jsonl(question_file) |
|
|
answers = get_text_from_jsonl(answer_file) |
|
|
|
|
|
|
|
|
html_content = """ |
|
|
<!DOCTYPE html> |
|
|
<html lang="en"> |
|
|
<head> |
|
|
<meta charset="UTF-8"> |
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
|
<title>Image Questions and Answers</title> |
|
|
<style> |
|
|
table { |
|
|
width: 100%; |
|
|
border-collapse: collapse; |
|
|
} |
|
|
th, td { |
|
|
border: 1px solid black; |
|
|
padding: 8px; |
|
|
text-align: left; |
|
|
} |
|
|
th { |
|
|
background-color: #f2f2f2; |
|
|
} |
|
|
img { |
|
|
max-width: 200px; |
|
|
height: auto; |
|
|
} |
|
|
</style> |
|
|
</head> |
|
|
<body> |
|
|
<h1>Image Questions and Answers</h1> |
|
|
<table> |
|
|
<tr> |
|
|
<th>Image</th> |
|
|
<th>Question or Prompt</th> |
|
|
<th>Ground Truth</th> |
|
|
<th>Answer</th> |
|
|
</tr> |
|
|
""" |
|
|
|
|
|
|
|
|
x = 0 |
|
|
for image_path, question_text in questions.items(): |
|
|
image_filename = os.path.basename(image_path) |
|
|
ground_truth = os.path.basename(os.path.dirname(image_path)) |
|
|
html_content += f""" |
|
|
<tr> |
|
|
<td><img src="testimages2/{image_filename}" alt="{image_filename}"></td> |
|
|
<td>{question_text}</td> |
|
|
<td>{ground_truth}</td> |
|
|
<td>{answers[x]}</td> |
|
|
</tr> |
|
|
""" |
|
|
x+=1 |
|
|
|
|
|
|
|
|
html_content += """ |
|
|
</table> |
|
|
</body> |
|
|
</html> |
|
|
""" |
|
|
|
|
|
|
|
|
with open(output_file, 'w') as f: |
|
|
f.write(html_content) |
|
|
|
|
|
print(f"HTML file '{output_file}' generated successfully.") |
|
|
|
|
|
|
|
|
|
|
|
image_directory = 'my_code/json/testimages2' |
|
|
question_filename = '/mnt/user/myang/LLaVA-2/my_code/json/llavatestquestions2.jsonl' |
|
|
answer_filename = '/mnt/user/myang/LLaVA-2/my_code/json/llavafinetune13b5epochlora2v3.jsonl' |
|
|
output_html_file = '/mnt/user/myang/LLaVA-2/my_code/json/llavafinetune13b5epochlora2v3html.html' |
|
|
|
|
|
generate_html(image_directory, question_filename, answer_filename, output_html_file) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|