Untitled

mail@pastecode.io avatar
unknown
python
6 months ago
2.6 kB
6
Indexable
Never
import cv2
import mediapipe as mp
import time

cap = cv2.VideoCapture(0) #wybieranie kamery w laptoku

mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils

pTime = 0 #previous time
cTime = 0 #current time

while True:
    success, img = cap.read()
    img = cv2.flip(img, 1)
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    results = hands.process(imgRGB)
    # print(results.multi_hand_landmarks)

    if results.multi_hand_landmarks:
        for handLms in results.multi_hand_landmarks: # handLms = landmarks
            for id, lm in enumerate(handLms.landmark):
                # print(id, lm)
                h, w, c = img.shape # height, width, channels
                cx, cy = int(lm.x*w), int(lm.y*h)
                # print(id, cx, cy)
                if id == 20:
                    cv2.circle(img, (cx, cy), 10, (0,255,0), cv2.FILLED)
                    cv2.circle(img, (cx, cy), 6, (0,0,255), cv2.FILLED)
                if id == 16:
                    cv2.circle(img, (cx, cy), 10, (0,255,0), cv2.FILLED)
                    cv2.circle(img, (cx, cy), 6, (0,0,255), cv2.FILLED)
                if id == 12:
                    cv2.circle(img, (cx, cy), 10, (0,255,0), cv2.FILLED)
                    cv2.circle(img, (cx, cy), 6, (0,0,255), cv2.FILLED)
                if id == 8:
                    cv2.circle(img, (cx, cy), 10, (0,255,0), cv2.FILLED)
                    cv2.circle(img, (cx, cy), 6, (0,0,255), cv2.FILLED)
                if id == 4:
                    cv2.circle(img, (cx, cy), 10, (0,255,0), cv2.FILLED)
                    cv2.circle(img, (cx, cy), 6, (0,0,255), cv2.FILLED)

            mpDraw.draw_landmarks(
                img, handLms, mpHands.HAND_CONNECTIONS,
                                  landmark_drawing_spec = mpDraw.DrawingSpec(color=(0,0,255), thickness=2, circle_radius=2), # ustawienia dwudziestu kropek
                                  connection_drawing_spec = mpDraw.DrawingSpec(color=(0,255,0), thickness=2, circle_radius=2) # ustawienia linii
                                  )

    cTime = time.time()
    fps = 1/(cTime-pTime)
    pTime = cTime

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

    cv2.imshow("Image", img)
    if cv2.waitKey(1) == 27: #27 czyli przycisk escape
        break

cap.release()
cv2.destroyAllWindows()