昨天去那边 拿了最后的一点东西,成都算是彻底决断了
"""
当前使用Mediapipe去识别人体的关键点
"""
import cv2
import mediapipe as mp
import util
class Pose:
def __init__(self):
mpPose = mp.solutions.pose
self.pose = mpPose.Pose()
self.landmark = []
self.img = None
def process(self, img):
self.img = img
self.drawPointStyle()
if len(self.landmark) == 0:
return
self.armBuildCut()
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = pose.process(imgRGB)
print(result.pose_landmarks)
pointStyle = mp.solutions.drawing_utils.DrawingSpec(color=(0, 0, 255), thickness=2)
lineStyle = mp.solutions.drawing_utils.DrawingSpec(color=(0, 0, 255), thickness=2)
conn = mp.solutions.pose_connections
mp.solutions.drawing_utils.draw_landmarks(img, result.pose_landmarks, conn.POSE_CONNECTIONS, pointStyle, lineStyle)
self.drawPointStyle(img)
def drawPointStyle(self):
img = self.img
pose = self.pose
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = pose.process(imgRGB)
if not result.pose_landmarks:
return
print(result.pose_landmarks)
pointStyle = mp.solutions.drawing_utils.DrawingSpec(color=(0, 0, 255),
thickness=2)
lineStyle = mp.solutions.drawing_utils.DrawingSpec(color=(0, 0, 255),
thickness=2)
conn = mp.solutions.pose_connections
mp.solutions.drawing_utils.draw_landmarks(
img,
result.pose_landmarks,
conn.POSE_CONNECTIONS,
pointStyle,
lineStyle
)
# 绘制关键点的序号
landmark = result.pose_landmarks.landmark
imgH, imgW = img.shape[:2]
for idx, lm in enumerate(landmark):
x = int(lm.x * imgW)
y = int(lm.y * imgH)
cv2.putText(img,
str(idx),
org=(x - 15, y - 5),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=0.4,
color=(0, 255, 0),
thickness=1)
def armBuildCnt(self):
point12 = self.indexCvPoint(12)
point14 = self.indexCvPoint(14)
point16 = self.indexCvPoint(16)
angle = util.getAngle(point12, point14, point16)
if angle < 15:
print('------')
def indexCvPoint(self, index):
imgH, imgW = self.img.shape[:2]
lm = self.landmark[index]
x = lm.x * imgW
y = lm.y * imgH
return x, y
import cv2
from pose import Pose
class HandleCapture:
def __init__(self):
# 处理肢体关键点
self.pose = Pose()
def handelVideoCapture(self):
# 创建一个视频的捕获器 打开摄像头
cap = cv2.VideoCapture(0)
while cap.isOpened():
retval, frame = cap.read()
if not retval:
print('can not read image')
break
# 处理肢体关键点
frame = cv2.flip(frame, 1)
self.pose.process(frame)
cv2.imshow('frame', frame)
key = cv2.waitKey(25)
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
capture = HandleCapture()
capture.handelVideoCapture()