Getting UnboundLocalError: local variable ‘img’ referenced before assignment while applying craft-text-detector to image in python

I’m trying to get image crops using craft-text-detector in python.

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

Code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># 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()
</code>
<code># 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() </code>
# 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()

Above code is working fine and saving image-crops in /tmp/advance-outputs.

But above code is taking image_path, I want to give it PIL Image. For this I have change image_path into PIL Image:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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()
`
</code>
<code>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() ` </code>
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()
`

But this code is giving me error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$ 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
</code>
<code>$ 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 </code>
$ 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

Guide me where I’m doing wrong, and how can I surpass this error.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật