-
Notifications
You must be signed in to change notification settings - Fork 15
/
ocr.py
35 lines (25 loc) · 809 Bytes
/
ocr.py
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
29
30
31
32
33
from googleVisionApi import GoogleVisionApi
def inferExtension(filePath):
extension = filePath.split(".")[-1].lower()
if len(extension) > 0:
return extension
raise Exception("No format found for: " + filePath)
def filename(filePath):
extension = inferExtension(filePath)
name = filePath.split("/")[-1][:-(len(extension)+1)]
return name
class Ocr:
def __init__(self):
self.googleApi = GoogleVisionApi()
def clearAll(self):
self.googleApi.clearAll()
def processFile(self,filePath, dstFolderPath):
extension = inferExtension(filePath)
if extension in ["jpg","jpeg","png"]:
response = self.googleApi.request(filePath)
with open(dstFolderPath + filename(filePath)+'.json','w') as f:
f.write(response)
else:
print("Invalid Extension:")
print(extension)
print("\n")