Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 검색
- Angular Router
- route
- 아이오닉 스크롤 이벤트
- summary
- 모달
- 앵귤러 모달
- angular animation
- flex-1
- Router
- 스크롤 이벤트 감지
- mysql if
- 호버
- Oracle LISTAGG 사용 예시
- angular modal
- prisma
- angular route
- formgroup
- egov spring ajax 사용 예시
- TAILWIND
- 앵귤러 애니메이션
- Ionic modal
- angular button
- 옵저버블
- ajax 사용 예시
- 스크롤 이벤트
- scroll
- 셀렉트박스 커스텀
- modal
- ApexChart
Archives
- Today
- Total
깜놀하는 해므찌로
Python OCR 기반 tesseract 텍스트 인식 및 문자 연동 본문
반응형
SMALL
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
'IT' 카테고리의 다른 글
Java 자릿수 알아내는 법 / 한 자리 수 앞에 "0" 붙여 처리하는 방법 ex) 01,02...10, 11 (0) | 2022.07.07 |
---|---|
Egov Spring 데이터 베이스 DB 연동 방법 (0) | 2022.07.07 |
Java Gmail Library 자바 Gmail 연동 / 이메일 전송 라이브러리 (0) | 2022.07.06 |
ajax 통신 JavaScript try catch / if 조건 에러 / ajax 존재하지 않는 페이지 정보 읽어오기 꿀팁! (0) | 2022.07.06 |
Egov Spring ajax json 비동기 통신 사용 예시 (0) | 2022.07.06 |