Datasets:
Tasks:
Image-Text-to-Text
Modalities:
Image
Formats:
imagefolder
Languages:
English
Size:
1K - 10K
ArXiv:
License:
| import os | |
| import json | |
| import random | |
| from PIL import Image, ImageDraw, ImageFont | |
| # 设置输出目录 | |
| OUTPUT_DIR = "palindrome_dataset/img" | |
| os.makedirs(OUTPUT_DIR, exist_ok=True) | |
| DIGIT_COLORS = ["black", "purple", "pink", "blue", "green"] | |
| FONT_SIZE = 50 | |
| # 适配 Windows/Linux 字体路径 | |
| if os.name == "nt": # Windows | |
| FONT_PATH = "C:/Windows/Fonts/arial.ttf" | |
| else: # Linux | |
| FONT_PATH = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" | |
| def generate_palindrome(length): | |
| """ 生成一个回文数字串 """ | |
| half = [random.randint(1, 9) for _ in range(length // 2)] | |
| palindrome = half + half[::-1] if length % 2 == 0 else half + [random.randint(1, 9)] + half[::-1] | |
| return palindrome | |
| def create_palindrome_image(file_name, palindrome): | |
| """ 创建回文数字图片,最后一个数字变成问号 """ | |
| img_width = len(palindrome) * 60 | |
| img_height = 100 | |
| image = Image.new("RGB", (img_width, img_height), "white") | |
| draw = ImageDraw.Draw(image) | |
| try: | |
| font = ImageFont.truetype(FONT_PATH, FONT_SIZE) | |
| except OSError: | |
| print("⚠️ 字体文件未找到,使用 PIL 默认字体") | |
| font = ImageFont.load_default() | |
| for i, num in enumerate(palindrome[:-1]): | |
| color = random.choice(DIGIT_COLORS) | |
| draw.text((i * 60 + 10, 25), str(num), font=font, fill=color) | |
| # 最后一个数字换成问号 | |
| draw.text(((len(palindrome) - 1) * 60 + 10, 25), "?", font=font, fill="red") | |
| image_path = f"{OUTPUT_DIR}/{file_name}" | |
| image.save(image_path) | |
| return image_path | |
| def generate_json(file_name, data): | |
| """ 生成 JSON 文件 """ | |
| with open(file_name, "w", encoding="utf-8") as f: | |
| json.dump(data, f, ensure_ascii=False, indent=4) | |
| def main(): | |
| # 生成 support.json(32 个完整回文) | |
| support_data = [] | |
| for i in range(32): | |
| length = random.randint(5, 9) | |
| palindrome = generate_palindrome(length) | |
| file_name = f"support_{i + 1}.png" | |
| image_path = create_palindrome_image(file_name, palindrome) | |
| support_data.append({ | |
| "id": f"support_{i + 1}", | |
| "image": [image_path], | |
| "question": "What is the missing number in this palindrome?", | |
| "answer": palindrome[-1] | |
| }) | |
| generate_json("support.json", support_data) | |
| # 生成 query.json(100 个回文,独立) | |
| query_data = [] | |
| for i in range(100): | |
| length = random.randint(5, 9) | |
| palindrome = generate_palindrome(length) | |
| file_name = f"query_{i + 1}.png" | |
| image_path = create_palindrome_image(file_name, palindrome) | |
| query_data.append({ | |
| "id": f"query_{i + 1}", | |
| "image": [image_path], | |
| "question": "What is the missing number in this palindrome?", | |
| "answer": palindrome[-1] | |
| }) | |
| generate_json("query.json", query_data) | |
| # 生成 training_shots_pool.json(独立的 32 组 shots,不和 support.json 重复) | |
| training_shots_pool = [] | |
| for i in range(32): | |
| length = random.randint(5, 9) | |
| palindrome = generate_palindrome(length) | |
| file_name = f"training_shot_{i + 1}.png" | |
| image_path = create_palindrome_image(file_name, palindrome) | |
| training_shots_pool.append({ | |
| "id": f"training_shot_{i + 1}", | |
| "image": [image_path], | |
| "question": "What is the missing number in this palindrome?", | |
| "answer": palindrome[-1] | |
| }) | |
| generate_json("training_shots_pool.json", training_shots_pool) | |
| # 生成 training_query.json(独立的 50 个 query,不和 query.json 重复) | |
| training_queries = [] | |
| for i in range(50): | |
| length = random.randint(5, 9) | |
| palindrome = generate_palindrome(length) | |
| file_name = f"training_query_{i + 1}.png" | |
| image_path = create_palindrome_image(file_name, palindrome) | |
| training_queries.append({ | |
| "id": f"training_query_{i + 1}", | |
| "image": [image_path], | |
| "question": "What is the missing number in this palindrome?", | |
| "answer": palindrome[-1] | |
| }) | |
| generate_json("training_query.json", training_queries) | |
| # 生成 training_data.json(50 组,每个 4 shots + 1 query) | |
| training_data = [] | |
| for query in training_queries: | |
| chosen_shots = random.sample(training_shots_pool, 4) # ✅ shots 现在只从 training_shots_pool 选 | |
| messages = [{"role": "user", "content": [{"type": "text", "text": "Learn from the demos and give only the answer to the final question."}]}] | |
| for shot in chosen_shots: | |
| messages[0]["content"].append({"type": "image", "image": shot["image"][0]}) | |
| messages[0]["content"].append({"type": "text", "text": f"Question: What is the missing number in this palindrome? Answer: {shot['answer']}\n"}) | |
| messages[0]["content"].append({"type": "image", "image": query["image"][0]}) | |
| messages[0]["content"].append({"type": "text", "text": "Question: What is the missing number in this palindrome? Answer: \n"}) | |
| messages.append({"role": "assistant", "content": [{"type": "text", "text": str(query['answer'])}]}) | |
| training_data.append({"id": query["id"], "messages": messages}) | |
| generate_json("training_data.json", training_data) | |
| print("✅ 数据集生成完成!已保存 query.json、support.json、training_shots_pool.json、training_query.json 和 training_data.json!") | |
| if __name__ == "__main__": | |
| main() | |