-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.py
More file actions
184 lines (154 loc) · 6.28 KB
/
main.py
File metadata and controls
184 lines (154 loc) · 6.28 KB
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
175
176
177
178
179
180
181
182
183
184
# This demo is modified based on this project: https://github.com/thearn/webcam-pulse-detector
from heartrate_estimator import HeartrateEstimator
import cv2
import numpy as np
import time
import sys
import argparse
import depthai as dai
from pathlib import Path
from utils import frame_norm, to_planar
class getVitalApp(object):
def __init__(self, pixel_threshold):
self.pressed = 0
self.processor = HeartrateEstimator(pixel_threshold=pixel_threshold)
self.key_controls = {"s": self.toggle_search}
# Create an OAK-D pipeline
self.pipeline = dai.Pipeline()
# Setup color camera node
colorCam = self.pipeline.createColorCamera()
xoutRgb = self.pipeline.createXLinkOut()
xoutRgb.setStreamName("rgb")
colorCam.setPreviewSize(800, 450)
colorCam.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
colorCam.setPreviewKeepAspectRatio(False)
colorCam.setInterleaved(False)
colorCam.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR)
colorCam.initialControl.setManualFocus(130)
# Using ImageManip node to create input to NN input
manip = self.pipeline.createImageManip()
manip.initialConfig.setResize(300, 300)
manip.setKeepAspectRatio(False)
colorCam.preview.link(manip.inputImage)
# Create face detection NN node
detection_nn = self.pipeline.createNeuralNetwork()
detection_nn.setBlobPath(
str(
(
Path(__file__).parent
/ Path(
"./models/face-detection-retail-0004_openvino_2021.2_6shave.blob"
)
)
.resolve()
.absolute()
)
)
# Feed camera input to NN
manip.out.link(detection_nn.input)
colorCam.preview.link(xoutRgb.input)
xout_det = self.pipeline.createXLinkOut()
xout_det.setStreamName("det_nn")
detection_nn.out.link(xout_det.input)
# Using landmark to locate a skin region is possible too, need more tests.
# landmarks_nn = self.pipeline.createNeuralNetwork()
# landmarks_nn.setBlobPath(
# str(
# (
# Path(__file__).parent
# / Path(
# "landmarks-regression-retail-0009_openvino_2021.2_6shave.blob"
# )
# )
# .resolve()
# .absolute()
# )
# )
# xin_land = self.pipeline.createXLinkIn()
# xin_land.setStreamName("land_in")
# xin_land.out.link(landmarks_nn.input)
# xout_land = self.pipeline.createXLinkOut()
# xout_land.setStreamName("land_nn")
# landmarks_nn.out.link(xout_land.input)
self.device = dai.Device(self.pipeline)
# Setup device output queues
self.previewQueue = self.device.getOutputQueue(
name="rgb", maxSize=4, blocking=False
)
self.q_det = self.device.getOutputQueue(
name="det_nn", maxSize=4, blocking=False
)
# self.land_in = self.device.getInputQueue(
# name="land_in", maxSize=4, blocking=False
# )
# self.q_land = self.device.getOutputQueue(
# name="land_nn", maxSize=4, blocking=False
# )
def toggle_search(self):
"""
Toggles a motion lock on the processor's face detection component.
Locking the forehead location in place significantly improves
data quality, once a forehead has been sucessfully isolated.
"""
# state = self.processor.find_faces.toggle()
state = self.processor.find_faces_toggle()
def key_handler(self):
"""
Handle keystrokes, as set at the bottom of __init__()
A plotting or camera frame window must have focus for keypresses to be
detected.
"""
self.pressed = cv2.waitKey(1) & 255 # wait for keypress for 10 ms
if self.pressed == 27: # exit program on 'esc'
print("Exiting")
sys.exit()
for key in self.key_controls.keys():
if chr(self.pressed) == key:
self.key_controls[key]()
def run(self):
while True:
# t1 = time.monotonic()
frame = self.previewQueue.get().getCvFrame()
bboxes = np.array(self.q_det.get().getFirstLayerFp16())
bboxes = bboxes.reshape((bboxes.size // 7, 7))
bboxes = bboxes[bboxes[:, 2] > 0.5][:, 3:7]
largest_bbox = []
largest_area = 0
landmarks = []
for raw_bbox in bboxes:
bbox = frame_norm(frame, raw_bbox)
area = (bbox[2] - bbox[0]) * (bbox[3] - bbox[1])
if area > largest_area:
largest_area = area
largest_bbox = bbox
# Comment out until more tests on using landmarks for skin reigon are performed
# if largest_bbox != []:
# face_frame = frame[
# largest_bbox[1] : largest_bbox[3], largest_bbox[0] : largest_bbox[2]
# ]
# nn_data = dai.NNData()
# nn_data.setLayer("0", to_planar(face_frame, (48, 48)))
# self.land_in.send(nn_data)
# landmarks = self.q_land.get().getFirstLayerFp16()
# set current image frame to the processor's input
# process the image frame to perform all needed analysis
# t1 = time.monotonic()
bpm = self.processor.measure_pulse(frame, largest_bbox)
self.processor.draw_vitals(frame, bpm)
# print("Pulse Time:", time.monotonic() - t1)
# show the processed/annotated output frame
cv2.imshow("Heart Pulse Estimation", frame)
# handle any key presses
self.key_handler()
# print("Time:", time.monotonic() - t1)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--pixel_thresh",
default=1.5,
type=float,
help="Threshold value for the buffer switching mechanism.",
)
args = parser.parse_args()
App = getVitalApp(pixel_threshold=args.pixel_thresh)
App.run()