I’m trying to create image text crops using craft-text-detector in python. For this first I made a simple program which is taking image from path and making image crops.
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
)
import os
image_dir = 'sample-images/'
output_dir = '/tmp/advance-outputs/'
filename = 'image.png'
image_path = os.path.join(image_dir, filename)
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 = craft.detect_text(image_path)
image = read_image(image_path)
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
)
exported_file_paths = export_detected_regions(
image=image,
regions=prediction_result["boxes"],
output_dir=output_dir,
rectify=True
)
export_extra_results(
image=image,
regions=prediction_result["boxes"],
heatmaps=prediction_result["heatmaps"],
output_dir=output_dir
)
empty_cuda_cache()
Above code is working fine. Next time I’ve tried to make it a service using FastApi and instead of image paths, I used image bytes and tried to do make text crops using craft-text-detector.
import base64
import logging
import cv2
from fastapi import FastAPI, HTTPException
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
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
)
import numpy as np
from pydantic import BaseModel, ValidationError
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI()
class ImageRequest(BaseModel):
image_bytes: bytes
output_dir: str
class TextDetectionResponse(BaseModel):
message: str
output_dir: str
def detect_text(image_bytes: bytes, output_dir: str):
image_array = np.frombuffer(image_bytes, dtype=np.uint8)
mat_like_image = cv2.imdecode(image_array, cv2.IMREAD_UNCHANGED)
refine_net = load_refinenet_model(cuda=False)
craft_net = load_craftnet_model(cuda=False)
image = read_image(mat_like_image)
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
)
exported_file_paths = export_detected_regions(
image=image,
regions=prediction_result["boxes"],
output_dir=output_dir,
rectify=True
)
export_extra_results(
image=image,
regions=prediction_result["boxes"],
heatmaps=prediction_result["heatmaps"],
output_dir=output_dir
)
empty_cuda_cache()
@app.post("/detect-text-and-make-crops")
async def detect_text_and_make_crops(request: ImageRequest):
try:
output_dir = request.output_dir
updated_image_bytes = base64.b64decode(request.image_bytes)
detect_text(updated_image_bytes, output_dir)
text_detection_result = {
'message': 'Text Detection Completed, and Crops are Saved',
'output_dir': output_dir
}
text_detection_response = TextDetectionResponse(**text_detection_result)
return JSONResponse(content=jsonable_encoder(text_detection_response))
except ValidationError as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == '__main__':
import uvicorn
uvicorn.run("__main__:app", host="0.0.0.0", port=8090)
Now this code is working for some images, but the problem is it is giving error for some images:
INFO: 127.0.0.1:36386 - "POST /detect-text-and-make-crops HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "../python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 408, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "../python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
return await self.app(scope, receive, send)
File "../project.py", line 73, in detect_text
export_extra_results(
File "../python3.10/site-packages/craft_text_detector/file_utils.py", line 246, in export_extra_results
cv2.polylines(
cv2.error: OpenCV(4.5.4) :-1: error: (-5:Bad argument) in function 'polylines'
> Overload resolution failed:
> - Layout of the output array img is incompatible with cv::Mat
> - Expected Ptr<cv::UMat> for argument 'img'
I’ve tried to search it, and found that it may be because function polylines requires specific data types. But I’m unable to resolve it. Guide me how can I resolve above thing?