-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotomation.py
125 lines (104 loc) · 5.53 KB
/
otomation.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from selenium import webdriver
import time
from config import *
import cv2
import requests
import json
import shutil
import os
from google.oauth2.credentials import Credentials
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
from googleapiclient.discovery import build
import io
class OcrOtomation():
def __init__(self):
self.executable_path = r"C:\Users\binbi\PycharmProjects\OcrOtomation\chromedriver.exe"
self.ocr_url = "https://www.onlineocr.net/"
self.images_dir = r"C:\Users\binbi\PycharmProjects\OcrOtomation\Images"
self.thresholded_images_dir = r"C:\Users\binbi\PycharmProjects\OcrOtomation\ThresholdedImages"
self.text_converted_dir = r"C:\Users\binbi\PycharmProjects\OcrOtomation\TextConverted"
self.text_converted_gdrive_dir = r"C:\Users\binbi\PycharmProjects\OcrOtomation\TextConvertedGdrive"
def get_logged_in(self):
self.browser = webdriver.Chrome(executable_path=self.executable_path)
login_page = r"https://www.onlineocr.net/account/login"
self.browser.get(login_page)
mail_adress = self.browser.find_element_by_xpath('//*[@id="MainContent_txtUserName"]').send_keys(ocr_mail_adress)
password = self.browser.find_element_by_xpath('//*[@id="MainContent_txtPassword"]').send_keys(ocr_mail_password)
get_logged_in_button = self.browser.find_element_by_xpath('//*[@id="MainContent_btnLogin"]').click()
time.sleep(1)
def recognize(self,file_url):
img = cv2.imread(file_url)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
adaptive_threshold = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY, 81, 11)
save_path = os.path.join(self.thresholded_images_dir,file_url)
cv2.imwrite(save_path,adaptive_threshold)
return save_path
def get_request(self,thresholded_file_path,which_account=1):
request_url = "http://www.ocrwebservice.com/restservices/processDocument?language=turkish&outputformat=docx"
file_absolute_path = thresholded_file_path
with open(file_absolute_path, "rb") as image_file:
image_data = image_file.read()
if which_account == 1:
posted_request = requests.post(request_url,data=image_data,auth=(ocr_username,ocr_licence_code))
else:
posted_request = requests.post(request_url, data=image_data, auth=(ocr_username2, ocr_licence_code2))
if posted_request.status_code == 401:
print("Unauthorizded user informations")
else:
response = json.loads(posted_request.content)
file_response = requests.get(response["OutputFileUrl"], stream=True)
filename_without_jpg = thresholded_file_path.split("\\")[-1].replace(".jpeg","")
save_path = f"{os.path.join(self.text_converted_dir, filename_without_jpg)}.docx"
with open(save_path, 'wb') as output_file:
shutil.copyfileobj(file_response.raw, output_file)
print("Dosya docx formatına başarıyla dönüştürüldü.")
def path_images(self,return_file_names=False):
paths = [os.path.join(self.images_dir,name) for name in os.listdir(self.images_dir)]
if return_file_names:
file_names = [file_name.replace(".jpeg","").replace(".jpg","") for file_name in os.listdir(self.images_dir)]
paths_and_names = zip(paths,file_names)
return paths_and_names
else:
return paths
def upload_file_to_gdrive(self,file_name,file_path):
SCOPES = ['https://www.googleapis.com/auth/drive']
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
service = build("drive", "v3", credentials=creds)
mime = "image/jpeg"
file_metadata = {"name":file_name,"mimeType": "application/vnd.google-apps.document"}
media = MediaFileUpload(file_path,mimetype= mime)
file = service.files().create(
body=file_metadata,
media_body= media,
fields = "id").execute()
file_id = file.get("id")
return file_id
def download_file_from_gdrive(self,file_id,file_name):
SCOPES = ['https://www.googleapis.com/auth/drive']
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
service = build("drive", "v3", credentials=creds)
mime = "application/pdf"
request = service.files().export_media(fileId = file_id, mimeType= mime)
file_name = os.path.join(self.text_converted_gdrive_dir,file_name + ".pdf")
fh = io.FileIO(file_name, 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
def run(self,google_drive=True):
file_ids = []
for path,file_name in self.path_images(return_file_names=True):
recognized_path = self.recognize(path)
# try:
# self.get_request(recognized_path)
# except Exception:
# self.get_request(recognized_path,which_account=2)
if google_drive:
file_id = self.upload_file_to_gdrive(file_path=path,file_name=file_name)
self.download_file_from_gdrive(file_id=file_id,file_name=file_name)
if __name__ == "__main__":
ocr = OcrOtomation()
ocr.run()