Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mediapipe lagging with Tkinter #5353

Closed
NishanthThePythoner opened this issue Apr 26, 2024 · 4 comments
Closed

Mediapipe lagging with Tkinter #5353

NishanthThePythoner opened this issue Apr 26, 2024 · 4 comments
Assignees
Labels
legacy:holistic Issues related to Holistic platform:python MediaPipe Python issues type:support General questions

Comments

@NishanthThePythoner
Copy link

NishanthThePythoner commented Apr 26, 2024

I've been trying to use Mediapipe detection model with Tkinter but the result is a very laggy video. Here is the code I'm using:

import cv2
from custom_funcs import mediapipe_detection
from custom_funcs import  draw_styled_landmarks
from custom_funcs import mp_holistic 
from tkinter import *
import cv2 
from PIL import Image, ImageTk 

width, height = 800, 600

cap = cv2.VideoCapture(0)

cap.set(cv2.CAP_PROP_FRAME_WIDTH, width) 
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height) 

app = Tk() 
app.bind('<Escape>', lambda e: app.quit()) 

label_widget = Label(app) 
label_widget.pack() 

def open_camera():
  
 with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
    ret, frame = cap.read()
    image, results = mediapipe_detection(frame, holistic)
    draw_styled_landmarks(image, results)
      
  opencv_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGBA)
  captured_image = Image.fromarray(opencv_image)
  photo_image = ImageTk.PhotoImage(image=captured_image)
  label_widget.photo_image = photo_image
  label_widget.configure(image=photo_image) 

  label_widget.after(1, open_camera) 
          

button1 = Button(app, text="Open Camera", command=open_camera) 
button1.pack() 

app.mainloop()

Here's the code written in custom_funcs module:

import cv2
import numpy as np
import mediapipe as mp

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

def extract_keypoints(results):
    lh = np.array([[res.x, res.y, res.z] for res in results.left_hand_landmarks.landmark]).flatten() if results.left_hand_landmarks else np.zeros(21*3)
    rh = np.array([[res.x, res.y, res.z] for res in results.right_hand_landmarks.landmark]).flatten() if results.right_hand_landmarks else np.zeros(21*3)
    return np.concatenate([lh, rh])

def mediapipe_detection(image, model):
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image.flags.writeable = False
    results = model.process(image)
    image.flags.writeable = True
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    return image, results

def draw_styled_landmarks(image, results):
    mp_drawing.draw_landmarks(image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS, 
                             mp_drawing.DrawingSpec(color=(121,22,76), thickness=2, circle_radius=4), 
                             mp_drawing.DrawingSpec(color=(121,44,250), thickness=2, circle_radius=2)
                             )
    mp_drawing.draw_landmarks(image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS, 
                             mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=4), 
                             mp_drawing.DrawingSpec(color=(245,66,230), thickness=2, circle_radius=2)
                             )
@NishanthThePythoner NishanthThePythoner added the type:others issues not falling in bug, perfromance, support, build and install or feature label Apr 26, 2024
@NishanthThePythoner
Copy link
Author

@kuaashish

@kuaashish kuaashish added type:support General questions legacy:holistic Issues related to Holistic platform:python MediaPipe Python issues and removed type:others issues not falling in bug, perfromance, support, build and install or feature labels Apr 30, 2024
@kuaashish
Copy link
Collaborator

kuaashish commented Apr 30, 2024

Hi @NishanthThePythoner,

We apologize for the delay in responding. We have noticed that your current standalone code utilizes the legacy holistic solution which has been upgraded to the new Holistic Task API, which offers improved functionality. The new Task APIs simplify the process of building and customizing machine learning solutions for your applications and support for legacy Holistic solution is completely ended.

Would you be willing to try out the new HolisticLandmarker task APIs available here? Please let us know if you encounter any issues during implementation. We apologize to inform you that documentation for implementing the new Holistic Task API is not yet available, but we are actively working on it and will be out soon.

Thank you!!

@kuaashish kuaashish added the stat:awaiting response Waiting for user response label Apr 30, 2024
Copy link

github-actions bot commented May 8, 2024

This issue has been marked stale because it has no recent activity since 7 days. It will be closed if no further activity occurs. Thank you.

@github-actions github-actions bot added the stale label May 8, 2024
Copy link

This issue was closed due to lack of activity after being marked stale for past 7 days.

@kuaashish kuaashish removed stat:awaiting response Waiting for user response stale labels May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy:holistic Issues related to Holistic platform:python MediaPipe Python issues type:support General questions
Projects
None yet
Development

No branches or pull requests

2 participants