-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtess.1.py
115 lines (96 loc) · 3.14 KB
/
tess.1.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
# USAGE
# python ocr.py --image images/example_01.png
# python ocr.py --image images/example_02.png --preprocess blur
# import the necessary packages
from PIL import Image
from fpdf import FPDF
import random
import datetime
import pytesseract
import argparse
import cv2
import os
import os.path
# crud image
from CrudImage import CrudImage
from Crawler import Crawler
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-n", "--name", required=True, default="text2",
help="name to save file")
ap.add_argument("-i", "--image", required=True, default='images/documento-de-posio-oficial-2012-1-728.jpg, images/ex02.jpg',
help="path to input image to be OCR'd")
ap.add_argument("-s", "--storage", type=str,
help="location where to move the files")
ap.add_argument("-p", "--preprocess", type=str, default="thresh",
help="type of preprocessing to be done")
args = vars(ap.parse_args())
pdf = FPDF()
storage = args['storage']
images = args['image'].split(",")
text = str(datetime.datetime.now())+"\n"
for src in images:
src = src.strip()
# print(src)
if len(src) <= 1:
break
# img =Image.open(src)
# img.show()
# load the example image and convert it to grayscale
image = cv2.imread(src)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# check to see if we should apply thresholding to preprocess the
# image
if args["preprocess"] == "thresh":
gray = cv2.threshold(gray, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
# make a check to see if median blurring should be done to remove
# noise
elif args["preprocess"] == "blur":
gray = cv2.medianBlur(gray, 3)
# write the grayscale image to disk as a temporary file so we can
# apply OCR to it
# filename = "{address}/{name}.png".format(address = "./temp", name = os.getpid())
filename = "{address}/{name}.png".format(address = '/var/www/html/temp/', name = (random.randint(0,5000)))
cv2.imwrite(filename, gray)
newFileName = src.split('/')[-1].split('.')[0]
# load the image as a PIL/Pillow image, apply OCR, and then delete
# the temporary file
tempImage = Image.open(filename)
# print(filename)
text += pytesseract.image_to_string(tempImage)
#text = text.encode('utf-8')
width, height = tempImage.size
if width > height:
pdf.add_page("L")
size = 298
else:
pdf.add_page("P")
size = 210
pdf.image(filename, x = 0, y = 0, w = 210)
# save image in folder images
# fileNameImage = "{address}/{name}".format(address = "./images", name = src.split('/')[-1])
# cv2.imwrite(fileNameImage, image)
os.remove(filename)
cv2.waitKey(0)
filename = args["name"]
counter = 1
newFileName = filename
while(os.path.exists(storage+''+newFileName+'.pdf')):
newFileName = filename+"("+str(counter)+")"
counter += 1
pdfFileName = storage+''+newFileName+'.pdf'
pdf.output(pdfFileName, 'F')
# Create file txt
counter = 1
newFileName = filename
while(os.path.exists(storage+''+newFileName+'.txt')):
newFileName = filename+"("+str(counter)+")"
counter += 1
arqui = open(storage+''+newFileName+'.txt', 'w')
arqui.write(text)
arqui.close()
# crud = CrudImage()
# filename = pdfFileName.split('/')[-1]
# getIndex = Crawler().insetDocument(filename, text)
print('Ok')