hyeongrokheo / mediapipe-public

mediapipe 강의 기본소스
0 stars 1 forks source link

code #1

Open KEHyeon opened 1 year ago

KEHyeon commented 1 year ago
import cv2
import mediapipe as mp
import math
FRAME_DELAY = 100

mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands

mp_fingers = mp_hands.HandLandmark

def isFuck(hand_landmarks):
    # print(hand_landmarks[6].y, hand_landmarks[7].y)
    # print(hand_landmarks[14].y, hand_landmarks[15].y)
    # print(hand_landmarks[18].y, hand_landmarks[19].y)
    # print(hand_landmarks[10].y, hand_landmarks[11].y)

    if(hand_landmarks[6].y < hand_landmarks[7].y and 
    hand_landmarks[14].y < hand_landmarks[15].y and 
    hand_landmarks[18].y < hand_landmarks[19].y and
    hand_landmarks[10].y > hand_landmarks[11].y) :
        return True
    return False

def run():
    cap = cv2.VideoCapture(0)

    hands = mp_hands.Hands(
        model_complexity=0,
        min_detection_confidence=0.5,
        min_tracking_confidence=0.5
    )
    while cap.isOpened():
        success, image = cap.read()
        if not success:
            print('Ignoring empty camera frame.')
            continue
        image = cv2.flip(image, 1)

        image.flags.writeable = False
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        width, height, _ = image.shape #1080 1920
        results = hands.process(image)

        if results.multi_hand_landmarks:
            for hand_landmarks in results.multi_hand_landmarks:
                if(isFuck(hand_landmarks.landmark)) :
                    for i in hand_landmarks.landmark:
                        image = mosaic(image, i.x*1920, i.y*1080)
                    cv2.putText(
                        image,
                        text="yes",
                        org=(100,100),
                        fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                        fontScale=1,
                        color=(255,255,255),
                        thickness=2
                        )
                mp_drawing.draw_landmarks(
                    image, 
                    hand_landmarks,
                    mp_hands.HAND_CONNECTIONS,
                    mp_drawing_styles.get_default_hand_landmarks_style(),
                    mp_drawing_styles.get_default_hand_connections_style()
                )

        image.flags.writeable = True
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        cv2.imshow('MediaPipe Hands', image)
        cv2.waitKey(FRAME_DELAY)
    cap.release()

def mosaic(image, x, y):
    x = int(x);
    y = int(y);
    print(x, y)
    # if s + 100 > 1920:
        # s = 1080
    x = min(x, 979)
    # if e + 100 > 1080:
        # e = 1920
    y = min(y, 1819)
    target_image = image[x : x + 100, y : y + 100]

    target_width, target_height, _ = target_image.shape

    target_image = cv2.resize(target_image, (0, 0), fx=0.08, fy=0.08)
    target_image = cv2.resize(target_image, (target_height, target_width), fx=0.08, fy=0.08, interpolation=cv2.INTER_AREA)

    image[x : x + 100, y : y + 100] = target_image
    return image
run()