๐ ๊ฐ๋ฐ ํ๊ฒฝ : 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()

โ OutPut / ์ข-์ฐ ๋, ์ฝ ๊ตฌ๋ถ

→ Face Detection ํ, ๊ท - ๋ - ์ฝ - ์ ์ ๊ตฌ๋ถํ๊ธฐ ์ํด ๋นจ๊ฐ์ ํ์
'MLDL_์ ๋ฆฌ > Sample' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [DL] - Object Detection / MMDetection Package (0) | 2022.03.07 |
|---|---|
| [DL] - MediaPipe / Video pose Detection (0) | 2022.03.06 |
| [DL] - ์ ํ๋ ์ธก์ (0) | 2022.03.01 |
| DL - Confusion Matrix (0) | 2022.02.05 |
| DL - ์ค์ฐจ์ญ์ ํ๋ฒ์ ์ด์ฉํ ํ๋ฅ ๋ถํฌ (0) | 2022.02.05 |