Skip to content

Commit e423f81

Browse files
authored
Merge pull request #1121 from Shantnu-singh/main
added hand detection using openCv
2 parents cb97958 + ac8feb0 commit e423f81

File tree

4 files changed

+200
-0
lines changed

4 files changed

+200
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import cv2
2+
import time
3+
import mediapipe as mp
4+
5+
# mode = False , maxHands = 2 , detectionCon = 0.5 , TrackCon = 0.5
6+
class HandDetection:
7+
def __init__(self , min_detection_confidence = 0.5):
8+
# self.mode = mode
9+
# self.maxHand = maxHands
10+
self.min_detection_confidence = min_detection_confidence
11+
# self.TrackCon = TrackCon
12+
13+
self.mpHands = mp.solutions.hands
14+
self.hands = self.mpHands.Hands( self.min_detection_confidence )
15+
self.mpDraw = mp.solutions.drawing_utils
16+
17+
18+
def findHand(self , frame , flag = True):
19+
RGB_frame = cv2.cvtColor(frame , cv2.COLOR_BGR2RGB)
20+
self.results = self.hands.process(RGB_frame)
21+
22+
# print(self.results.multi_hand_landmarks)
23+
24+
if self.results.multi_hand_landmarks:
25+
for multihands in self.results.multi_hand_landmarks:
26+
if flag:
27+
self.mpDraw.draw_landmarks(frame , multihands , self.mpHands.HAND_CONNECTIONS)
28+
29+
return frame
30+
31+
def findPosition(self , frame , handno = 0 , draw = True):
32+
33+
lmList = []
34+
35+
if self.results.multi_hand_landmarks:
36+
myHand = self.results.multi_hand_landmarks[handno]
37+
38+
for id , lm in enumerate(myHand.landmark):
39+
h , w , c = frame.shape
40+
cx , cy = int(lm.x*w) , int(lm.y*h)
41+
# print(id , cx , cy)
42+
lmList.append([id , cx , cy])
43+
44+
if draw:
45+
46+
cv2.circle(frame , (cx , cy) , 7 , (255 , 0 , 9) , cv2.FILLED)
47+
48+
return lmList
49+
50+
51+
def main():
52+
pTime = 0
53+
cTime = 0
54+
55+
56+
cap = cv2.VideoCapture(0)
57+
58+
detector = HandDetection()
59+
60+
61+
while True:
62+
63+
rec , frame = cap.read()
64+
65+
frame = detector.findHand(frame)
66+
lmlist = detector.findPosition(frame)
67+
68+
if len(lmlist) != 0:
69+
print(lmlist[4])
70+
71+
cTime =time.time()
72+
Fps = 1/(cTime - pTime)
73+
pTime = cTime
74+
75+
cv2.putText(frame , str(int(Fps)) , (10 , 40) , cv2.FONT_HERSHEY_COMPLEX , 1 , (0,255 , 0) , 2 )
76+
77+
78+
79+
cv2.imshow("webcam" , frame)
80+
if cv2.waitKey(1) & 0xFF == ord("x"):
81+
break
82+
83+
cap.release()
84+
cv2.destroyAllWindows()
85+
86+
if __name__ == "__main__":
87+
main()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import mediapipe as mp
2+
import cv2
3+
import numpy as np
4+
import time
5+
6+
mpHands = mp.solutions.hands
7+
hands = mpHands.Hands()
8+
mpDraw = mp.solutions.drawing_utils
9+
10+
pTime = 0
11+
cTime = 0
12+
13+
14+
cap = cv2.VideoCapture(0)
15+
16+
while True:
17+
18+
rec , frame = cap.read()
19+
20+
gray_frame = cv2.cvtColor(frame , cv2.COLOR_BGR2RGB)
21+
results = hands.process(gray_frame)
22+
23+
print(results.multi_hand_landmarks)
24+
25+
if results.multi_hand_landmarks:
26+
for multihands in results.multi_hand_landmarks:
27+
for id , lm in enumerate(multihands.landmark):
28+
h , w , c = frame.shape
29+
cx , cy = int(lm.x*w) , int(lm.y*h)
30+
print(id , cx , cy)
31+
32+
if id == 4:
33+
cv2.circle(frame , (cx , cy) , 15 , (255 , 255 , 9) , cv2.FILLED)
34+
35+
mpDraw.draw_landmarks(frame , multihands , mpHands.HAND_CONNECTIONS)
36+
37+
cTime =time.time()
38+
Fps = 1/(cTime - pTime)
39+
pTime = cTime
40+
41+
cv2.putText(frame , str(int(Fps)) , (10 , 40) , cv2.FONT_HERSHEY_COMPLEX , 1 , (0,255 , 0) , 2 )
42+
43+
44+
45+
cv2.imshow("webcam" , frame)
46+
if cv2.waitKey(1) & 0xFF == ord("x"):
47+
break
48+
49+
cap.release()
50+
cv2.destroyAllWindows()
51+
52+
2.05 KB
Binary file not shown.

Hand detection package/readme.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Hand Detection Module
2+
3+
This project is a Python module that utilizes MediaPipe and OpenCV for real-time hand detection and landmark recognition using a webcam. The module detects hand landmarks and highlights specific points, such as the tip of the thumb, on the video feed.
4+
5+
## Table of Contents
6+
- [Introduction](#introduction)
7+
- [Features](#features)
8+
- [Installation](#installation)
9+
- [Usage](#usage)
10+
- [How It Works](#how-it-works)
11+
- [Limitations](#limitations)
12+
## Introduction
13+
14+
This hand detection module is designed for real-time detection and tracking of hand landmarks. It leverages MediaPipe's pre-trained hand detection model and OpenCV for video capture and processing.
15+
16+
## Features
17+
18+
- Real-time hand detection using a webcam.
19+
- Identification and marking of specific hand landmarks (e.g., thumb tip).
20+
- Display of the video feed with overlayed hand landmarks and frames per second (FPS).
21+
- Simple and efficient, with the ability to handle multiple hands.
22+
23+
## Installation
24+
25+
To use this module, ensure you have Python installed along with the required libraries:
26+
27+
```bash
28+
pip install opencv-python
29+
pip install mediapipe
30+
```
31+
32+
## Usage
33+
34+
To run the hand detection module, execute the script:
35+
36+
```bash
37+
python Hand_detection_module.py
38+
```
39+
40+
The script will open your webcam and start detecting hand landmarks in real-time. Press 'x' to exit the application.
41+
42+
## How It Works
43+
44+
1. **Video Capture**: The script captures video from the default webcam using OpenCV.
45+
2. **Frame Processing**: Each frame is converted to RGB and processed by the MediaPipe Hands model to detect hand landmarks.
46+
3. **Landmark Detection**: The detected landmarks are identified and their coordinates are calculated.
47+
4. **Landmark Highlighting**: Specific landmarks (like the thumb tip) are highlighted on the video feed using colored circles.
48+
5. **FPS Calculation**: The script calculates and displays the frames per second (FPS) to monitor performance.
49+
50+
### Key Sections of the Code
51+
52+
- **Hand Landmark Detection**: The code uses `mp.solutions.hands` to detect hand landmarks and `mp.solutions.drawing_utils` to draw them on the video feed.
53+
- **Specific Landmark Highlighting**: The tip of the thumb (landmark ID 4) is highlighted with a filled circle.
54+
- **FPS Display**: The FPS is calculated based on the time difference between consecutive frames and displayed on the video feed.
55+
56+
## Limitations
57+
58+
- **Lighting Conditions**: The accuracy of hand detection can be affected by poor lighting conditions.
59+
- **Single Camera Source**: The script is designed to work with a single webcam source and may require modifications for other video inputs.
60+
- **Dependency on MediaPipe**: This module heavily relies on the MediaPipe library, so any updates or changes in MediaPipe might require adjustments to the code.
61+

0 commit comments

Comments
 (0)