-
Notifications
You must be signed in to change notification settings - Fork 65
/
py3_raspberry_pi.py
148 lines (130 loc) · 4.54 KB
/
py3_raspberry_pi.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/python3
import numpy as np
import cv2
from pyocr import pyocr
from pyocr import builders
from PIL import Image
from time import sleep
###############################################################
### Interactivity
###############################################################
drawing = False # true if mouse is pressed
ix,iy = -1,-1
rectangle = None
def define_rectangle(iy, ix, y, x):
x_sorted = sorted([ix, x])
y_sorted = sorted([iy, y])
return (x_sorted[0], y_sorted[0]), (x_sorted[1], y_sorted[1])
def is_rectangle(rect):
if rect != None\
and 0 != rect[0][0]-rect[1][0]\
and 0 != rect[0][1]-rect[1][1]:
return True
else:
return False
# mouse callback function
def draw_shape(event,x,y,flags,param):
global ix,iy,drawing,mode,rectangle
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
rectangle = define_rectangle(iy, ix, y, x)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
if not (ix == x and iy == y):
rectangle = define_rectangle(iy, ix, y, x)
# GUI INPUTS
thresh, erosion_iters, most_common_filter = 0, 0, 0
def reg_threshold(x):
global thresh
thresh = x
def reg_erode(x):
global erosion_iters
erosion_iters = x
def reg_filter(x):
global most_common_filter
most_common_filter = x
###############################################################
### Imagery
###############################################################
def initialize_images():
p_img = '/path/to/image/file.jpg'
img_temp = cv2.imread(p_img, 1)
# cv2.imshow('imagetemp',img_temp)
img = img_temp.copy()
return img, img_temp, None
###############################################################
### Processing
###############################################################
def processing():
global rectangle,thresh,erosion_iters,most_common_filter
#
r = cv2.getTrackbarPos('Threshold', 'Inputs') #changed from frame to Inputs
#
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_shape)
#
img_temp, img, roi = initialize_images()
rectangle = None
scale = 1
factor = 0.75
#
while(1):
sleep(0.2)
img = img_temp.copy()
#
### ROI ###########################################################
if is_rectangle(rectangle):
roi = img_temp[rectangle[0][1]:rectangle[1][1], rectangle[0][0]:rectangle[1][0]]
cv2.rectangle(img,rectangle[0],rectangle[1],(0,255,0),0)
else:
roi = None
### FILTER ########################################################
# retrieving parameters from GUI slidebars
### DISPLAY #######################################################
cv2.imshow('image',img)
if roi is not None: #changed from "if roi != None" which gave array related error
kernel = np.ones((5, 5), np.uint8)
roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
roi = cv2.threshold(roi, thresh, 255, cv2.THRESH_BINARY)[1]
roi = cv2.erode(roi, kernel, iterations=erosion_iters)
cv2.imshow('ROI',roi)
if roi is not None and not drawing: #changed from "if roi != None" which gave array related error
### OCR ###########################################################
tool = pyocr.get_available_tools()[0] #
lang = 'letsgodigital'#'letsgodigital'#"eng" #export TESSDATA_PREFIX=/path/to/tessdata/folder
txt = tool.image_to_string(Image.fromarray(roi), lang=lang, builder=builders.TextBuilder())
print(txt)
### ACTIONS #######################################################
k = cv2.waitKey(1) & 0xFF
if k == ord('c'):
img_temp, img, roi = initialize_images()
img = img_temp.copy()
rectangle, roi = None, None
pass
elif k == ord('r'):
# todo: ROI resizing
scale *= factor
print(scale)
img = cv2.resize(img, (0,0), fx=factor, fy=factor)
img_temp = img.copy()
elif k == 27: #esc key
cv2.destroyAllWindows()
break
### CLEANUP #######################################################
#cv2.waitKey(0) moved to inside esc key check
#cv2.destroyAllWindows()
if __name__ == '__main__':
print("Starting..")
#
cv2.namedWindow('Inputs')
display = np.zeros((1, 200, 3), np.uint8)
cv2.imshow('Inputs', display)
cv2.createTrackbar('Threshold', 'Inputs', int(thresh), 255, reg_threshold)
cv2.createTrackbar('Erode', 'Inputs', int(erosion_iters), 6, reg_erode)
cv2.createTrackbar('Filter', 'Inputs', int(most_common_filter), 10, reg_filter)
#
processing()
pass