|
1- |
Py-Opencv-HandTracking
Python program, Building a
Hand Tracking System Using Opencv |
|
|
| |
|
|
-
Py-Opencv-HandTracking Python program, Building
a Hand Tracking System Using Opencv
-
Hand Landmark Model
.
-
Install the required modules of library
-
Define the main function:
Check for the Working of the Webcam - (webcam = cv2.VideoCapture(0))
Read the frames from the webcam and
convert the image to RGB.
Detect hands in the frame with the help of
“hands.process()” function.
Once the hands get detected:
1- locate the key points
2- we highlight the dots in the keypoints using cv2.circle,
3- and connect the key points using mpDraw.draw_landmarks.
|
| |
- Create a new Python project In Visual Studio
2022
- the
Microsoft Visual Studio (... or 2019 or 2022) is
a powerful IDE for Python language
- Open/Run
Microsoft Visual Studio 2022
- To view
Python templates, search for python.
 |
Select the
Python
Application template, and select
Next. |
- Create a new Python project In
Visual Studio 2022
On the Configure your new project screen
- (specify a name and file location
for the project, and then select Create)
Project name:
Py-Opencv-HandTracking
Location:
C:\Users\...\source\repos
(default location for Visual
Studio 2022)
|
- The new project opens in Visual Studio
2022 - (Visual Studio 2022
Compiler - IDE, to compile Python project /
file )
- The
Visual Studio Solution Explorer window shows the
project structure
- Python
Project Properieties - Py-Opencv-HandTracking
- Projct
Folder: C:\Users\...\source\repos\Py-Opencv-HandTracking
- Startup
File: Py-Opencv-HandTracking.py
- Project
file:
Py-Opencv-HandTracking.sln
|
- Download Python
Project : Py-Opencv-HandTracking.zip -
(11.1 KB zip file) Download
- Project
consist of:
One Python Form -
(Py_Opencv_HandTracking.py )
|
 | |
|
Download Python
Project : Py-Opencv-HandTracking.zip -
(11.1 KB zip file) Download
|
|
|
|
|
- Install opencv, mediapipe and time in Python on Windows
Operating System, Using Command Prompt
| 1-
Install opencv CV in Python on Windows Operating System,
Using Command Prompt |
|
Note: cv2 the same as OpenCV |
In the
Command prompt, use the following pip
command to install the
opencv library
pip install opencv-python |
| |
| 2-
Install mediapipe in Python on Windows Operating
System, Using Command Prompt |
|
pip install mediapipe |
| |
| 3-
Install time in Python on Windows Operating
System, Using Command Prompt |
|
pip install lime |
| |
| |
Source Code of Python Project -Py-Opencv-HandTracking :
|
Download this Source Code at
python file: Py_Opencv_HandTracking.py - (1.70 KB Python file)
download
|
 |
Continue
|
Py_Opencv_HandTracking.py Python Code, Building
a Hand Tracking System Using Openc with Webcam | |
|
|
| |
# Building a Hand Recognition Tracking System
using webcam
import
cv2 import
mediapipe
as mp import
time
# Access the HandLandmark enum
hand_landmarks_enum =
mp.solutions.hands.HandLandmark
# Iterate through the enum and print each
landmark name
print('') print("1-
Hand Landmark Key Names: ") for
landmark
in
hand_landmarks_enum:
print(landmark.name)
cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands
hands = mpHands.Hands(static_image_mode=False,
max_num_hands=2,
min_detection_confidence=0.5,
min_tracking_confidence=0.5)
mpDraw = mp.solutions.drawing_utils
pTime = 0
cTime = 0
while
True:
# Read video frame by frame
success, img = cap.read()
# Convert BGR image to RGB image
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Process the RGB image
results = hands.process(imgRGB)
# If hands are present in image(frame)
if
results.multi_hand_landmarks:
for
handLms
in
results.multi_hand_landmarks:
for
id,
lm
in
enumerate(handLms.landmark):
h, w, c = img.shape
cx, cy =
int(lm.x
*w),
int(lm.y*h)
#if id ==0:
cv2.circle(img, (cx,cy), 3, (255,0,255),
cv2.FILLED)
mpDraw.draw_landmarks(img, handLms,
mpHands.HAND_CONNECTIONS)
cTime = time.time()
fps = 1/(cTime-pTime)
pTime = cTime
cv2.putText(img,str(int(fps)),
(10,70), cv2.FONT_HERSHEY_PLAIN, 3, (255,0,255),
3) cv2.imshow("Image",
img)
cv2.waitKey(1)
# Release the video capture and close windows
cap.release()
cv2.destroyAllWindows() |
|
| | |
|
|
|
|
- Debug Py-Opencv-HandTracking project, 0 Errors
Output of Python Project -Py-Opencv-HandTracking,
is Using the Webcam of PC
:
|
|
|
1- |
Output info,Info and Hand Ladmark Key
Names |
| |
 |
| |
|
| |
|
|
2- |
the Hands Tracking
Images Models Output: |
|
|
|
2.1- |
the Right Hand
Tracking Image |
| |
 |
| |
|
|
2.2- |
the Left Hand
Tracking Image |
| |
 |
|
|
|
|
|
| | | |
| |
|
| |
|
|
2- |
Py-Opencv-BodyTracking
Python program, Building a BodyTracking System Using Opencv |
|
|
| |
|
|
-
Py-Opencv-BodyTracking Python program, Building
a Body Tracking System Using Opencv
-
The pose landmarker model tracks 33 body
landmark locations, representing the
approximate location of the following body
parts:
|
 |
| The model output
contains both normalized coordinates
(Landmarks) and world coordinates (WorldLandmarks)
for each landmark |
-
Install the required modules of library
-
Define the main function:
Check for the Working of the Webcam - (webcam = cv2.VideoCapture(0))
1- locate the key points of Landmark Key numbers and names or
Body/Pose Landmark
2- Get the Position of image displayed, activate Print - (print(lmList))
3- Show image Pose Landmark.
|
| |
- Create a new Python project In Visual Studio
2022
- the
Microsoft Visual Studio (... or 2019 or 2022) is
a powerful IDE for Python language
- Open/Run
Microsoft Visual Studio 2022
- To view
Python templates, search for python.
 |
Select the
Python
Application template, and select
Next. |
- Create a new Python project In
Visual Studio 2022
On the Configure your new project screen
- (specify a name and file location
for the project, and then select Create)
Project name:
Py-Opencv-BodyTracking
Location:
C:\Users\...\source\repos
(default location for Visual
Studio 2022)
|
- The new project opens in Visual Studio
2022 - (Visual Studio 2022
Compiler - IDE, to compile Python project /
file )
- The
Visual Studio Solution Explorer window shows the
project structure
- Python
Project Properieties - Py-Opencv-BodyTracking
- Projct
Folder: C:\Users\...\source\repos\Py-Opencv-BodyTracking
- Startup
File: Py_Opencv_BodyTracking.py
- Project
file:
Py-Opencv-BodyTracking.sln
|
- Download Python
Project : Py-Opencv-BodyTracking.zip -
(13.8 KB zip file) Download
- Project
consist of:
One Python Form -
(Py_Opencv_BodyTracking.py )
|
 | |
|
Download Python
Project : Py-Opencv-BodyTracking.zip -
(13.8 KB zip file) Download
|
|
|
- Install opencv, mediapipe and time in Python on Windows
Operating System, Using Command Prompt
| 1-
Install opencv CV in Python on Windows Operating System,
Using Command Prompt |
|
Note: cv2 the same as OpenCV |
In the
Command prompt, use the following pip
command to install the
opencv library
pip install opencv-python |
| |
| 2-
Install mediapipe in Python on Windows Operating
System, Using Command Prompt |
|
pip install mediapipe |
| |
| 3-
Install time in Python on Windows Operating
System, Using Command Prompt |
|
pip install lime |
| |
| |
Source Code of Python Project -Py-Opencv-BodyTracking :
|
Download this Source Code at
python file: Py_Opencv_BodyTracking.py - (3.06 KB Python file)
download
|
 |
Continue
|
Py_Opencv_BodyTracking.py
Python Code, to Crea | |
|
|
| |
# Pose Landmark or Body Landmark
import
cv2 import
mediapipe
as mp import
math
# Accessing a specific landmark
nose_landmark =
mp.solutions.pose.PoseLandmark.NOSE
# You can iterate through all landmarks
print('') print("
Pose Landmark Key Names: ") for
landmark_enum
in
mp.solutions.pose.PoseLandmark:
print(landmark_enum.value,'
- ',landmark_enum.name)
# Prints integer index and string name of the
landmark
print('')
class
poseDetector():
def
__init__(self,
mode=False,
smooth=True,
detectionCon=0.5, trackCon=0.5):
self.mode
= mode
self.smooth
= smooth
self.detectionCon
= detectionCon
self.trackCon
= trackCon
self.pTime
= 0
self.mpDraw
= mp.solutions.drawing_utils
self.mpPose
= mp.solutions.pose
self.pose
=
self.mpPose.Pose(static_image_mode=self.mode, smooth_landmarks=self.smooth, min_detection_confidence=self.detectionCon, min_tracking_confidence=self.trackCon)
def
findPose(self,
img, draw=True):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results
=
self.pose.process(imgRGB)
if
self.results.pose_landmarks:
if
draw:
self.mpDraw.draw_landmarks(img,
self.results.pose_landmarks,
self.mpPose.POSE_CONNECTIONS)
return
img
def
getPosition(self,
img):
self.lmList
= []
if
self.results.pose_landmarks:
for
id,
lm
in
enumerate(self.results.pose_landmarks.landmark):
h, w, c = img.shape
cx, cy =
int(lm.x
* w),
int(lm.y
* h)
self.lmList.append([id,
cx, cy])
return
self.lmList
def
findAngle(self,
img, p1, p2, p3, draw=True):
# Get the landmark x1, y1 =
self.lmList[p1][1:] x2, y2 =
self.lmList[p2][1:] x3, y3 =
self.lmList[p3][1:]
# Calculate the angle
angle = math.degrees(math.atan2(y3 - y2, x3 -
x2) - math.atan2(y1 - y2, x1 - x2))
# some time this angle comes zero, so below
conditon we added
if
angle < 0:
angle += 360
# Draw
if
draw:
cv2.line(img, (x1, y1), (x2, y2), (255, 255,
255), 3)
cv2.line(img, (x3, y3), (x2, y2), (255, 255,
255), 3)
cv2.circle(img, (x1, y1), 10, (0, 0, 255),
cv2.FILLED)
cv2.circle(img, (x1, y1), 15, (0, 0, 255), 1)
cv2.circle(img, (x2, y2), 10, (0, 0, 255),
cv2.FILLED)
cv2.circle(img, (x2, y2), 15, (0, 0, 255), 1)
cv2.circle(img, (x3, y3), 10, (0, 0, 255),
cv2.FILLED)
cv2.circle(img, (x3, y3), 15, (0, 0, 255), 1)
return
angle def
main(): detector =
poseDetector() cap = cv2.VideoCapture(0)
while
True:
success, img = cap.read()
img = detector.findPose(img)
lmList = detector.getPosition(img)
#print(lmList) cv2.imshow("Image",
img)
cv2.waitKey(1)
if
__name__ ==
"__main__": main() |
|
| | |
|
|
|
|
- Debug Py-Opencv-BodyTracking project, 0 Errors
Output of Python Project -Py-Opencv-BodyTracking,
is Using the Webcam of PC
:
|
|
|
1- |
Info, Pose Landmark
Key numbers and names |
| |
 |
|
|
|
|
2- |
Show
of Output Data, is Using the Webcam of PC
- image |
| |
 |
|
Sample |
| |
 |
| |
|
|
|
|
| | | |
| |
|
|