-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOCR.py
56 lines (43 loc) · 1.48 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import tkinter as tk
from tkinter import filedialog, messagebox
from aip import AipOcr # baidu-aip
import os
import re
import time
import pyperclip
os.chdir('/Users/**/Desktop/')
def input_file_path():
window = tk.Tk()
window.title('(^_^))')
window.geometry('220x20')
l = tk.Label(window, text='"Pls Select The File"')
l.pack()
inpath = filedialog.askopenfilename(parent=window,
initialdir=os.getcwd(),
title="Please select a file:",
filetypes=[('all files', '.*')])
expath = re.sub("\..*", ".txt", inpath)
return inpath, expath
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
if __name__ == "__main__":
now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
now1 = time.time()
APP_ID = '21808493'
API_KEY = 'xXUZ0xzi2b7LQgwoYilrulBE'
SECRET_KEY = 'LxyP9CP6uUUGAXKq62mMRd6IAEed7hYA'
pic, output = input_file_path()
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
image = get_file_content(pic)
text = client.basicAccurate(image)
result = text["words_result"]
lst = ''
for i in result:
lst = lst + i["words"] + '\n'
pyperclip.copy(lst)
print(pyperclip.paste())
now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
now2 = time.time()
now3 = now2 - now1
messagebox.showinfo('结束', '结束,用时'+str(now3)+'s')