Hardi Kurnianto
Published

Baby Positioning Detection System

Utilizing computer vision technology with several libraries from OpenCV to detect the baby's pose while in bed

BeginnerShowcase (no instructions)5 hours7
Baby Positioning Detection System

Things used in this project

Hardware components

Webcam, Logitech® HD Pro
Webcam, Logitech® HD Pro
×1

Software apps and online services

VS Code
Microsoft VS Code

Story

Read more

Code

DBP

Python
Code for this project, First you have to make a video of the baby with the name bayi.mp4
import cv2
import mediapipe as mp

INPUT_VIDEO = "bayi.mp4"
OUTPUT_VIDEO = "baby_position_output1.mp4"

mp_pose = mp.solutions.pose
mp_draw = mp.solutions.drawing_utils

pose = mp_pose.Pose(
    static_image_mode=False,
    min_detection_confidence=0.5,
    min_tracking_confidence=0.5
)

cap = cv2.VideoCapture(INPUT_VIDEO)

if not cap.isOpened():
    print("Video tidak ditemukan")
    exit()

fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

fourcc = cv2.VideoWriter_fourcc(*'mp4v')

out = cv2.VideoWriter(
    OUTPUT_VIDEO,
    fourcc,
    fps,
    (width, height)
)

while True:

    ret, frame = cap.read()

    if not ret:
        break

    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    results = pose.process(rgb)

    position = "Tengkurap"

    if results.pose_landmarks:

        lm = results.pose_landmarks.landmark

        nose = lm[mp_pose.PoseLandmark.NOSE]

        left_eye = lm[mp_pose.PoseLandmark.LEFT_EYE]
        right_eye = lm[mp_pose.PoseLandmark.RIGHT_EYE]

        left_shoulder = lm[mp_pose.PoseLandmark.LEFT_SHOULDER]
        right_shoulder = lm[mp_pose.PoseLandmark.RIGHT_SHOULDER]

        # Visibility wajah
        face_visibility = (
            nose.visibility 
            
        ) 

        shoulder_width = abs(
            left_shoulder.x - right_shoulder.x
        )

        # Klasifikasi sederhana
        if face_visibility > 0.999 and shoulder_width > 0.020:
            position = "Terlentang"

        elif face_visibility < 0:
            position = "TENGKURAP"

        else:
            position = "Miring"

        mp_draw.draw_landmarks(
            frame,
            results.pose_landmarks,
            mp_pose.POSE_CONNECTIONS
        )

    cv2.putText(
        frame,
        f"POSISI: {position} {face_visibility} {shoulder_width}",
        (20, 50),
        cv2.FONT_HERSHEY_SIMPLEX,
        1,
        (0, 255, 0),
        2
    )

    out.write(frame)

    cv2.imshow("Baby Position Detection", frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindows()

print("Selesai")
print("Output:", OUTPUT_VIDEO)

Credits

Hardi Kurnianto
19 projects • 18 followers
Master student at Intelligent Control and Systems Engineering Department

Comments