【问题标题】:How to get one of the specific values(with the specific index) of this iterable enumerate (results.pose_landmarks.landmark)?如何获取此可迭代枚举(results.pose_landmarks.landmark)的特定值(具有特定索引)之一?
【发布时间】:2021-12-31 20:43:37
【问题描述】:
import cv2 #OpenCV is the library that we will be using for image processing
import mediapipe as mp #Mediapipe is the framework that will allow us to get our pose estimation
import time


mpDraw = mp.solutions.drawing_utils
mpPose = mp.solutions.pose

pose = mpPose.Pose()
#pose = mpPose.Pose(static_image_mode = False, upper_body_only = True) #ONLY UPPER_BODY_TRACKING

#cap = cv2.VideoCapture(0)
cap = cv2.VideoCapture('PoseVideos/1_girl_choreography.mp4')

pTime = 0 #previous time

while True:
    success, img = cap.read() #that will give it our image and then we can write the cv2.imshow()
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #convert our image to RGB because Mediapipe use that format
    results = pose.process(imgRGB) #we are simply going to send this image to our model

    #print(enumerate(results.pose_landmarks.landmark)) #<enumerate object at 0x0000012312DD1A00>

    #so then we will check if it is detected or not
    if results.pose_landmarks:

        mpDraw.draw_landmarks(img, results.pose_landmarks, mpPose.POSE_CONNECTIONS) 

        for id, lm in enumerate(results.pose_landmarks.landmark):

            h, w, c = img.shape #get dimensions(h height, w width) and the c channel of image
            
            print(id)
            print(lm)

            cx, cy = int(lm.x * w), int(lm.y * h)

            cv2.circle(img, (cx, cy), 5, (255, 0, 0), cv2.FILLED)


    cTime = time.time()
    fps = 1 / (cTime - pTime)
    pTime = cTime

    cv2.putText(img, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)

    cv2.imshow("Image", img)
    cv2.waitKey(1)

我想做 for 所做的事情,但不是对 enumerate () 中的所有元素 (id) 做它,而是只为 id = 25 做它,因为在这种情况下,这是我唯一感兴趣的点.

我需要在循环的这条指令中改变什么,该循环使用可迭代到这个enumerate(results.pose_landmarks.landmark):

如何输入id[25]lm[25]

我尝试使用 itertools,但在这种情况下 ir 确实有效

import itertools

gen = (id, lm for id, lm in enumerate(results.pose_landmarks.landmark))
specific_id, specific_lm = 25,25 #index
print( next(itertools.islice(gen, specific_id, None)) )

【问题讨论】:

    标签: python python-3.x opencv pose-estimation mediapipe


    【解决方案1】:

    可以直接访问results.pose_landmarks.landmark,无需枚举。

    lm = results.pose_landmarks.landmark[25]
    

    results.pose_landmarks.landmark 是一个列表

    【讨论】:

      【解决方案2】:

      编辑:你可以试试:

      for id, lm in enumerate(results.pose_landmarks.landmark):
          if not id == 25:
              continue
          ...
      

      不优雅,但可以完成工作。

      Edit2:旧答案已删除。

      【讨论】:

      • 我尝试了您在该线程中所说的内容,但一直失败。我不认为这是一个重复的问题。在那里我更新了我的问题,但它仍然对我不起作用。
      猜你喜欢
      • 2022-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      相关资源
      最近更新 更多