[오류 해결] paddleocr 사용 시 TypeError: '<' not supported between instances of 'tuple' and 'float'

hottogi·2023년 2월 15일
1

오류 발생

paddleocr로 얻어낸 result값을 이미지 바운딩 하기 위해

from paddleocr import PaddleOCR,draw_ocr
# Paddleocr supports Chinese, English, French, German, Korean and Japanese.
# You can set the parameter `lang` as `ch`, `en`, `fr`, `german`, `korean`, `japan`
# to switch the language model in order.
ocr = PaddleOCR(use_angle_cls=True, lang='en') # need to run only once to download and load model into memory
img_path = './imgs_en/img_12.jpg'
result = ocr.ocr(img_path, cls=True)
for line in result:
    print(line)
    
# draw result
from PIL import Image
image = Image.open(img_path).convert('RGB')
boxes = [line[0] for line in result]
txts = [line[1][0] for line in result]
scores = [line[1][1] for line in result]
im_show = draw_ocr(image, boxes, txts, scores, font_path='./fonts/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')

해당 코드 실행시 아래와 같은 오류 발생

해결 과정

먼저 result를 확인해 보자.

result[0]을 확인해 보자.

필요없는 차원이 하나 더 붙어 있음을 확인.

result = result[0] 으로 바깥의 한 차원을 제거하여 오류 해결

수정 코드

(8번째 줄에 result = result[0] 추가된 것만 바뀌었습니다)

from paddleocr import PaddleOCR,draw_ocr
# Paddleocr supports Chinese, English, French, German, Korean and Japanese.
# You can set the parameter `lang` as `ch`, `en`, `fr`, `german`, `korean`, `japan`
# to switch the language model in order.
ocr = PaddleOCR(use_angle_cls=True, lang='en') # need to run only once to download and load model into memory
img_path = './imgs_en/img_12.jpg'
result = ocr.ocr(img_path, cls=True)
result = result[0]
for line in result:
    print(line)
    
# draw result
from PIL import Image
image = Image.open(img_path).convert('RGB')
boxes = [line[0] for line in result]
txts = [line[1][0] for line in result]
scores = [line[1][1] for line in result]
im_show = draw_ocr(image, boxes, txts, scores, font_path='./fonts/simfang.ttf')
im_show = Image.fromarray(im_show)
im_show.save('result.jpg')

font_path는 각자 갖고 계신 폰트의 경로로 수정해주세요.

profile

0개의 댓글