-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtextRecognition.py
69 lines (62 loc) · 2.47 KB
/
textRecognition.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
from pytesser import *
from PIL import Image
import os
import cv2
from matplotlib import pyplot as plt
import imutils
import sys
import textDetection
import formatText
#Not fully developed, use contourBasedTextRecognition instead
def readText(image, contours):
for contour in contours:
[x, y, w, h] = contour
temp_image = image[y:y+h, x:x+w]
#temp_image = imutils.resize(temp_image, width=50)
#print(temp_image.shape, x, y, w, h)
cv2.imshow('temp', temp_image)
cv2.waitKey()
cv2.imwrite('temp_image.jpg', temp_image)
print(image_to_string(Image.open('temp_image.jpg')).strip())
sys.stdout.flush()
os.remove('temp_image.jpg')
def contourBasedTextRecognition(image, contours, minimizeContours = False):
dates = []
lots = []
for contour in contours:
#Look for expiration date
if len(contour) == 4: #If contours is just a normal set of contours (i.e. you passed in the variable 'contours' in main.py)
[x, y, w, h] = contour
else: #If contours is from the subImageLocation file (i.e. you passed in the variable 'output' in main.py)
[x, y, w, h], _ = contour
if minimizeContours: #kind of hackish, it just works
temp_image = image[int(y+h/2):y+h, int(x+w/2.4):x+w]
else:
temp_image = image[y:y+h, x:x+w]
cv2.imwrite('temp_image.jpg', temp_image)
image_text = image_to_string(Image.open('temp_image.jpg')).strip() #use PyTesser to extract text
dates.append(image_text)
formattedDate = formatText.formatDate(image_text)
if formattedDate:
date_location = [x, y, w, h]
#cv2.imshow('temp', temp_image)
#cv2.waitKey()
#print(image_text)
#sys.stdout.flush()
#Look for lot number (TODO: separate this into a separate function)
if minimizeContours:
temp_image = image[y:int(y+h/2), x:x+w]
else:
temp_image = image[y:y+h, x:x+w]
cv2.imwrite('temp_image.jpg', temp_image)
image_text = image_to_string(Image.open('temp_image.jpg')).strip()
lots.append(image_text)
formattedLot = formatText.formatLot(image_text)
if formattedLot:
log_location = [x, y, w, h]
#cv2.imshow('temp', temp_image)
#cv2.waitKey()
#print(image_text)
#sys.stdout.flush()
os.remove('temp_image.jpg')
return (dates, lots, date_location, log_location)