Create text_ocr.py
Browse files- text_ocr.py +118 -0
text_ocr.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from googleapiclient.discovery import build
|
| 2 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
| 3 |
+
from google.auth.transport.requests import Request
|
| 4 |
+
import io
|
| 5 |
+
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
|
| 6 |
+
import os
|
| 7 |
+
import pickle
|
| 8 |
+
from tqdm import tqdm
|
| 9 |
+
import cv2
|
| 10 |
+
import re
|
| 11 |
+
import subprocess
|
| 12 |
+
|
| 13 |
+
class TextOcr():
|
| 14 |
+
def __init__(self, ocrType):
|
| 15 |
+
self.service=None
|
| 16 |
+
self.ocrType=ocrType
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def getGoogleCred(self,):
|
| 20 |
+
SCOPES = ['https://www.googleapis.com/auth/drive']
|
| 21 |
+
creds = None
|
| 22 |
+
# The file token.pickle stores the user's access and refresh tokens, and is
|
| 23 |
+
# created automatically when the authorization flow completes for the first
|
| 24 |
+
# time.
|
| 25 |
+
if os.path.exists('token.pickle'):
|
| 26 |
+
with open('token.pickle', 'rb') as token:
|
| 27 |
+
creds = pickle.load(token)
|
| 28 |
+
# If there are no (valid) credentials available, let the user log in.
|
| 29 |
+
if not creds or not creds.valid:
|
| 30 |
+
if creds and creds.expired and creds.refresh_token:
|
| 31 |
+
creds.refresh(Request())
|
| 32 |
+
else:
|
| 33 |
+
flow = InstalledAppFlow.from_client_secrets_file(
|
| 34 |
+
'credentials.json', SCOPES)
|
| 35 |
+
creds = flow.run_local_server(port=0)
|
| 36 |
+
# Save the credentials for the next run
|
| 37 |
+
with open('token.pickle', 'wb') as token:
|
| 38 |
+
pickle.dump(creds, token)
|
| 39 |
+
service = build('drive', 'v3', credentials=creds)
|
| 40 |
+
return service
|
| 41 |
+
|
| 42 |
+
def filterText(self,inputText):
|
| 43 |
+
inputText = re.sub('[\\\\+/§◎*)@<>#%(&=$_\-^01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz:;«¢~「」〃ゝゞヽヾ一●▲・ヽ÷①↓®▽■◆『£〆∴∞▼™↑←]', '', inputText) #remove special char
|
| 44 |
+
inputText = ''.join(inputText.split()) #remove whitespace
|
| 45 |
+
return inputText
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def getTextGoogleOcr(self,img):
|
| 49 |
+
if self.service is None:
|
| 50 |
+
self.service=self.getGoogleCred()
|
| 51 |
+
|
| 52 |
+
exceptionCount=0
|
| 53 |
+
while exceptionCount<5:
|
| 54 |
+
try:
|
| 55 |
+
#https://tanaikech.github.io/2017/05/02/ocr-using-google-drive-api/
|
| 56 |
+
txtPath = 'googleocr.txt' # Text file outputted by OCR
|
| 57 |
+
imgPath="googleocr.jpg"
|
| 58 |
+
cv2.imwrite(imgPath, img)
|
| 59 |
+
mime = 'application/vnd.google-apps.document'
|
| 60 |
+
res = self.service.files().create(
|
| 61 |
+
body={'name': imgPath,
|
| 62 |
+
'mimeType': mime },
|
| 63 |
+
media_body=MediaFileUpload(imgPath, mimetype=mime, resumable=True) ).execute()
|
| 64 |
+
downloader = MediaIoBaseDownload(
|
| 65 |
+
io.FileIO(txtPath, 'wb'),
|
| 66 |
+
self.service.files().export_media(fileId=res['id'], mimeType="text/plain"))
|
| 67 |
+
done = False
|
| 68 |
+
while done is False:
|
| 69 |
+
status, done = downloader.next_chunk()
|
| 70 |
+
self.service.files().delete(fileId=res['id']).execute()
|
| 71 |
+
with open(txtPath, "r", encoding="utf-8" ) as f: text_google = f.read() #txt to str
|
| 72 |
+
text_google=text_google.replace('\ufeff', '')
|
| 73 |
+
text_google=self.filterText(text_google)
|
| 74 |
+
except:
|
| 75 |
+
exceptionCount+=1
|
| 76 |
+
continue
|
| 77 |
+
break
|
| 78 |
+
return text_google
|
| 79 |
+
|
| 80 |
+
def getTextWindowOcr(self,img):
|
| 81 |
+
inputFile="lib_/input.jpg"
|
| 82 |
+
outputFile='lib_/output.txt'
|
| 83 |
+
cv2.imwrite(inputFile, img)
|
| 84 |
+
p = subprocess.Popen(('./lib_/winocr/winocr.exe'))
|
| 85 |
+
p.wait()
|
| 86 |
+
with open(outputFile, "r", encoding="utf-8" ) as f: text = f.read() #txt to str
|
| 87 |
+
if os.path.exists(inputFile): os.remove(inputFile)
|
| 88 |
+
if os.path.exists(outputFile): os.remove(outputFile)
|
| 89 |
+
text=self.filterText(text)
|
| 90 |
+
return text
|
| 91 |
+
|
| 92 |
+
def checkWindowOcr(self,):
|
| 93 |
+
p = subprocess.Popen(('./lib_/winocr/winocr.exe'))
|
| 94 |
+
p.wait()
|
| 95 |
+
if os.path.exists("./lib_/loadResult.txt"):
|
| 96 |
+
with open("./lib_/loadResult.txt", "r", encoding="utf-8" ) as f: text = f.read() #txt to str
|
| 97 |
+
if text=="True":
|
| 98 |
+
return True
|
| 99 |
+
return False
|
| 100 |
+
|
| 101 |
+
def getTextFromImg(self,imgPath,rectList,textOnlyFolder):
|
| 102 |
+
fileName=os.path.basename(imgPath)
|
| 103 |
+
img = cv2.imread(textOnlyFolder+fileName)
|
| 104 |
+
textList=[]
|
| 105 |
+
rectP,rect=rectList
|
| 106 |
+
for x1,y1,x2,y2 in rectP:
|
| 107 |
+
# Cropping the text block for giving input to OCR
|
| 108 |
+
cropped = img[y1: y2, x1: x2]
|
| 109 |
+
|
| 110 |
+
if self.ocrType=="googleocr":
|
| 111 |
+
text=self.getTextGoogleOcr(cropped)
|
| 112 |
+
elif self.ocrType=="windowocr":
|
| 113 |
+
text=self.getTextWindowOcr(cropped)
|
| 114 |
+
textList+=[text]
|
| 115 |
+
|
| 116 |
+
return textList
|
| 117 |
+
|
| 118 |
+
|