Bluebeach_wjm 2023-10-09 21:37 采纳率: 60%
浏览 46
已结题

AttributeError: module 'mediapipe' has no attribute 'solutions'

最近在做一个在nano上的姿态识别和手势识别的项目,看到了mediapipe想玩玩看,于是借鉴了别人的一些代码跑了跑看看效果如何,出现了以下问题:

img


这里显示我mediapipe没有某个模块,我重装了多遍的mediapipe,以及更换了多个py版本3.10,3.9等还是这个情况,请问该怎么解决

这是我的环境

img

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)

这是我的代码,若是代码出现问题欢迎指正

  • 写回答

3条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-10-11 01:56
    关注

    【以下回答由 GPT 生成】

    问题的原因是mendipi molue中缺少solutions模块,解决方法如下:

    1. 首先,确保你使用的是mediapipe的最新版本。可以通过以下命令升级mediapipe:
    pip install --upgrade mediapipe
    
    1. 如果升级后仍然出现问题,可以尝试使用以下代码来引入正确的mediapipe模块:
    import mediapipe.python.solutions as mp_solutions
    
    1. 修改你的代码,将mediapipe.solutions部分替换为mp_solutions,并修改对应的其他地方,比如将mp_body.Pose替换为mp_solutions.pose.Pose,将mp_draw.draw_landmarks替换为mp_solutions.drawing_utils.draw_landmarks

    修改后的代码如下:

    import os
    import time
    import cv2 as cv
    import mediapipe.python.solutions as mp_solutions
    
    
    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", output_img)
    

    这样你可以尝试运行代码,如果还有其他问题,欢迎继续提问。


    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月7日
  • 已采纳回答 10月30日
  • 创建了问题 10月9日

悬赏问题

  • ¥20 需要帮我远程操控一下,运行一下我的那个代码,我觉得我无能为力了
  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。
  • ¥15 请问用arcgis处理一些数据和图形,通常里面有一个根据点划泰森多边形的命令,直接划的弊端是只能执行一个完整的边界,但是我们有时候会用到需要在有很多边界内利用点来执行划泰森多边形的命令
  • ¥30 在wave2foam中执行setWaveField时遇到了如下的浮点异常问题,请问该如何解决呢?
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?