-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
77 lines (63 loc) · 3.65 KB
/
run.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
import cv2
import numpy as np
from background_subtraction.background_subtraction import BackgroundSubtractor
from constants import get_constants
from conversion_to_binary.conversion_to_binary import convert_to_binary
from distance_transform.distance_transform import calculate_distance_transform
from frame_obtaining.frame_obtaining import FrameObtainer
from labeling.labeling import create_components
from palm_segmentor.palm_segmentor import PalmSegmentor
if __name__ == '__main__':
params = get_constants()
frame_obtainer = FrameObtainer(params['cap_region_x_begin'],
params['cap_region_y_end'],
params['threshold'],
params['blur_value'],
params['background_sub_threshold'],
params['eta'])
frame_obtainer.create_trackbar()
background_subtractor = BackgroundSubtractor(params['background_sub_threshold'],
params['eta'])
palm_point_segmentor = PalmSegmentor()
while frame_obtainer.get_camera().isOpened():
original_image = frame_obtainer.read_frame()
if background_subtractor.is_background_captured():
background_subtractor.set_frame(original_image)
background = background_subtractor.extract_background(params['cap_region_y_end'],
params['cap_region_x_begin'],
params['erode_iterations'])
binary_image = convert_to_binary(background, params['blur_value'], params['threshold'])
binary_image = cv2.resize(binary_image, (200, 200), interpolation=cv2.INTER_AREA)
dt = calculate_distance_transform(binary_image)
max_i, max_j = palm_point_segmentor.obtaining_palm_point(dt)
background = cv2.resize(background, (200, 200), interpolation=cv2.INTER_AREA)
image_with_palm_point = palm_point_segmentor.from_one_channel_to_three(background, binary_image)
palm_point_segmentor.draw_image_with_palm_point(image_with_palm_point)
palm_point_segmentor.draw_image_with_inner_circle(binary_image)
# palm_mask_producer = PalmMaskProducer(binary_image, image_with_palm_point)
# palm_mask_producer.compute_samples(max_i, max_j, palm_point_segmentor.get_maximum_radius_12(), params['sampling_step'])
# mask_points = palm_mask_producer.get_palm_mask()
#
# palm_mask = np.zeros((200, 200), np.uint8)
# for point in mask_points:
# palm_mask[point[0]][point[1]] = 255
# cv2.imshow('Palm Mask', palm_mask)
labeled_image = create_components(binary_image)
for i in range(0, labeled_image.shape[0]):
for j in range(0, labeled_image.shape[1]):
if labeled_image[i][j].any() != 0:
image_with_palm_point[i][j] = labeled_image[i][j]
image_with_palm_point = cv2.resize(image_with_palm_point, (400, 400), interpolation=cv2.INTER_AREA)
cv2.imshow('Final', image_with_palm_point)
k = cv2.waitKey(10)
if k == 27:
frame_obtainer.get_camera().release()
cv2.destroyAllWindows()
break
# press B to capture background
elif k == ord('b'):
background_subtractor.set_background_captured(True)
print('! Background Captured!')
elif k == ord('r'):
background_subtractor.set_background_captured(False)
print('! Background Reset!')