import os
import re
from .common import read_json_file

def get_frame_number(filename):
    """从文件名中提取帧号数字"""
    match = re.search(r'frame_(\d+)', filename)
    if match:
        return int(match.group(1))
    return 0

def read_jsons_in_order(directory):
    """按照frame序号顺序读取目录下的文件"""
    # 获取目录下所有文件
    files = os.listdir(directory)
    # 按照frame号码排序
    sorted_files = sorted(files, key=get_frame_number)
    
    results = []
    for file in sorted_files:
        file_path = os.path.join(directory, file)
        if os.path.isfile(file_path):
            try:
                data = read_json_file(file_path)
                results.append((file_path, data))
            except Exception as e:
                print(f"读取文件 {file} 时出错: {e}")
    
    return results

def read_images_in_order(directory):
    """按照frame序号顺序读取目录下的文件"""
    # 获取目录下所有文件
    files = os.listdir(directory)
    # 按照frame号码排序
    sorted_files = sorted(files, key=get_frame_number)
    
    results = []
    for file in sorted_files:
        file_path = os.path.join(directory, file)
        if os.path.isfile(file_path):
            try:
                results.append(file_path)
            except Exception as e:
                print(f"读取文件 {file} 时出错: {e}")
    
    return results


if __name__ == "__main__":
    file_path = "/data/data/luosy/project/oral/data/img_caption/"
    sorted_files = read_files_in_order(file_path)
    
    # 打印排序后的文件内容
    for filename, content in sorted_files:
        print(f"处理文件: {filename}")
        print(f"内容: {type(content)}")
        print("-" * 50)