我正在尝试使用 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
指导我哪里做错了,以及如何克服这个错误。
您确定图像路径存在吗?
话虽这么说..
从你的回溯来看:
其次,您提供的回溯表明您分配给“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>
祝你好运!