#πŸ”’ mediapipe

4 messages Β· Page 1 of 1 (latest)

lament spear
#

Help please. Cod don't open

import cv2
import mediapipe as mp
import time

Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅ΠΌ Π²ΠΈΠ΄Π΅ΠΎ Π·Π°Ρ…Π²Π°Ρ‚

cap = cv2.VideoCapture(0)

if not cap.isOpened():
print("Ошибка: Π½Π΅ ΡƒΠ΄Π°Π»ΠΎΡΡŒ ΠΎΡ‚ΠΊΡ€Ρ‹Ρ‚ΡŒ ΠΊΠ°ΠΌΠ΅Ρ€Ρƒ.")
exit()

Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅ΠΌ ΠΌΠΎΠ΄ΡƒΠ»ΡŒ Hands ΠΈΠ· MediaPipe

mp_hands = mp.solutions.hands
hands = mp_hands.Hands(static_image_mode=False, max_num_hands=2, min_detection_confidence=0.5, min_tracking_confidence=0.5)
mp_draw = mp.solutions.drawing_utils

pTime = 0 # ΠŸΡ€Π΅Π΄Ρ‹Π΄ΡƒΡ‰Π΅Π΅ врСмя для FPS расчСта

while True:
success, img = cap.read()
if not success:
print("НС ΡƒΠ΄Π°Π»ΠΎΡΡŒ ΡΡ‡ΠΈΡ‚Π°Ρ‚ΡŒ ΠΊΠ°Π΄Ρ€ с ΠΊΠ°ΠΌΠ΅Ρ€Ρ‹.")
break

# ΠŸΡ€ΠΈΠΌΠ΅Π½ΡΠ΅ΠΌ Ρ€Π°Π·ΠΌΡ‹Ρ‚ΠΈΠ΅ для сниТСния ΡˆΡƒΠΌΠΎΠ²
img = cv2.GaussianBlur(img, (5, 5), 0)

# ΠŸΠ΅Ρ€Π΅Π²ΠΎΠ΄ΠΈΠΌ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π² RGB
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(img_rgb)

# ΠŸΡ€ΠΎΠ²Π΅Ρ€ΡΠ΅ΠΌ, Π½Π°ΠΉΠ΄Π΅Π½Ρ‹ Π»ΠΈ Ρ€ΡƒΠΊΠΈ
if results.multi_hand_landmarks:
    for handLms in results.multi_hand_landmarks:
        # ΠŸΡ€ΠΎΡ…ΠΎΠ΄ΠΈΠΌ ΠΏΠΎ Ρ‚ΠΎΡ‡ΠΊΠ°ΠΌ (landmarks)
        for id, lm in enumerate(handLms.landmark):
            h, w, c = img.shape  # Высота, ΡˆΠΈΡ€ΠΈΠ½Π°, ΠΊΠ°Π½Π°Π»Ρ‹ изобраТСния
            cx, cy = int(lm.x * w), int(lm.y * h)  # ΠšΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ Ρ‚ΠΎΡ‡ΠΊΠΈ

            # ΠžΡ‚ΠΌΠ΅Ρ‡Π°Π΅ΠΌ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½Π½Ρ‹Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ
            if id == 0:  # ОснованиС ладони
                cv2.circle(img, (cx, cy), 10, (255, 0, 255), cv2.FILLED)

        # ΠžΡ‚ΠΎΠ±Ρ€Π°ΠΆΠ°Π΅ΠΌ соСдинСния ΠΌΠ΅ΠΆΠ΄Ρƒ Ρ‚ΠΎΡ‡ΠΊΠ°ΠΌΠΈ
        mp_draw.draw_landmarks(img, handLms, mp_hands.HAND_CONNECTIONS)

# РассчитываСм ΠΈ ΠΎΡ‚ΠΎΠ±Ρ€Π°ΠΆΠ°Π΅ΠΌ FPS
cTime = time.time()
fps = 1 / max((cTime - pTime), 1e-6)  # Π—Π°Ρ‰ΠΈΡ‚Π° ΠΎΡ‚ дСлСния Π½Π° 0
pTime = cTime

cv2.putText(img, f'FPS: {int(fps)}', (10, 70),
            cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)

# ΠŸΠΎΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅
cv2.imshow("Image", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

ОсвобоТдаСм рСсурсы

hands.close() # Π—Π°ΠΊΡ€Ρ‹Π²Π°Π΅ΠΌ MediaPipe Hands
cap.release()
cv2.destroyAllWindows()

midnight lavaBOT
#

@lament spear

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

midnight lavaBOT
#

@lament spear

Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.