0% found this document useful (0 votes)
4 views2 pages

Code

The document contains a Python script that uses OpenCV and MediaPipe to capture video from a webcam and detect human poses. It processes the video frames to extract landmark data, visualizes the pose on the video feed, and saves the landmark information to a CSV file. The script runs until a specified number of frames is collected or the user quits the application.

Uploaded by

quochuynd112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Code

The document contains a Python script that uses OpenCV and MediaPipe to capture video from a webcam and detect human poses. It processes the video frames to extract landmark data, visualizes the pose on the video feed, and saves the landmark information to a CSV file. The script runs until a specified number of frames is collected or the user quits the application.

Uploaded by

quochuynd112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 2

import cv2

import mediapipe as mp
import pandas as pd
# Đọc ảnh từ webcam
cap=cv2.VideoCapture(0)

# Khởi tạo thư viện mediapipe


mpPose=mp.solutions.pose
pose=mpPose.Pose()
mpDraw=mp.solutions.drawing_utils

lm_list=[]
label="HANDSWING3"
no_of_frames=600

def make_landmark_timestep(results):
print(results.pose_landmarks.landmark)
c_lm = []
for id, lm in enumerate(results.pose_landmarks.landmark):
c_lm.append(lm.x)
c_lm.append(lm.y)
c_lm.append(lm.z)
c_lm.append(lm.visibility)
return c_lm

def draw_landmark_on_image(mpDraw,results,img):
# Vẽ các đường nối

mpDraw.draw_landmarks(img,results.pose_landmarks,mpPos
e.POSE_CONNECTIONS)
# vẽ các điểm nút
for id, lm in enumerate(results.pose_landmarks.landmark):
h,w,c=img.shape
print(id,lm)
cx,cy=int(lm.x *w), int(lm.y *h)
cv2.circle(img,(cx,cy),10,(0,0,255),cv2.FILLED)
return img

while len(lm_list)<= no_of_frames:


ret,frame=cap.read()
if ret:
# Nhận diện pose
frameRGB=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
results=pose.process(frameRGB)
if results.pose_landmarks:
# Ghi nhận thông số khung xương
lm=make_landmark_timestep(results)
lm_list.append(lm)
# Vẽ khung xương lên ảnh

frame=draw_landmark_on_image(mpDraw,results,frame)
cv2.imshow("image",frame)
if cv2.waitKey(1)==ord('q'):
break
# Write vào file csv
df=pd.DataFrame(lm_list)
df.to_csv(label+".txt")
cap.release()
cv2.destroyAllWindows()

You might also like