(capture)
| 20 | |
| 21 | |
| 22 | def show_video(capture): |
| 23 | # cap = cv2.VideoCapture(0) # Capture from camera. Adjust the number |
| 24 | cap = cv2.VideoCapture(capture) # Capture from video |
| 25 | |
| 26 | mp_drawing = mp.solutions.drawing_utils |
| 27 | mp_drawing_styles = mp.solutions.drawing_styles |
| 28 | mp_hands = mp.solutions.hands |
| 29 | |
| 30 | try: |
| 31 | pTime = time.time() |
| 32 | |
| 33 | with mp_hands.Hands( |
| 34 | min_detection_confidence=0.5, min_tracking_confidence=0.5 |
| 35 | ) as hands: |
| 36 | |
| 37 | while cap.isOpened(): |
| 38 | # Read from video capture device |
| 39 | success, img = cap.read() |
| 40 | |
| 41 | # FPS |
| 42 | pTime, img = calculate_fps(img, pTime) |
| 43 | |
| 44 | # Convert from BGR to RGB |
| 45 | img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| 46 | |
| 47 | # To improve performance, optionally mark the image as not writeable to pass by reference. |
| 48 | img.flags.writeable = False |
| 49 | results = hands.process(img) |
| 50 | |
| 51 | # Draw the hand annotations on the image. |
| 52 | img.flags.writeable = True |
| 53 | img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) |
| 54 | if results.multi_hand_landmarks: |
| 55 | for hand_landmarks in results.multi_hand_landmarks: |
| 56 | mp_drawing.draw_landmarks( |
| 57 | img, |
| 58 | hand_landmarks, |
| 59 | mp_hands.HAND_CONNECTIONS, |
| 60 | mp_drawing_styles.get_default_hand_landmarks_style(), |
| 61 | mp_drawing_styles.get_default_hand_connections_style(), |
| 62 | ) |
| 63 | |
| 64 | # Show video |
| 65 | if img is not None: |
| 66 | cv2.imshow("Hands", img) |
| 67 | |
| 68 | # Wait for key to exit |
| 69 | if cv2.waitKey(10) & 0xFF == ord("q"): |
| 70 | break |
| 71 | |
| 72 | finally: |
| 73 | cap.release() |
| 74 | cv2.destroyAllWindows() |
| 75 | |
| 76 | |
| 77 | if __name__ == "__main__": |
no test coverage detected