【问题标题】:pytesseract not keeping leading zeroes when using image_to_data()pytesseract 在使用 image_to_data() 时不保留前导零
【发布时间】:2022-12-01 02:35:24
【问题描述】:

我正在使用 pytesseract 处理下图:

当我使用 image_to_string() 函数时

config = "--oem 3 -l eng --psm 7"
pytesseract.image_to_string(potential_image, config = config)

我得到正确的“03”输出。但是,当我使用 image_to_data() 函数时

predict = pytesseract.image_to_data(potential_image, config = config, output_type="data.frame") 
print(predict)
predict = predict[predict["conf"] != -1]

try:
    detected = " ".join([str(int(a)) if isinstance(a, float) else str(a) for a in predict["text"].tolist()])
    confidence = predict["conf"].iloc[0]
    print("Converted detected:", detected)
    print("with confidence:", confidence)                  
except:
    pass

我得到:

   level  page_num  block_num  par_num  line_num  word_num  left  top  width  height       conf  text
4      5         1          1        1         1         1     4    4     25      16  95.180374   3.0
Converted detected: 3
with confidence: 95.180374

前导 0 未保留,结果是一个浮点数,我稍后必须将其转换为 int / string。有没有办法保留文本输出,使其与image_to_string() 相同?

【问题讨论】:

    标签: python dataframe python-tesseract


    【解决方案1】:

    不要使用 data.frame 作为输出类型,而是使用常规的 Python 字典:

    pytesseract.image_to_data(image, config = config, output_type = pytesseract.Output.DICT)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-11
      • 1970-01-01
      • 2014-09-01
      • 2011-10-01
      • 2014-02-14
      • 2012-11-28
      • 1970-01-01
      相关资源
      最近更新 更多