90 lines
3.3 KiB
Python
90 lines
3.3 KiB
Python
import cv2
|
|
import mediapipe as mp
|
|
import os
|
|
import numpy as np
|
|
import json
|
|
|
|
# Create the assets directory if it doesn't exist
|
|
if not os.path.exists('assets'):
|
|
os.makedirs('assets')
|
|
|
|
# Load the pose detection model
|
|
with mp.solutions.pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
|
|
|
|
cap = cv2.VideoCapture(0)
|
|
|
|
# read the dataset.json file
|
|
dataset_file = 'dataset.json'
|
|
if os.path.exists(dataset_file):
|
|
with open(dataset_file, 'r') as json_file:
|
|
data = json.load(json_file)
|
|
else:
|
|
data = []
|
|
|
|
l = len(data)
|
|
|
|
|
|
# Load saved pose images and store their landmarks and filenames
|
|
saved_landmarks = []
|
|
saved_filenames = []
|
|
for i in range(l):
|
|
filename = f'assets/{data[i]["nama"]}.jpg'
|
|
image = cv2.imread(filename)
|
|
if image is not None:
|
|
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
|
results = pose.process(image_rgb)
|
|
if results.pose_landmarks:
|
|
landmarks = np.array([[landmark.x, landmark.y, landmark.z] for landmark in results.pose_landmarks.landmark])
|
|
saved_landmarks.append(landmarks)
|
|
saved_filenames.append(filename)
|
|
|
|
while True:
|
|
ret, frame = cap.read()
|
|
if not ret:
|
|
break
|
|
# Convert the image to RGB
|
|
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
# Process the image and find the landmarks
|
|
results = pose.process(image)
|
|
# Draw the landmarks on the image
|
|
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
|
mp.solutions.drawing_utils.draw_landmarks(image, results.pose_landmarks, mp.solutions.pose.POSE_CONNECTIONS)
|
|
|
|
# Compare the pose with saved pose dataset images
|
|
highest_similarity = -1
|
|
most_similar_filename = ""
|
|
if results.pose_landmarks:
|
|
detected_landmarks = np.array([[landmark.x, landmark.y, landmark.z] for landmark in results.pose_landmarks.landmark])
|
|
|
|
for i, saved_landmark in enumerate(saved_landmarks):
|
|
# Calculate cosine similarity between the landmarks
|
|
similarity = np.dot(detected_landmarks.flatten(), saved_landmark.flatten()) / (np.linalg.norm(detected_landmarks) * np.linalg.norm(saved_landmark))
|
|
|
|
if similarity > highest_similarity:
|
|
highest_similarity = similarity
|
|
most_similar_filename = saved_filenames[i]
|
|
|
|
# Calculate similarity percentage
|
|
similarity_percentage = round(highest_similarity * 100, 2)
|
|
|
|
# Display the most similar filename and similarity percentage if similarity is above 96%
|
|
print (similarity_percentage)
|
|
print (most_similar_filename)
|
|
if similarity_percentage > 93:
|
|
text = f"Most Similar: {most_similar_filename} - Similarity: {similarity_percentage}%"
|
|
cv2.putText(image, text, (10, image.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
|
|
|
# Display the image
|
|
cv2.imshow('Pose Detection', image)
|
|
|
|
|
|
|
|
|
|
# Exit the loop if the 'ESC' key is pressed
|
|
if cv2.waitKey(1) == 27 or cv2.waitKey(1) == ord('q'):
|
|
break
|
|
|
|
# Release the capture and destroy the window
|
|
cap.release()
|
|
cv2.destroyAllWindows()
|