This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
174 lines (133 loc) · 4 KB
/
test.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
import time
from io import BytesIO
from time import sleep
import PIL
import PIL.ImageOps
import cv2
import imagehash
import numpy as np
import pytesseract
from PIL import Image
# from picamera import PiCamera
def blurr_ratio(image):
return cv2.Laplacian(image, cv2.CV_64F).var()
def trim(im):
from PIL import Image, ImageChops
bg = Image.new(im.mode, im.size, im.getpixel((0, 0)))
diff = ImageChops.difference(im, bg)
diff = ImageChops.add(diff, diff, 2.0, -100)
bbox = diff.getbbox()
if bbox:
return im.crop(bbox)
# def computehash(image_stream):
# dimage = imagehash.dhash(image_stream)
# return dimage
def image_opt(my_stream, cropxt, cropyt, cropxb, cropyb):
image = Image.open(my_stream)
# image.save("test2.png")
im = image.convert("L")
# im.show()
im = PIL.ImageOps.autocontrast(im, 0.6)
th = 170 # the value has to be adjusted for an image of interest
im = im.point(lambda i: i < th and 255)
im = trim(im)
width = im.size[0]
height = im.size[1]
a = (int((width * cropyt) / 2), int((height * cropxt) / 2), width - int((width * cropyb) / 2),
height - int((height * cropxb) / 2))
return im.crop(a)
# im.show()
def hash_test():
lis = os.listdir("motomed_live")
for i in range(len(lis)):
lis[i] = "motomed_live/" + lis[i]
hashlist = []
star1 = time.clock()
for i in range(len(lis)):
start = time.clock()
image = image_opt(lis[i], 0.2, 0.5, 0.3, 0.5)
ihash = imagehash.dhash(image, hash_size=32)
end = time.clock()
if i > 0:
distance = ihash - oldhash
hashlist.append((distance, lis[i], ihash))
if distance > 67:
# sleep(0.5)
star2 = time.clock()
cv_image = np.array(image)
result = blurr_ratio(cv_image)
# result=0
end2 = time.clock()
print(i, " time taken: ", end - start, "s fps:", 25 / (end - start), " distance:", distance, "result",
result, "time", end2 - star2)
oldhash = ihash
else:
oldhash = ihash
end1 = time.clock()
print(len(lis), end1 - star1, len(lis) / (end1 - star1))
def blur_test():
lis = os.listdir("motomed_live")
for i in range(len(lis)):
lis[i] = "motomed_live/" + lis[i]
star1 = time.clock()
for i in range(len(lis)):
start = time.clock()
result = blurr_ratio(cv2.imread(lis[i]))
end = time.clock()
print(end - start, "s", "result:", result)
end1 = time.clock()
print(len(lis), end1 - star1, len(lis) / (end1 - star1))
def tts_init():
from time import sleep
engin = tts.init()
engin.setProperty('voice', "german")
sleep(1)
return engin
"""
def initialize_cam():
from picamera import PiCamera
import time
camera = PiCamera()
camera.color_effects = (128, 128)
time.sleep(2)
return camera
"""
def take_picture(camera):
my_stream = BytesIO()
camera.capture(my_stream, 'png')
return Image.open(my_stream)
def testing_pictures(camera):
import time
camera.start_preview()
time.sleep(20)
camera.stop_preview()
def image_to_text(image):
return pytesseract.image_to_string(image)
def text_to_sound(text):
engine.say(text)
engine.runAndWait()
def preview(camera, engine):
image = take_picture(camera)
image = image_opt(image, 1, 1, 1, 1)
output = image_to_text(image)
print(output)
text_to_sound(output)
def save_image(image, name):
image.save(name)
def controllout(camera):
import time
i = 0
while True:
start = time.clock()
image = take_picture(camera)
save_image(image, "testpicture/" + str(i) + ".png")
stop = time.clock()
if 0.06 - stop - start > 0:
sleep(0.06 - stop - start)
if __name__ == "__main__":
cv2.UMat
# camera = initialize_cam()
engine = tts_init()
hash_test()
# blur_test()