깜놀하는 해므찌로

Python OCR 기반 tesseract 텍스트 인식 및 문자 연동 본문

IT

Python OCR 기반 tesseract 텍스트 인식 및 문자 연동

agnusdei1207 2022. 7. 6. 17:06
반응형
SMALL

CCTV 가 갑자기 중지 될 시 담당자에게 문자 알림을 주는 서비스 구현

Google CoLab 구글 코랩 활용

%matplotlib inline

!sudo apt install tesseract-ocr
!pip3 install pytesseract
!pip3 install opencv-contrib-python # 주요 및 추가 모듈 설치
!pip3 install Pillow==9.1.0
!pip3 install coolsms_python_sdk

 

1. OCR 및 이미지 처리 라이브러리 설치

import pytesseract
import shutil
import os
import random
from PIL import Image
import matplotlib.pyplot as plt
import cv2
import numpy as np
import sys
from sdk.api.message import Message
from sdk.exceptions import CoolsmsException
from datetime import datetime
from flask import Flask

2. 라이브러리 import

large = cv2.imread('/content/sample_data/zzz.png', cv2.IMREAD_COLOR)
rgb = cv2.pyrDown(large)
# rgb = large
small = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
grad = cv2.morphologyEx(small, cv2.MORPH_GRADIENT, kernel)
_, bw = cv2.threshold(grad, 0.0, 255.0, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 1))
connected = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel)
# using RETR_EXTERNAL instead of RETR_CCOMP
contours, hierarchy = cv2.findContours(connected.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
mask = np.zeros(bw.shape, dtype=np.uint8)
for idx in range(len(contours)):
    x, y, w, h = cv2.boundingRect(contours[idx])
    mask[y:y+h, x:x+w] = 0
    cv2.drawContours(mask, contours, idx, (0, 0, 0), 0)
    r = int(cv2.countNonZero(mask[y:y+h, x:x+w])) / (w * h)
    if r > 0.45 and w > 8 and h > 8:
        cv2.rectangle(rgb, (x, y), (x+w-1, y+h-1), (0, 0, 0), 0)
# show image with contours rect

cv2.rectangle(rgb, (x, y), (x+w-1, y+h-1), (0, 0, 0), 0)
cv2.waitKey()
h = h + 5 # 텍스트가 짤리는 것을 방지하기 위해 여유 공간 확보
w = w + 5
x = x - 5
y = y - 5
print(x,y, x+w, y+h) # 좌표 수치 확인

3. 테스트용 이미지 경로 설정

4. 텍스트 부분 좌상단 우하단 좌표 가져오기

rec = cv2.rectangle(rgb, (x, y), (x+w, y+h), (0, 255, 0), 1)
plt.imshow(rec)

 

5. 잘 가져왔는지 확인

img = Image.fromarray(rec) 
area = (x,y, x+w, y+h) # 좌표 저장
cropped_img = img.crop(area)
 
np_array_cropped_img = np.array(cropped_img)
 
plt.imshow(np_array_cropped_img)
plt.axis("on")
plt.show()

6. 이미지의 필요한 부분한 잘라내서 추출

im = Image.fromarray(np_array_cropped_img)
im.save("stream_check.png")

7. 본격적으로 텍스트를 판단하기 위해 이미지 저장

config = ('-l kor+eng --oem 3 --psm 11')
msg = pytesseract.image_to_string("stream_check.png", config=config)

now = datetime.now()
print("OCR 추출된 텍스트 : ",msg)

if msg.find('Stream unavailable') == 0:
  print("스트리밍 중지!! 현재 시각 : ", now)

else:
  print("스트리밍 정상 작동중")

8. 추출된 텍스트 확인 및 감지 확인

def sms():
  # set api key, api secret
  api_key = "..."
  api_secret = "..."
  ## 4 params(to, from, type, text) are mandatory. must be filled
  params = dict()
  params['type'] = 'sms' # Message type ( sms, lms, mms, ata )
  params['to'] = '...' 
  params['from'] = '...'
  params['text'] = '스트림이 중지됐습니다. 확인하세요!'
  cool = Message(api_key, api_secret)
  try:
    response = cool.send(params)  
    print("Success Count : %s" % response['success_count'])
    print("Error Count : %s" % response['error_count'])
    print("Group ID : %s" % response['group_id'])
    if "error_list" in response:
      print("Error List : %s" % response['error_list'])
  except CoolsmsException as e:
    print("Error Code : %s" % e.code)
    print("Error Message : %s" % e.msg)
    sys.exit()

9. 문자 API 연동 함수 선언 및 감지됏을 때 호출

10. 문자 도착 확인

 

반응형
LIST