Automative Media Player (Explanation of Code)
Automative Media Player (Explanation of Code)
import mediapipe as mp
import pyautogui
import time
return cnt
1. **Function Definition**:
```python
def count_fingers(lst):
```
- This line defines a function named `count_fingers` that takes one
argument `lst`. It seems `lst` is expected to be an object containing
landmarks detected on a hand using some library like MediaPipe.
2. **Initialization**:
```python
cnt = 0
```
- Initializes a variable `cnt` to count the number of fingers detected.
It's set to 0 initially.
3. **Threshold Calculation**:
```python
thresh = (lst.landmark[0].y*100 - lst.landmark[9].y*100)/2
```
- Calculates a threshold value based on the difference in y-
coordinates of specific landmarks on the hand. This threshold might be
used to determine whether a finger is open or closed.
4. **Finger Counting**:
- The following `if` statements check certain conditions to determine
whether each finger is open or closed:
```python
if (lst.landmark[5].y*100 - lst.landmark[8].y*100) > thresh:
cnt += 1
```
- Checks if the distance between landmarks corresponding to the tip
of the thumb and the base of the index finger is greater than the
threshold.
- Similar logic is applied to other fingers, from index finger to pinky.
6. **Return Count**:
```python
return cnt
```
- Returns the count of fingers detected based on the conditions
checked in the function.
So, overall, this function calculates the number of fingers based on the
landmarks detected in a hand pose, using some predefined conditions
and thresholds.
cap = cv2.VideoCapture(0)
drawing = mp.solutions.drawing_utils
hands = mp.solutions.hands
hand_obj = hands.Hands(max_num_hands=1)
2. **Mediapipe Setup**:
```python
drawing = mp.solutions.drawing_utils
hands = mp.solutions.hands
hand_obj = hands.Hands(max_num_hands=1)
```
- These lines import the necessary modules from the MediaPipe
library (`mediapipe`) and set up a hand tracking object.
- `mp.solutions.drawing_utils` provides utility functions to draw
landmarks and connections on the image.
- `mp.solutions.hands` provides a pre-trained model for hand
tracking.
- `hand_obj = hands.Hands(max_num_hands=1)` initializes the hand
tracking object, specifying that it should detect a maximum of 1 hand in
the frame.
Overall, this code segment sets up a video capture object using
OpenCV and initializes a hand tracking object using the MediaPipe
library. The hand tracking object will be used to detect and track hands
in the video frames captured by the webcam.
start_init = False
prev = -1
while True:
end_time = time.time()
_, frm = cap.read()
frm = cv2.flip(frm, 1)
if res.multi_hand_landmarks:
hand_keyPoints = res.multi_hand_landmarks[0]
cnt = count_fingers(hand_keyPoints)
if not(prev==cnt):
if not(start_init):
start_time = time.time()
start_init = True
prev = cnt
start_init = False
drawing.draw_landmarks(frm, hand_keyPoints,
hands.HAND_CONNECTIONS)
cv2.imshow("window", frm)
if cv2.waitKey(1) == 27:
cv2.destroyAllWindows()
cap.release()
Break
1. **Variable Initialization**:
- `start_init` is a boolean variable initialized to `False`. It's used to
track whether the hand gesture recognition process has started.
- `prev` is a variable initialized to `-1`. It's used to track the previous
finger count.
2. **Main Loop**:
- `while True:` initiates an infinite loop to continuously process video
frames from the webcam.