首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得此可迭代枚举(results.pose_landmarks.landmark)的一个特定值(具有特定索引)?

如何获得此可迭代枚举(results.pose_landmarks.landmark)的一个特定值(具有特定索引)?
EN

Stack Overflow用户
提问于 2021-12-31 20:43:37
回答 2查看 449关注 0票数 0
代码语言:javascript
复制
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所做的事情,但不是对枚举()中的所有元素( id )执行,而是只对id= 25执行,因为在这种情况下,这是我感兴趣的唯一点。

我需要在循环的这个指令中更改什么,它使用的是这个enumerate(results.pose_landmarks.landmark):的可迭代性。

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

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

代码语言:javascript
复制
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)) )
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-02 20:16:47

您可以直接访问results.pose_landmarks.landmark,不需要枚举。

代码语言:javascript
复制
lm = results.pose_landmarks.landmark[25]

results.pose_landmarks.landmark是一个列表

票数 1
EN

Stack Overflow用户

发布于 2021-12-31 20:54:12

你可以试试:

代码语言:javascript
复制
for id, lm in enumerate(results.pose_landmarks.landmark):
    if not id == 25:
        continue
    ...

不优雅,但能完成任务。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70545556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档