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:
<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
:
<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:
<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