File size: 5,238 Bytes
90cd92a |
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
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:
# Parse the JSON object
data = json.loads(line.strip())
# Access the "text" field and append to list
if "text" in data:
texts.append(data["text"])
return texts
def generate_html(image_dir, question_file, answer_file, output_file):
# Load the JSONL files
questions = read_jsonl(question_file)
answers = get_text_from_jsonl(answer_file)
# Start the HTML string
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>
"""
# Add table rows for each image and its question or prompt
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
# End the HTML string
html_content += """
</table>
</body>
</html>
"""
# Write the HTML string to the output file
with open(output_file, 'w') as f:
f.write(html_content)
print(f"HTML file '{output_file}' generated successfully.")
# Example usage
image_directory = 'my_code/json/testimages2' # Directory containing images
question_filename = '/mnt/user/myang/LLaVA-2/my_code/json/llavatestquestions2.jsonl' # JSON file with questions
answer_filename = '/mnt/user/myang/LLaVA-2/my_code/json/llavafinetune13b5epochlora2v3.jsonl' # JSON file with answers
output_html_file = '/mnt/user/myang/LLaVA-2/my_code/json/llavafinetune13b5epochlora2v3html.html' # Output HTML file
generate_html(image_directory, question_filename, answer_filename, output_html_file)
# def generate_html(image_dir, question_file, answer_file, output_file):
# # Load the JSON files
# with open(question_file, 'r') as qf:
# questions = json.load(qf)
# with open(answer_file, 'r') as af:
# answers = json.load(af)
# # Start the HTML string
# 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;
# }
# </style>
# </head>
# <body>
# <h1>Image Questions and Answers</h1>
# <table>
# <tr>
# <th>Image</th>
# <th>Question</th>
# <th>Answer</th>
# </tr>
# """
# # Add table rows for each image, its question, and answer
# for i in range(len(questions)):
# entry=questions[i]
# image_path = os.path.join("testimages", entry.get('image'))
# question = entry.get('text')
# answer = answers[i].get('text')
# html_content += f"""
# <tr>
# <td><img src="{image_path}" alt="{entry.get('image')}" style="max-width:200px;"></td>
# <td>{question}</td>
# <td>{answer}</td>
# </tr>
# """
# # End the HTML string
# html_content += """
# </table>
# </body>
# </html>
# """
# # Write the HTML string to the output file
# with open(output_file, 'w') as f:
# f.write(html_content)
# print(f"HTML file '{output_file}' generated successfully.") |