MLDL_์ •๋ฆฌ/Sample

[DL] - MediaPipe / Video Object Detection

KimTory 2022. 3. 6. 14:31

๐Ÿ‘‰ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ  : Jupyter Notebook, Python 3.9, MediaPipe, Opencv

โœ Source Code

import cv2
import mediapipe as mp


# face detection, ์ฐพ์€ detection ์˜์—ญ์— ์ž„์˜์˜ ํ‘œ์‹œ๋ฅผ ์œ„ํ•ด ๋ณ€์ˆ˜ ์„ ์–ธ
mp_face_detection = mp.solutions.face_detection
mp_drawing = mp.solutions.drawing_utils


# For webcam input:
cap = cv2.VideoCapture("c:\\face_video.mp4") # Source Video
# min_detection_confidence ๋Š” 0 ~ 1์˜ ๊ฐ’์œผ๋กœ ๊ฐ’์„ ์˜ฌ๋ฆด์ˆ˜๋ก ์ •๊ตํ•˜๊ฒŒ object detection ์ง„ํ–‰
# 0.5 ์„ค์ • ์‹œ, ์˜ค์ธ์‹ ํ•˜๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์–ด 70% ์ •๋„๋กœ ๋ณ€๊ฒฝ
with mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.7) as face_detection:
    while cap.isOpened():
        success, image = cap.read()
        if not success:
          break 

        image.flags.writeable = False
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        results = face_detection.process(image)

        # Draw the face detection annotations on the image.
        image.flags.writeable = True
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        
        if results.detections:
            # detection ์ •๋ณด๋Š” 6๊ฐ€์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ
          for detection in results.detections:
                mp_drawing.draw_detection(image, detection)
                print(detection) # score, x,y, width, height ๋“ฑ..์˜ ์ •๋ณด๋ฅผ detection ์ •๋ณด ์ถœ๋ ฅ
        
                # ํŠน์ • ์œ„์น˜ ๊ฐ€์ ธ ์˜ค๊ธฐ
                keypoints = detection.location_data.relative_keypoints
                right_eye = keypoints[0] # Right, ๋ˆˆ
                left_eye = keypoints[1] # Left, ๋ˆˆ
                nose_tip = keypoints[2] # ์ฝ” ๋
                
                h, w, _ = image.shape # src image ์„ธ๋กœ, ๊ฐ€๋กœ ํฌ๊ธฐ 
                
                right_eye = (int(right_eye.x * w), int(right_eye.y * h)) # ()๋กœ ๊ฐ์‹ธ์„œ tuple ํ˜•ํƒœ๋กœ ๋งŒ๋“ฆ
                left_eye = (int(left_eye.x * w), int(left_eye.y * h))
                nose_tip= (int(nose_tip.x * w), int(nose_tip.y * h))
                # ์ขŒ-์šฐ ๋ˆˆ์— ํŠน์ • ๋„ํ˜• ์‚ฝ์ž…
                cv2.circle(image, right_eye, 50, (255, 0, 0), 10, cv2.LINE_AA) # ๋ฐ˜์ง€๋ฆ„ 50, ์ƒ‰์€ BGR์œผ๋กœ blue
                cv2.circle(image, left_eye, 50, (0, 255, 0), 10, cv2.LINE_AA) # ๋ฐ˜์ง€๋ฆ„ 50, ์ƒ‰์€ BGR์œผ๋กœ green
                cv2.circle(image, nose_tip, 50, (0, 255, 255), 10, cv2.LINE_AA) # ๋ฐ˜์ง€๋ฆ„ 50, ์ƒ‰์€ BGR์œผ๋กœ yellow
        
        # Webcam์ด ์•„๋‹ˆ๋ฏ€๋กœ, Flip์œผ๋กœ ๋ฐ˜์ „ ์‹œํ‚ค์ง€ ์•Š์Œ
        # Size๋ฅผ ์›๋ณธ ๋Œ€๋น„, 0.5 ์ถ•์†Œ
        cv2.imshow('MediaPipe Face Detection', cv2.resize(image, None, fx=0.5, fy=0.5))
        if cv2.waitKey(1) == ord('q'): # "q" Key Click ์‹œ, ์ข…๋ฃŒ
          break
        
cap.release() # video ํ• ๋‹น ๋ณ€์ˆ˜ ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ
cv2.destroyAllWindows()

 

Detection Object ์ •๋ณด

 

โœ OutPut / ์ขŒ-์šฐ ๋ˆˆ, ์ฝ” ๊ตฌ๋ถ„

→ Face Detection ํ›„, ๊ท€ - ๋ˆˆ - ์ฝ” - ์ž…์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด ๋นจ๊ฐ„์  ํ‘œ์‹œ