python字符串比较使用tesseract和opencv

时间:2015-12-02 17:16:10

标签: python string opencv tesseract

使用图像调用以下内容可以得到正确的结果。 但是,字符串相等性因未知原因而失败。

def image_to_string(im, cleanup = cleanup_scratch_flag):
    """Converts im to file, applies tesseract, and fetches resulting text.
    If cleanup=True, delete scratch files after operation."""
    try:
        util.image_to_scratch(im, scratch_image_name)
        call_tesseract(scratch_image_name, scratch_text_name_root)
        text = util.retrieve_text(scratch_text_name_root)
    finally:
        if cleanup:
            util.perform_cleanup(scratch_image_name, scratch_text_name_root)
    return text


cityname=image_to_string(im)
print cityname # this statement prints 'London' without quotes
print cityname=='London' # This statement is always false

2 个答案:

答案 0 :(得分:2)

要了解正在发生的事情,请尝试print repr(cityname)print type(cityname)

同时尝试print str(cityname) == 'London';它应该导致True

很可能cityname 是一个字符串,而是其他一些特定于OpenCV的对象类型。它的str()方法可能会返回已识别的字符串值;这是print打印的内容。

答案 1 :(得分:0)

print cityname
print repr(cityname) 
print type(cityname)
print str(cityname) == 'LONDON'

输出

LONDON
'LONDON\n\n'
<type 'str'>
False

print cityname.rstrip() # is returning true