2

I'm trying to make a python script inside blender that uses the mediapipe module to get the coordinates of the landmarks of my hands, face, and body and then i want to move an object to thosse specific coordinates

I got the coordinate part done but i'm having trouble getting it to work inside blender

import mediapipe as mp
import inspect
import cv2

mp_drawing = mp.solutions.drawing_utils
mp_holistic = mp.solutions.holistic

cap = cv2.VideoCapture(0)
# Initiate holistic model
with mp_holistic.Holistic(min_detection_confidence=0, min_tracking_confidence=0) as holistic:
    
    while cap.isOpened():
        ret, frame = cap.read()
        
        # Recolor Feed
        image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        # Make Detections
        results = holistic.process(image)
        # print(results.face_landmarks)
        
        # face_landmarks, pose_landmarks, left_hand_landmarks, right_hand_landmarks
        
        # Recolor image back to BGR for rendering
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        

        if bool(results.right_hand_landmarks):
            print(results.right_hand_landmarks)
            for data_point in results.right_hand_landmarks.landmark:
                print("X = " + str(data_point.x))
                print("Y = " + str(data_point.y))
                print("Z = " + str(data_point.z))
                # Center coordinates
                h, w, c = image.shape
                center_coordinates = (int(w * data_point.x), int(h * data_point.y))
 
                # Radius of circle
                radius = 5
  
                # Blue color in BGR
                color = (int(-1000 * data_point.z), int(-1000 * data_point.z), int(-1000 * data_point.z))
  
                # Line thickness of 2 px
                thickness = 5
  
                # Using cv2.circle() method
                # Draw a circle with blue line borders of thickness of 2 px
                image = cv2.circle(image, center_coordinates, radius, color, thickness)
                cv2.imshow('Raw Webcam Feed', image)

        if cv2.waitKey(10) & 0xFF == ord('q'):
            break

cap.release()
cv2.destroyAllWindows()

this script would print (and also show) the coordinate of the hand landmarks but what im having trouble with is that this is in a while loop and blender would freeze imediatly after i run my script and would only turn back on when i stopped the script. I've found a template for "operator modal timer" which can run a script in a kind of like while loop while still giving time for blender to refresh and do all of its other things.

import bpy


class ModalTimerOperator(bpy.types.Operator):
    """Operator which runs its self from a timer"""
    bl_idname = "wm.modal_timer_operator"
    bl_label = "Modal Timer Operator"

    _timer = None

    def modal(self, context, event):
        if event.type in {'RIGHTMOUSE', 'ESC'}:
            self.cancel(context)
            return {'CANCELLED'}

        if event.type == 'TIMER':
            # change theme color, silly!
            


        return {'PASS_THROUGH'}

    def execute(self, context):
        wm = context.window_manager
        self._timer = wm.event_timer_add(0.1, window=context.window)
        wm.modal_handler_add(self)
        return {'RUNNING_MODAL'}

    def cancel(self, context):
        wm = context.window_manager
        wm.event_timer_remove(self._timer)


def register():
    bpy.utils.register_class(ModalTimerOperator)


def unregister():
    bpy.utils.unregister_class(ModalTimerOperator)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.wm.modal_timer_operator()

But the issue is i can't use it since i would need to start and stop the webcam each frame which would be very slow. So what im trying to ask is : Can i somehow put the whole hand landmarks script into a subprocces or a seperately running program which would just send the x,y,z coordinates so that blender can grab it (using the modal timer each second) and move the object to that position?

metalex
  • 131
  • 9

0 Answers0