()
| 269 | return cv2.waitKey(5) & 0xFF == 27 |
| 270 | |
| 271 | def main(): |
| 272 | global last_frames, frame_midpoint |
| 273 | |
| 274 | parser = argparse.ArgumentParser() |
| 275 | parser.add_argument('--input', '-i', help='Input video device or file (number or path), defaults to 0', default='0') |
| 276 | parser.add_argument('--flip', '-f', help='Set to any value to flip resulting output (selfie view)') |
| 277 | parser.add_argument('--landmarks', '-l', help='Set to any value to draw body landmarks') |
| 278 | parser.add_argument('--record', '-r', help='Set to any value to save a timestamped AVI in current directory') |
| 279 | parser.add_argument('--type', '-t', help='Set to any value to type output rather than only display') |
| 280 | parser.add_argument('--repeat', '-p', help='Set to any value to allow instant semaphore repetitions') |
| 281 | args = parser.parse_args() |
| 282 | |
| 283 | INPUT = int(args.input) if (args.input and args.input.isdigit()) else args.input |
| 284 | FLIP = args.flip is not None |
| 285 | DRAW_LANDMARKS = args.landmarks is not None |
| 286 | RECORDING = args.record is not None |
| 287 | DISPLAY_ONLY = args.type is None |
| 288 | ALLOW_REPEAT = args.repeat is not None |
| 289 | |
| 290 | cap = cv2.VideoCapture(INPUT) |
| 291 | |
| 292 | frame_size = (int(cap.get(3)), int(cap.get(4))) |
| 293 | frame_midpoint = (int(frame_size[0]/2), int(frame_size[1]/2)) |
| 294 | |
| 295 | recording = cv2.VideoWriter(RECORDING_FILENAME, |
| 296 | cv2.VideoWriter_fourcc(*'MJPG'), FPS, frame_size) if RECORDING else None |
| 297 | |
| 298 | with mp.solutions.pose.Pose() as pose_model: |
| 299 | with mp.solutions.hands.Hands(max_num_hands=2) as hands_model: |
| 300 | while cap.isOpened(): |
| 301 | success, image = cap.read() |
| 302 | if not success: break |
| 303 | |
| 304 | image.flags.writeable = False |
| 305 | image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
| 306 | pose_results = pose_model.process(image) |
| 307 | hand_results = hands_model.process(image) |
| 308 | |
| 309 | image.flags.writeable = True |
| 310 | image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) |
| 311 | |
| 312 | # draw pose |
| 313 | if DRAW_LANDMARKS: |
| 314 | mp.solutions.drawing_utils.draw_landmarks( |
| 315 | image, |
| 316 | pose_results.pose_landmarks, |
| 317 | mp.solutions.pose.POSE_CONNECTIONS, |
| 318 | DEFAULT_LANDMARKS_STYLE) |
| 319 | |
| 320 | hands = [] |
| 321 | hand_index = 0 |
| 322 | if hand_results.multi_hand_landmarks: |
| 323 | for hand_landmarks in hand_results.multi_hand_landmarks: |
| 324 | # draw hands |
| 325 | if DRAW_LANDMARKS: |
| 326 | mp.solutions.drawing_utils.draw_landmarks( |
| 327 | image, |
| 328 | hand_landmarks, |
no test coverage detected