获取 UnboundLocalError:在将 craft-text- detector 应用于 python 中的图像时,在赋值之前引用局部变量“img”

问题描述 投票:0回答:1

我正在尝试使用 python 中的 craft-text- detector 来获取图像作物。

使用

craft-text-detector==0.4.3
torch==1.11.0
torchvision==0.12.0
numpy==1.23.0

代码:

# import craft functions
from craft_text_detector import (
    Craft,
    read_image,
    load_craftnet_model,
    load_refinenet_model,
    get_prediction,
    export_detected_regions,
    export_extra_results,
    empty_cuda_cache
    )

# set image path and export folder directory
output_dir = '/tmp/advance-outputs/'

# construct full path to the image
image_path = 'sample-images/image.jpg'
         
refine_net = load_refinenet_model(cuda=False)
craft_net = load_craftnet_model(cuda=False)
        
craft = Craft(output_dir=output_dir, crop_type="poly", cuda=False)  # Create the Craft instance here

# apply craft text detection to the current image and export detected regions
prediction_result = craft.detect_text(image_path)

# read image
image = read_image(image_path)

# load models
        
# perform prediction
prediction_result = get_prediction(
 image=image,
 craft_net=craft_net,
 refine_net=refine_net,
 text_threshold=0.7,
 link_threshold=0.4,
 low_text=0.4,
 cuda=False,
 long_size=1280
 )

# export detected text regions
exported_file_paths = export_detected_regions(
image=image,
regions=prediction_result["boxes"],
output_dir=output_dir,
rectify=True
)

# export heatmap, detection points, box visualization
export_extra_results(
 image=image,
 regions=prediction_result["boxes"],
 heatmaps=prediction_result["heatmaps"],
 output_dir=output_dir
)

# unload models from gpu
empty_cuda_cache()

上面的代码工作正常,并将

image-crops
保存在
/tmp/advance-outputs
中。

但是上面的代码正在接受

image_path
,我想给它
PIL Image
。为此,我将
image_path
更改为
PIL Image
:

from PIL import Image
from craft_text_detector import (
    Craft,
    read_image,
    load_craftnet_model,
    load_refinenet_model,
    get_prediction,
    export_detected_regions,
    export_extra_results,
    empty_cuda_cache
    )


output_dir = '/tmp/advance-outputs/'

image_path = 'sample-images/image.jpg'

pil_image = Image.open(image_path)
         
refine_net = load_refinenet_model(cuda=False)
craft_net = load_craftnet_model(cuda=False)
        
craft = Craft(output_dir=output_dir, crop_type="poly", cuda=False)  

        
prediction_result = get_prediction(
 image=pil_image,
 craft_net=craft_net,
 refine_net=refine_net,
 text_threshold=0.7,
 link_threshold=0.4,
 low_text=0.4,
 cuda=False,
 long_size=1280
 )

exported_file_paths = export_detected_regions(
image=pil_image,
regions=prediction_result["boxes"],
output_dir=output_dir,
rectify=True
)

export_extra_results(
 image=pil_image,
 regions=prediction_result["boxes"],
 heatmaps=prediction_result["heatmaps"],
 output_dir=output_dir
)

empty_cuda_cache()
`

但是这段代码给了我错误:

$ python3 text_detection_advanced.py 
Traceback (most recent call last):
  File "../pythoncode/text_detection_advanced.py", line 339, in <module>
    prediction_result = get_prediction(
  File "../site-packages/craft_text_detector/predict.py", line 47, in get_prediction
    image = image_utils.read_image(image)
  File "../site-packages/craft_text_detector/image_utils.py", line 28, in read_image
    return img
UnboundLocalError: local variable 'img' referenced before assignment

指导我哪里做错了,以及如何克服这个错误。

python ocr
1个回答
0
投票

您确定图像路径存在吗?

  • 由于这个图像加载函数是一个库导入函数,很可能你的路径是无效的。

话虽这么说..

从你的回溯来看:

  • 首先,您需要在返回变量“img”之前创建它,无论代码的其余部分如何,这是一个很好的做法,可以防止异常

其次,您提供的回溯表明您分配给“image”而不是“img”,因此“img”永远不会被设置:

image = image_utils.read_image(...)
#should be:
img = image_utils.read_image(...)

第三,在以任何一种方式访问之前检查文件是否存在是一个很好的做法。 所以我建议:

imgPath = "<Your path>"
img = 0
if os.path.exists(ImgPath):
    img =  image_utils.read_image(ImgPath)
return img
    ...
if img not 0:
   <Rest of your code>

祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.