קורס יסודות בינה מלאכותית – POSE – RB23-08
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
import cv2 import numpy as np from ultralytics import YOLO import pandas as pd # Initialize the YOLO model model = YOLO('yolov8m-pose.pt') # Ensure the model path is correct and the model is available # Path to the input image image_path = 'D:\\temp3\\man-hands-side.png' # Load the image image = cv2.imread(image_path) # Perform inference results = model(image, show=True, device='cpu') # Keypoint names keypoint_names = [ 'Nose', 'Left eye', 'Right eye', 'Left ear', 'Right ear', 'Left shoulder', 'Right shoulder', 'Left elbow', 'Right elbow', 'Left wrist', 'Right wrist', 'Left hip', 'Right hip', 'Left knee', 'Right knee', 'Left ankle', 'Right ankle' ] def calculate_length_angle(p1, p2): """Calculate the length and angle between two points.""" length = np.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2) angle = np.arctan2(p2[1] - p1[1], p2[0] - p1[0]) * 180 / np.pi return length, angle def calculate_angle(p1, p2, p3): """Calculate the angle between three points.""" v1 = np.array([p1[0] - p2[0], p1[1] - p2[1]]) v2 = np.array([p3[0] - p2[0], p3[1] - p2[1]]) angle = np.arctan2(v2[1], v2[0]) - np.arctan2(v1[1], v1[0]) angle = np.degrees(angle) if angle < 0: angle += 360 return angle # Extract pose information and annotate image pose_index = 0 for result in results: for i in range(len(result.keypoints.xy)): pose_index += 1 keypoints = result.keypoints.xy[i].cpu().numpy() confidence = result.keypoints.conf[i].cpu().numpy() if result.keypoints.conf is not None else [None] * len(keypoints) # Annotate pose index on the image center_x = int(keypoints[5][0]) # Use the left shoulder as reference point center_y = int(keypoints[5][1]) if 0 <= center_x < image.shape[1] and 0 <= center_y < image.shape[0]: cv2.putText(image, f"Pose {pose_index}", (center_x, center_y - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) # Draw keypoints on the image for j, keypoint in enumerate(keypoints): x, y = int(keypoint[0]), int(keypoint[1]) if 0 <= x < image.shape[1] and 0 <= y < image.shape[0]: cv2.circle(image, (x, y), 5, (0, 0, 255), -1) # Change text color to black and use a smaller font cv2.putText(image, f"{j+1}: {keypoint_names[j]}", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 0), 1) # Print pose information in a table print(f"Pose Index: {pose_index}") table_data = [] for j, keypoint in enumerate(keypoint_names): coord = keypoints[j] conf = confidence[j] if j < len(keypoint_names) - 1: next_coord = keypoints[j + 1] length, angle = calculate_length_angle(coord, next_coord) else: length, angle = None, None table_data.append([keypoint, f"{coord[0]:.2f}", f"{coord[1]:.2f}", f"{conf:.2f}" if conf is not None else "N/A", length, angle]) # Create a pandas DataFrame for better visualization df = pd.DataFrame(table_data, columns=['Keypoint', 'X', 'Y', 'Confidence', 'Length', 'Angle']) print(df) print() # Calculate the angle between keypoints 8, 6, and 12 p8 = keypoints[8] p6 = keypoints[6] p12 = keypoints[12] angle = calculate_angle(p8, p6, p12) print(f"Angle between keypoints 8, 6, and 12: {angle:.2f} degrees") p8 = keypoints[11] p6 = keypoints[5] p12 = keypoints[7] angle = calculate_angle(p8, p6, p12) print(f"Angle between keypoints 7, 5, and 11: {angle:.2f} degrees") # Display the annotated image cv2.imshow('Annotated Image', image) cv2.waitKey(0) cv2.destroyAllWindows() # Save the annotated image if needed output_path = 'D:\\temp3\\man-hands-side-annotated.png' cv2.imwrite(output_path, image) |