You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将EasyOCR返回的不规则嵌套列表转换为带imageID的DataFrame

EasyOCR嵌套结果转DataFrame解决方案

报错原因

你代码出错的核心是对Python列表遍历的逻辑理解错误:for i in results_ 遍历得到的i是外层列表的元素本身(也就是单张图片的识别结果列表),不是下标索引,所以不能用results_[i]的方式取值,才会触发列表索引不能是列表的类型错误。

完整实现代码

import pandas as pd

# 你的EasyOCR识别结果嵌套列表
results_ = [
    # 这里替换成你实际的识别结果
    [],
    [],
    [([[163, 165], [219, 165], [219, 185], [163, 185]], 'HXRESLAGET', 0.6614451489762804)],
    [],
    [],
    [],
    [],
    [],
    [],
    [],
    [],
    [],
    [],
    [],
    [],
    [([[185, 47], [257, 47], [257, 63], [185, 63]], 'Ahabngue', 0.0021960531212282365),
     ([[330.01941932430907, 26.803883864861817], [375.8479983040051, 14.47000105999682], [378.98058067569093, 29.196116135138183], [334.1520016959949, 40.52999894000318]], '', 0.0),
     ([[281.0, 40.0], [331.87056283872016, 26.507942743332098], [335.0, 40.0], [284.12943716127984, 53.4920572566679]], 'o', 0.026024344610394934)],
    [],
    [],
    [([[71.60127388858555, 57.08292994374676], [116.99796908286363, 67.93629984577466], [113.39872611141445, 81.91707005625324], [68.00203091713637, 71.06370015422534]], 'eiao]', 0.04165130315364712)],
    []
]

rows = []
# 遍历外层列表,同时获取图片序号(image number)和对应图片的识别结果
for img_idx, img_results in enumerate(results_):
    # 遍历单张图片的所有识别条目
    for res in img_results:
        bbox, text, conf = res
        rows.append({
            "image number": img_idx,  # 如果要从1开始计数,改成img_idx+1即可
            "boundingbox": str(bbox), # 转成字符串方便存储到CSV,需要做坐标计算可以不转
            "letters": text,
            "confidence": conf
        })

# 转成DataFrame
df = pd.DataFrame(rows)
# 保存为CSV,index=False去掉默认的行索引列
df.to_csv("ocr_results.csv", index=False, encoding="utf-8-sig")

效果说明

用你提供的示例数据运行后,得到的DataFrame共5行,完全匹配你需要的四列结构,没有识别结果的空图片不会生成冗余行,image number和原图片的索引一一对应,可直接用于后续合并。

内容的提问来源于stack exchange,提问作者lapgoch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 19:54:03