...
 
Commits (2)
......@@ -55,7 +55,7 @@ class Ansprakon:
# storage for processing steps
self._min_buffer_length = args.buffer[0]
self.min_result_count = args.buffer[1]
self._min_result_count = args.buffer[1]
self._grabbed_image = None
self._preprocessed_image = None
self._rois_cut = None
......@@ -159,7 +159,7 @@ Or if it is final result device, speake the result if it was read at least 5 tim
# for final result devices
if self._final_result and not self._speak_on_button and self._results_processed is not None:
if len(self._result_buffer) >= self._min_buffer_length:
if self._result_buffer[0:-1].count(self._results_processed) >= self.min_result_count \
if self._result_buffer[0:-1].count(self._results_processed) >= self._min_result_count \
and self._results_processed != self._last_spoken:
call_nanotts.call_nanotts(self._nanotts_options, self._results_processed)
self._last_spoken = self._results_processed
......@@ -206,17 +206,17 @@ Setup argument parser and then run the processing loop.
ansprakon = Ansprakon(args)
while True:
try:
ansprakon.get_frame()
ansprakon.preprocess_image()
ansprakon.cut_rois()
ansprakon.run_ssocr()
ansprakon.detect_feat()
ansprakon.process_result()
ansprakon.speak_result()
ansprakon.sdnotify.notify("WATCHDOG=1")
except:
print("Unexpected error:", sys.exc_info()[0])
# try:
ansprakon.get_frame()
ansprakon.preprocess_image()
ansprakon.cut_rois()
ansprakon.run_ssocr()
ansprakon.detect_feat()
ansprakon.process_result()
ansprakon.speak_result()
ansprakon.sdnotify.notify("WATCHDOG=1")
# except:
# print("Unexpected error:", sys.exc_info()[0])
if __name__ == "__main__":
......
......@@ -27,7 +27,7 @@ def print_mouse_coords(event, x, y, flags, param):
print("[" + str(x) + ", " + str(y) + "]")
if event == cv2.EVENT_RBUTTONDBLCLK:
r = cv2.selectROI(bordered)
r = cv2.selectROI(warped)
print("[" + str(r[1]) + ":" + str(r[1] + r[3]) + ", " + str(r[0]) + ":" + str(r[0] + r[2]) + "]")
......@@ -40,67 +40,101 @@ while True:
ret, frame = cap.read()
if ret:
# frame = cv2.imread("/home/r0x/Development/ansprakon-v2/test_images/medisana_bloodsugar_test.png")
flipped = cv2.rotate(frame.copy(), cv2.ROTATE_90_CLOCKWISE)
gray = cv2.cvtColor(flipped.copy(), cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(gray[213:564, 59:389], 127, 255, cv2.ADAPTIVE_THRESH_MEAN_C)
blur = cv2.GaussianBlur(thresh1, (3, 3), 0)
flipped = cv2.rotate(frame, cv2.ROTATE_180)
gray = cv2.cvtColor(flipped[182:385, 0:639].copy(), cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(gray, 127, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C)
height, width = thresh1.shape
pts1 = np.float32([[18, 20], [303, 15], [25, 326], [307, 320]])
pts1 = np.float32([[65, 13], [630, 15], [35, 169], [603, 186]])
pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
m = cv2.getPerspectiveTransform(pts1, pts2)
warped = cv2.warpPerspective(thresh1, m, (width, height))
border_size = 10
bordered = cv2.copyMakeBorder(warped,
top=border_size,
bottom=border_size,
left=border_size,
right=border_size,
borderType=cv2.BORDER_CONSTANT,
value=[255, 255, 255])
systolic_digits = bordered[22:162, 40:327].copy()
diastolic_digits = bordered[162:304, 100:314].copy()
heartrate = bordered[307:354, 225:311].copy()
kernel_1 = np.ones((5, 5), np.uint8)
systolic_digits_dil = cv2.dilate(systolic_digits, kernel_1, iterations=1)
diastolic_digits_dil = cv2.dilate(diastolic_digits, kernel_1, iterations=1)
kernel_2 = np.ones((3, 3), np.uint8)
heartrate_dil = cv2.dilate(heartrate, kernel_2, iterations=1)
systolic_digits_dil_bordered = cv2.copyMakeBorder(systolic_digits_dil,
top=border_size,
bottom=border_size,
left=border_size,
right=border_size,
borderType=cv2.BORDER_CONSTANT,
value=[255, 255, 255])
diastolic_digits_dil_bordered = cv2.copyMakeBorder(diastolic_digits_dil,
top=border_size,
bottom=border_size,
left=border_size,
right=border_size,
borderType=cv2.BORDER_CONSTANT,
value=[255, 255, 255])
heartrate_dil_bordered = cv2.copyMakeBorder(heartrate_dil,
top=border_size,
bottom=border_size,
left=border_size,
right=border_size,
borderType=cv2.BORDER_CONSTANT,
value=[255, 255, 255])
cv2.imshow("1", bordered)
cv2.imshow("2", systolic_digits_dil_bordered)
cv2.imshow("3", diastolic_digits_dil_bordered)
cv2.imshow("4", heartrate_dil_bordered)
double_dot_upper = warped[45:72, 315:344].copy()
double_dot_lower = warped[133:164, 317:348].copy()
digits_1_2 = warped[0:200, 0:306].copy()
digits_3_4 = warped[0:202, 354:639].copy()
#
# kernel_1 = np.ones((3, 3), np.uint8)
# warped_dilated = cv2.dilate(warped, kernel_1, iterations=3)
#
# digits_1_2 = warped_dilated[0:154, 0:238].copy()
# digits_3_4 = warped_dilated[1:160, 311:595].copy()
#
# left_points = thresh1[14:160, 13:74].copy()
# right_points = thresh1[20:160, 530:595].copy()
#
# left_points_dilated = cv2.dilate(left_points, kernel_1, iterations=2)
#
# left_point_1 = None
# left_point_2 = left_points_dilated[62:78, 8:33].copy()
# left_point_3 = left_points_dilated[97:124, 8:33].copy()
#
# right_points_dilted = cv2.dilate(right_points, kernel_1, iterations=2)
#
# right_point_1 = right_points_dilted[20:38, 13:36].copy()
# right_point_2 = right_points_dilted[56:74, 21:41].copy()
# right_point_3 = right_points_dilted[95:112, 25:49].copy()
#
# double_dot_lower = thresh1[99:126, 275:307].copy()
# double_dot_upper = thresh1[40:65, 284:309].copy()
#
# border_size = 10
# bordered = cv2.copyMakeBorder(warped_dilated,
# top=border_size,
# bottom=border_size,
# left=border_size,
# right=border_size,
# borderType=cv2.BORDER_CONSTANT,
# value=[255, 255, 255])
#
# systolic_digits = bordered[22:162, 40:327].copy()
# diastolic_digits = bordered[162:304, 100:314].copy()
# heartrate = bordered[307:354, 225:311].copy()
#
# kernel_1 = np.ones((5, 5), np.uint8)
# systolic_digits_dil = cv2.dilate(systolic_digits, kernel_1, iterations=1)
# diastolic_digits_dil = cv2.dilate(diastolic_digits, kernel_1, iterations=1)
#
# kernel_2 = np.ones((3, 3), np.uint8)
# heartrate_dil = cv2.dilate(heartrate, kernel_2, iterations=1)
#
# systolic_digits_dil_bordered = cv2.copyMakeBorder(systolic_digits_dil,
# top=border_size,
# bottom=border_size,
# left=border_size,
# right=border_size,
# borderType=cv2.BORDER_CONSTANT,
# value=[255, 255, 255])
#
# diastolic_digits_dil_bordered = cv2.copyMakeBorder(diastolic_digits_dil,
# top=border_size,
# bottom=border_size,
# left=border_size,
# right=border_size,
# borderType=cv2.BORDER_CONSTANT,
# value=[255, 255, 255])
#
# heartrate_dil_bordered = cv2.copyMakeBorder(heartrate_dil,
# top=border_size,
# bottom=border_size,
# left=border_size,
# right=border_size,
# borderType=cv2.BORDER_CONSTANT,
# value=[255, 255, 255])
# feat_detect_rois = [double_dot_lower, double_dot_upper,
# left_point_2, left_point_3,
# right_point_1, right_point_2, right_point_3]
#cv2.imshow("1", double_dot_lower)
#cv2.imshow("2", double_dot_upper)
# cv2.imshow("1", warped_dilated)
cv2.imshow("1", warped)
#cv2.imshow("5", double_dot_upper)
cv2.waitKey(1)
......
......@@ -109,12 +109,14 @@ IDR alarm/radio
:param feat_rois:
:return:
"""
for i in range(len(feat_rois[1][0])):
feat_rois[1][0][i] = True if cv2.mean(feat_rois[1][0][i])[0] <= 240 else False
for i in range(len(feat_rois[1])):
feat_rois[1][i] = True if cv2.mean(feat_rois[1][i])[0] <= 240 else False
print(feat_rois[1])
return feat_rois
def feat_detect_device_9(feat_rois):
"""
Schneider Microwave
......
......@@ -330,39 +330,21 @@ IDF radio-alarm
:param img: the image to process
:return: the processed img
"""
gray = cv2.cvtColor(img[249:390, 24:616].copy(), cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(gray.copy(), 90, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C)
im_floodfill = thresh1.copy()
gray = cv2.cvtColor(img[241:401, 27:622].copy(), cv2.COLOR_BGR2GRAY)
# Mask used to flood filling.
# Notice the size needs to be 2 pixels than the image.
h, w = thresh1.shape[:2]
mask = np.zeros((h + 2, w + 2), np.uint8)
# Floodfill from point (0, 0)
cv2.floodFill(im_floodfill, mask, (0, 0), 255)
# find all your connected components (white blobs in your image)
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(im_floodfill, connectivity=8)
# connectedComponentswithStats yields every seperated component with information on each of them, such as size
# the following part is just taking out the background which is also considered a component, but most of the time
# we don't want that.
sizes = stats[0:, -1];
nb_components = nb_components
ret, thresh1 = cv2.threshold(gray, 115, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C)
height, width = thresh1.shape
pts1 = np.float32([[119, 20], [524, 20], [84, 150], [520, 150]])
pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
m = cv2.getPerspectiveTransform(pts1, pts2)
warped = cv2.warpPerspective(thresh1, m, (width, height))
# minimum size of particles we want to keep (number of pixels)
# here, it's a fixed value, but you can set it as you want, eg the mean of the sizes or whatever
min_size = 300
kernel_1 = np.ones((3, 3), np.uint8)
dilated = cv2.dilate(warped, kernel_1, iterations=1)
# your answer image
processed_image = np.zeros(output.shape)
# for every component in the image, you keep it only if it's above min_size
for i in range(0, nb_components):
if sizes[i] >= min_size:
processed_image[output == i + 1] = 255
double_dot_upper_gray = gray[20:44, 302:332].copy()
double_dot_lower_gray = gray[78:104, 293:321].copy()
processed = [ thresh1, dilated]
return [processed_image, double_dot_upper_gray, double_dot_lower_gray]
return processed
# Device ID 0
......@@ -429,26 +411,16 @@ SEVERIN Microwave
:return: the processed img
"""
flipped = cv2.rotate(img, cv2.ROTATE_180)
gray = cv2.cvtColor(flipped.copy(), cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(gray[199:393, 28:632].copy(), 175, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C)
gray = cv2.cvtColor(flipped[182:385, 0:639].copy(), cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(gray, 127, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C)
height, width = thresh1.shape
pts1 = np.float32([[39, 10], [596, 13], [8, 165], [570, 171]])
pts1 = np.float32([[65, 13], [630, 15], [35, 169], [603, 186]])
pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
m = cv2.getPerspectiveTransform(pts1, pts2)
warped = cv2.warpPerspective(thresh1, m, (width, height))
border_size = 10
bordered = cv2.copyMakeBorder(warped,
top=border_size,
bottom=border_size,
left=border_size,
right=border_size,
borderType=cv2.BORDER_CONSTANT,
value=[255, 255, 255])
return bordered
return warped
# Device ID 12
def image_device_12(img):
......
......@@ -204,7 +204,10 @@ IDR radio alarm
:param rois_processed:
:return:
"""
print(rois_processed)
for result in rois_processed[0]:
print(result)
results_processed = None
return results_processed
......@@ -246,20 +249,6 @@ def process_results_device_10(rois_processed):
return results_processed
def process_results_device_9(rois_processed):
# TODO: Process results
"""
SCHNEIDER Microwave
:param rois_processed:
:return:
"""
results_processed = None
for result in rois_processed[0]:
print(result)
return results_processed
def process_results_device_11(rois_processed):
# TODO: Process results
......
......@@ -271,45 +271,47 @@ CASIO calculator MS-20UC
return [ocr_rois, feat_detect_rois]
def roi_device_8(imgs):
def roi_device_8(img):
"""
IDR radio alarmclock
:param imgs:
:return:
"""
img = imgs[0]
digit_1_2 = img[2:127, 70:295].copy()
digit_3_4 = img[3:135, 320:552].copy()
kernel_1 = np.ones((3, 3), np.uint8)
cv2.imshow("dig 1_2", digit_1_2)
cv2.imshow("dig_3_4", digit_3_4)
cv2.waitKey(1)
ocr_rois = [digit_1_2, digit_3_4]
dot_digit_3_4 = digit_3_4[100:127, 103:132].copy()
digits_1_2 = img[1][0:154, 0:238].copy()
digits_3_4 = img[1][1:160, 311:595].copy()
double_dot_upper = img[18:44, 301:332].copy()
double_dot_lower = img[77:104, 295:321].copy()
double_dot_lower = img[1][99:126, 275:307].copy()
double_dot_upper = img[1][36:68, 284:317].copy()
sleep_dot = img[89:116, 565:592].copy()
decimal_dot = img[1][127:158, 437:479].copy()
alarm_1_buzz = img[50:73, 36:65].copy()
alarm_1_music = img[51:79, 565:590].copy()
alarm_2_buzz = img[12:44, 557:589].copy()
alarm_2_music = img[86:112, 33:62].copy()
left_points = img[0][14:160, 13:74].copy()
left_points_dilated = cv2.dilate(left_points, kernel_1, iterations=2)
# left_point_1 = None
left_point_2 = left_points_dilated[62:78, 8:33].copy()
left_point_3 = left_points_dilated[97:124, 8:33].copy()
feat_bin = [alarm_1_buzz, alarm_1_music,
alarm_2_buzz, alarm_2_music,
dot_digit_3_4,
double_dot_upper, double_dot_lower,
sleep_dot]
right_points = img[0][20:160, 530:595].copy()
right_points_dilted = cv2.dilate(right_points, kernel_1, iterations=2)
right_point_1 = right_points_dilted[20:38, 13:36].copy()
right_point_2 = right_points_dilted[56:74, 21:41].copy()
right_point_3 = right_points_dilted[95:112, 25:49].copy()
double_dot_gray = [imgs[1], imgs[2]]
# imgs[1] ist the double dot in gray
feat_detect_rois = [feat_bin, double_dot_gray]
ocr_rois = [digits_1_2, digits_3_4]
feat_detect_rois = [double_dot_lower, double_dot_upper,
decimal_dot,
left_point_2, left_point_3,
right_point_1, right_point_2, right_point_3]
cv2.imshow("1", digits_1_2)
cv2.imshow("2", digits_3_4)
# cv2.imshow("3", right_point_3)
# cv2.imshow("4", double_dot_upper)
# cv2.imshow("5", double_dot_lower)
cv2.waitKey(1)
return [ocr_rois, feat_detect_rois]
......@@ -359,15 +361,17 @@ def roi_device_11(img):
:param img:
:return:
"""
digits_1_2 = img[1:214, 1:279].copy()
digits_3_4 = img[1:214, 326:624].copy()
double_dot_upper = img[47:84, 289:323].copy()
double_dot_lower = img[140:167, 294:320].copy()
double_dot_upper = img[45:72, 315:344].copy()
double_dot_lower = img[133:164, 317:348].copy()
digits_1_2 = img[0:200, 0:306].copy()
digits_3_4 = img[0:202, 354:639].copy()
ocr_rois = [digits_1_2, digits_3_4]
feat_detect_rois = [double_dot_upper, double_dot_lower]
# cv2.imshow("all", img)
# cv2.imshow("digits_1-2", digits_1_2)
# cv2.imshow("digits_3-4", digits_3_4)
# cv2.waitKey(1)
......
......@@ -163,6 +163,7 @@ IDR alarm-clock :param rois:
"-d", "-1",
"-i", "3",
"-n", "10",
"-c", "digits"
"-C"
]
......@@ -215,9 +216,9 @@ SEVERIN Microwave
device_ssocr_args = [
"-D",
"-d", "-1",
# "-i", "4",
# "-n", "10",
"-m 4",
"-i", "4",
"-n", "10",
# "-m 4",
"-C"
]
......