最近在做一个在nano上的姿态识别和手势识别的项目,看到了mediapipe想玩玩看,于是借鉴了别人的一些代码跑了跑看看效果如何,出现了以下问题:
这里显示我mediapipe没有某个模块,我重装了多遍的mediapipe,以及更换了多个py版本3.10,3.9等还是这个情况,请问该怎么解决
这是我的环境
import os
import time
import cv2 as cv
import mediapipe as mp
class BodyPoseDetect:
def __init__(self, static_image=False, complexity=1, smooth_lm=True, segmentation=False, smooth_sm=True,
detect_conf=0.5, track_conf=0.5):
self.mp_body = mp.solutions.pose
self.mp_draw = mp.solutions.drawing_utils
self.body = self.mp_body.Pose(static_image, complexity, smooth_lm, segmentation, smooth_sm, detect_conf,
track_conf)
def detect_landmarks(self, img, disp=True):
img_rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
results = self.body.process(img_rgb)
detected_landmarks = results.pose_landmarks
if detected_landmarks:
if disp:
self.mp_draw.draw_landmarks(img, detected_landmarks, self.mp_body.POSE_CONNECTIONS)
return detected_landmarks, img
def get_info(self, detected_landmarks, img_dims):
lm_list = []
if not detected_landmarks:
return lm_list
height, width = img_dims
for id, b_landmark in enumerate(detected_landmarks.landmark):
cord_x, cord_y = int(b_landmark.x * width), int(b_landmark.y * height)
lm_list.append([id, cord_x, cord_y])
return lm_list
def main(is_image):
if is_image:
detector = BodyPoseDetect(static_image=True)
ori_img = cv.imread('image.jpg')
img = ori_img.copy()
landmarks, output_img = detector.detect_landmarks(img)
info_landmarks = detector.get_info(landmarks, img.shape[:2])
# print(info_landmarks[3])
cv.imshow("Original", ori_img)
cv.imshow("Detection", output_img)
cv.waitKey(0)
else:
detector = BodyPoseDetect()
cap = cv.VideoCapture(0)
prev_time = time.time()
cur_time = 0
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
out = cv.VideoWriter('output.avi', cv.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10,
(frame_width, frame_height)) # 保存视频
while True:
ret, frame = cap.read()
if not ret:
print("Video Over")
break
img = frame.copy()
landmarks, output_img = detector.detect_landmarks(img)
info_landmarks = detector.get_info(landmarks, img.shape[:2])
cur_time = time.time()
fps = 1 / (cur_time - prev_time)
prev_time = cur_time
cv.putText(output_img, f'FPS: {str(int(fps))}', (10, 70), cv.FONT_HERSHEY_COMPLEX_SMALL, 2, (0, 50, 170), 2)
cv.namedWindow('Original', cv.WINDOW_NORMAL) # 窗口大小可设置
cv.resizeWindow('Original', 580, 330) # 重设大小
cv.namedWindow('Detection', cv.WINDOW_NORMAL) # 窗口大小可设置
cv.resizeWindow('Detection', 580, 330) # 重设大小
out.write(output_img)
cv.imshow("Detection", output_img)
if cv.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()
if __name__ == "__main__":
is_image = False
main(is_image)
这是我的代码,若是代码出现问题欢迎指正