-
Notifications
You must be signed in to change notification settings - Fork 0
/
test
162 lines (112 loc) · 4.3 KB
/
test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import bpy
from bpy.types import Panel, Operator, PropertyGroup, FloatProperty, PointerProperty
from bpy.utils import register_class, unregister_class
from bpy_extras.io_utils import ImportHelper
import cv2
import mediapipe as mp
user_camera = None # Define user_camera here as None
def setup_camera():
bpy.ops.object.camera_add()
cam = bpy.context.active_object
cam.name = "UserCamera"
cam.data.clip_end = 100
cam.data.lens = 18
bpy.context.scene.camera = cam
return cam
def full_setup():
global user_camera
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False, confirm=False)
bpy.ops.object.add(radius=1.0, type='EMPTY')
pose = bpy.context.active_object
pose.name = "Pose"
bpy.ops.object.add(radius=1.0, type='EMPTY')
body = bpy.context.active_object
body.name = "Body"
body.parent = pose
pose = bpy.context.scene.objects["Pose"]
bpy.ops.object.add(radius=1.0, type='EMPTY')
face = bpy.context.active_object
face.name = "Face"
face.parent = pose
for k in range(468):
bpy.ops.mesh.primitive_cube_add()
box = bpy.context.active_object
box.name = str(k).zfill(3) + "Face"
box.scale = (0.002, 0.002, 0.002)
box.parent = face
face = bpy.context.scene.objects["Face"]
user_camera = setup_camera()
return face
def run_face():
global user_camera
user_camera = setup_camera()
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
cap = cv2.VideoCapture(0)
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
while cap.read():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
results = pose.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.pose_landmarks:
left_eye = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_EYE]
right_eye = results.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_EYE]
scale = 0.5
# Calculate the average position of left and right eyes
avg_position = [(left_eye.x + right_eye.x) / 2, (left_eye.y + right_eye.y) / 2, (left_eye.z + right_eye.z) / 2]
# Update the camera location based on the average position
user_camera.location.x = (0.5 - avg_position[0]) * scale
user_camera.location.y = (avg_position[2] - 0.5) * scale + 0.5
user_camera.location.z = (0.5 - avg_position[1]) * scale
# Simple rotation calculation
user_camera.rotation_euler.x = 1.3
user_camera.rotation_euler.y = 0
user_camera.rotation_euler.z = 0
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
image = cv2.flip(image, 1)
cv2.imshow('MediaPipe Pose', image)
if cv2.waitKey(1) & 0xFF == 27:
break
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
cap.release()
cv2.destroyAllWindows()
class RunOperator(bpy.types.Operator):
bl_idname = "object.run_body_operator"
bl_label = "Run Face Operator"
def execute(self, context):
run_face()
return {'FINISHED'}
class MediaPipePanel(bpy.types.Panel):
bl_label = "MediaPipe v1"
bl_category = "MediaPipe"
bl_idname = "MediaPipe"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_label = "MediaPipe v1"
def draw(self, context):
layout = self.layout
obj = context.object
row = layout.row()
row = layout.row()
row.label(text="Run Motion Capture")
row = layout.row()
row.operator(RunOperator.bl_idname, text="Camera")
row = layout.row()
row.label(text="(Press escape to stop)")
_classes = [
MediaPipePanel,
RunOperator,
]
def register():
for c in _classes: register_class(c)
def unregister():
for c in _classes: unregister_class(c)
if __name__ == "__main__":
register()