|
| 1 | +# Hand recognition to change brightness using index finger and thumb and set them using other fingers! |
| 2 | + |
| 3 | +# (0) in VideoCapture is used to connect to your computer's default camera |
| 4 | +import cv2 |
| 5 | +import mediapipe as mp |
| 6 | +import time |
| 7 | +capture = cv2.VideoCapture(0) |
| 8 | + |
| 9 | +# Initializing current time and precious time for calculating the FPS |
| 10 | +previousTime = 0 |
| 11 | +currentTime = 0 |
| 12 | + |
| 13 | +mp_holistic = mp.solutions.holistic |
| 14 | +holistic_model = mp_holistic.Holistic( |
| 15 | + min_detection_confidence=0.5, |
| 16 | + min_tracking_confidence=0.5 |
| 17 | +) |
| 18 | + |
| 19 | +# Initializing the drawing utils for drawing the facial landmarks on image |
| 20 | +mp_drawing = mp.solutions.drawing_utils |
| 21 | + |
| 22 | +while capture.isOpened(): |
| 23 | + # capture frame by frame |
| 24 | + ret, frame = capture.read() |
| 25 | + |
| 26 | + # resizing the frame for better view |
| 27 | + frame = cv2.resize(frame, (800, 600)) |
| 28 | + |
| 29 | + # Converting the from BGR to RGB |
| 30 | + image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
| 31 | + |
| 32 | + # Making predictions using holistic model |
| 33 | + # To improve performance, optionally mark the image as not writeable to |
| 34 | + # pass by reference. |
| 35 | + image.flags.writeable = False |
| 36 | + results = holistic_model.process(image) |
| 37 | + image.flags.writeable = True |
| 38 | + |
| 39 | + # Converting back the RGB image to BGR |
| 40 | + image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) |
| 41 | + |
| 42 | + # Drawing the Facial Landmarks |
| 43 | + mp_drawing.draw_landmarks( |
| 44 | + image, |
| 45 | + results.face_landmarks, |
| 46 | + mp_holistic.FACEMESH_CONTOURS, |
| 47 | + mp_drawing.DrawingSpec( |
| 48 | + color=(255,0,255), |
| 49 | + thickness=1, |
| 50 | + circle_radius=1 |
| 51 | + ), |
| 52 | + mp_drawing.DrawingSpec( |
| 53 | + color=(0,255,255), |
| 54 | + thickness=1, |
| 55 | + circle_radius=1 |
| 56 | + ) |
| 57 | + ) |
| 58 | + |
| 59 | + # Drawing Right hand Land Marks |
| 60 | + mp_drawing.draw_landmarks( |
| 61 | + image, |
| 62 | + results.right_hand_landmarks, |
| 63 | + mp_holistic.HAND_CONNECTIONS |
| 64 | + ) |
| 65 | + |
| 66 | + # Drawing Left hand Land Marks |
| 67 | + mp_drawing.draw_landmarks( |
| 68 | + image, |
| 69 | + results.left_hand_landmarks, |
| 70 | + mp_holistic.HAND_CONNECTIONS |
| 71 | + ) |
| 72 | + |
| 73 | + # Calculating the FPS |
| 74 | + currentTime = time.time() |
| 75 | + fps = 1 / (currentTime-previousTime) |
| 76 | + previousTime = currentTime |
| 77 | + |
| 78 | + # Displaying FPS on the image |
| 79 | + cv2.putText(image, str(int(fps))+" FPS", (10, 70), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2) |
| 80 | + |
| 81 | + # Display the resulting image |
| 82 | + cv2.imshow("Facial and Hand Landmarks", image) |
| 83 | + |
| 84 | + # Enter key 'q' to break the loop |
| 85 | + if cv2.waitKey(5) & 0xFF == ord('q'): |
| 86 | + break |
| 87 | + |
| 88 | +# When all the process is done |
| 89 | +# Release the capture and destroy all windows |
| 90 | +capture.release() |
| 91 | +cv2.destroyAllWindows() |
| 92 | + |
| 93 | + |
0 commit comments