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()
β OutPut / μ’-μ° λ, μ½ κ΅¬λΆ
→ Face Detection ν, κ· - λ - μ½ - μ μ ꡬλΆνκΈ° μν΄ λΉ¨κ°μ νμ