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):
我將如何輸入 anid[25]和 a lm[25]?
我嘗試使用 itertools,但在這種情況下它確實有效
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)) )
uj5u.com熱心網友回復:
編輯:您可以嘗試:
for id, lm in enumerate(results.pose_landmarks.landmark):
if not id == 25:
continue
...
不優雅,但可以完成作業。
Edit2:舊答案已洗掉。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/400099.html
標籤:Python 蟒蛇-3.x opencv 姿态估计 媒体管道
上一篇:為什么棋子在python-chess中朝相反的方向移動
下一篇:如何按唯一鍵值合并字典串列
