利用opencv 和 mediapipe 获取视频中每帧的手势动作信息,在blender 中将每帧的信息绘制处理,最后组合成动画。本实例是非实时的手势动作捕获。
源码import numpy as np
import mediapipe as mp
import cv2
import bpy
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
cap = cv2.VideoCapture(r'd:/output-hand.mp4')
max_num_hands = 1
min_detection_confidence = 0.6
factor = 10
resize = 0.2
drawed = False
n = 0
bpy.context.scene.frame_start = 0
try:
for obj in bpy.data.objects:
if obj.type == 'MESH':
bpy.data.objects.remove(obj)
except:
pass
with mp_hands.Hands(
static_image_mode=True,
max_num_hands=max_num_hands,
min_detection_confidence=min_detection_confidence) as hands:
while(cap.isOpened()):
n += 1
ret, image = cap.read()
bpy.context.scene.frame_set(n)
try:
image = cv2.flip(image, 1)
results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
if results.multi_hand_landmarks:
print(image.shape)
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
for i,lm in enumerate(hand_landmarks.landmark):
if not drawed:
bpy.ops.mesh.primitive_uv_sphere_add(location=(0,0,0))
bpy.context.active_object.name = 'p'+str(i)
bpy.ops.transform.resize(value=(resize, resize, resize))
bpy.ops.transform.translate(value=(lm.x*factor, lm.y*factor, lm.z*factor))
else:
# for i in range(21):
myobj = bpy.data.objects.get(f'p{i}')
if myobj:
myobj.location.x = lm.x*factor
myobj.location.y = lm.y*factor
myobj.location.z = lm.z*factor
# Insert new keyframe for "location" like this
myobj.keyframe_insert(data_path="location")
drawed = True
# print(i,lm.z)
# cv2.imshow('frame',image)
# if cv2.waitKey(10) & 0xFF == ord('q'):
# break
except:
break
cap.release()
# cv2.destroyAllWindows()
bpy.context.scene.frame_end = n+1
bpy.ops.object.select_all(action='SELECT')
部分分析
bpy.context.scene.frame_set(n)
设置当前帧数
myobj = bpy.data.objects.get(f'p{i}')
if myobj:
myobj.location.x = lm.x*factor
myobj.location.y = lm.y*factor
myobj.location.z = lm.z*factor
# Insert new keyframe for "location" like this
myobj.keyframe_insert(data_path="location")
更新物体的坐标,坐标更新后将最新信息插入到当前帧中 myobj.keyframe_insert(data_path="location")
bpy.context.scene.frame_end = n+1
video信息读取完后,设置最终帧数。
在blender中 Shift F12 进入 Dope Sheet 窗口,按下空格键播放。
以上就是“python 在blender中展示手势动作信息教程”的详细内容,想要了解更多Python教程欢迎持续关注编程学习网。
扫码二维码 获取免费视频学习资料
- 本文固定链接: http://phpxs.com/post/11353/
- 转载请注明:转载必须在正文中标注并保留原文链接
- 扫码: 扫上方二维码获取免费视频资料
查 看2022高级编程视频教程免费获取