Skip to content

Instantly share code, notes, and snippets.

@smd877
Last active January 14, 2021 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smd877/df98da60d00cff3f0e2d8b0a18cead40 to your computer and use it in GitHub Desktop.
Save smd877/df98da60d00cff3f0e2d8b0a18cead40 to your computer and use it in GitHub Desktop.
OpenCVとTesseractを使ってポケモンの特性を文字認識する
import cv2
from tesserocr import PyTessBaseAPI, PSM
from PIL import Image
# 環境で変わると思うので変数として持つ
DEVICE_ID = 0
# キャプチャの読み込み
cap = cv2.VideoCapture(DEVICE_ID)
# デフォルトの640x480だと文字認識で粗さがネックになるのでフルHDにしておく
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
for i in range(10):
ret,frame = cap.read()
# frameそのものを画像保存 デバッグ活用等
cv2.imwrite('temp.jpg', frame)
# frameから画像切り取り(高さ60幅300)
trim_img = frame[610 : 670, 1480 : 1780]
# 二値化させる
gray_trim_img = cv2.cvtColor(trim_img, cv2.COLOR_BGR2GRAY)
ret, thresh_trim_img = cv2.threshold(gray_trim_img, 0, 255, cv2.THRESH_OTSU)
# 二値化した画像も念の為保存 デバッグ活用等
cv2.imwrite('thresh_trim.jpg', thresh_trim_img)
# Tesseractで扱えるようにPIL利用
pil_img = Image.fromarray(thresh_trim_img)
# OCR開始
api = PyTessBaseAPI(psm=PSM.AUTO, lang='jpn')
api.SetImage(pil_img)
ret = api.GetUTF8Text()
# 余計な空白を除去
ret = ret.replace(' ','')
print(ret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment