Advertisement
Pug_coder

Python_Test

May 26th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. import cv2
  2. import mediapipe as mp
  3. from json import dumps
  4. mp_drawing = mp.solutions.drawing_utils
  5. mp_hands = mp.solutions.hands
  6.  
  7. def dump_as_json(data_point):
  8.   return dumps({'x': data_point.x, 'y': data_point.y, 'z': data_point.z})
  9.  
  10. cap = cv2.VideoCapture(0)
  11. with mp_hands.Hands(
  12.     max_num_hands=2,
  13.     min_detection_confidence=0.8,
  14.     min_tracking_confidence=0.8) as hands:
  15.   while cap.isOpened():
  16.     success, image = cap.read()
  17.     if not success:
  18.       print("Ignoring empty camera frame.")
  19.       # If loading a video, use 'break' instead of 'continue'.
  20.       continue
  21.  
  22.     # Flip the image horizontally for a later selfie-view display, and convert
  23.     # the BGR image to RGB.
  24.     image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
  25.     # To improve performance, optionally mark the image as not writeable to
  26.     # pass by reference.
  27.     image.flags.writeable = False
  28.     results = hands.process(image)
  29.  
  30.     # Draw the hand annotations on the image.
  31.     image.flags.writeable = True
  32.     image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  33.     if results.multi_hand_landmarks:
  34.       for i, hand_landmarks in enumerate(results.multi_hand_landmarks):
  35.         for data_point in hand_landmarks.landmark:
  36.           print(dump_as_json(data_point))
  37.         mp_drawing.draw_landmarks(
  38.             image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
  39.     cv2.imshow('MediaPipe Hands', image)
  40.     if cv2.waitKey(5) & 0xFF == 27:
  41.       break
  42. cap.release()
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement