Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use OpenCV to detect the posture of athletes

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how to use OpenCV to detect the posture of athletes. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.

Nowadays, the popularity of sports is becoming more and more popular. Similarly, the risk of exercising in the wrong way is increasing. Sometimes it can cause serious injury. Considering these reasons, this paper puts forward a solution to analyze the joint movement of athletes to help athletes correct their posture.

Human posture estimation is an important problem in the field of computer vision. Its algorithm helps to locate wrists, ankles, knees and so on. This is done to provide a personalized sports training experience using the concepts of deep learning and convolution neural networks. Especially for sports activities, the quality of training depends to a large extent on the correctness of human posture in images or video sequences.

Detect the posture of an athlete from an image or video sequence

Data set

It is also necessary to select the dataset correctly to have an appropriate impact on the results. In this pose detection, the model is pre-trained on two different data sets, namely the COCO key point data set and the MPII human pose data set.

1. The COCO:COCO key data set is a multi-person 2D pose estimation data set that contains images collected from Flickr. So far, COCO is the largest 2D pose estimation data set and is regarded as the benchmark for testing 2D pose estimation algorithms. There are 18 classifications of COCO model. COCO output format: nose-0, neck-1, right shoulder-2, right elbow-3, right wrist-4, left shoulder-5, left elbow-6, left wrist-7, right hip-8, right knee-9, right ankle-10, left hip-11, left knee-12, left ankle-13, right eye-14, left eye-15, right ear-16, left ear-17, background-18

2. The MPII:MPII human posture data set is a multi-person 2D pose estimation data set, which contains nearly 500 different human activities collected from Youtube videos. MPII is the first dataset to cover a wide range of poses and the first dataset to launch a 2D pose estimation challenge in 2014. The output of MPII model is 15 points. MPII output format: head-0, neck-1, right shoulder-2, right elbow-3, right wrist-4, left shoulder-5, left elbow-6, left wrist-7, right hip-8, right knee-9, right ankle-10, left hip-11, left knee-12, left ankle-13, chest-14, background-15

These points are generated when the data set is processed and fully trained by the convolution neural network (CNN).

Step 1: requirements collection (model weight) and load network

A trained model needs to be loaded into OpenCV. These models are trained on the framework of Caffe deep learning. The Caffe model contains two files, the .prototxt file and the .caffemodel file.

The .prototxt file specifies the architecture of the neural network.

The .caffemodel file stores the weight of the trained model.

Then we load these two files into the network.

# Specify the paths for the 2 filesprotoFile = "pose/mpi/pose_deploy_linevec_faster_4_stages.prototxt" weightsFile = "pose/mpi/pose_iter_160000.caffemodel" # Read the network into Memorynet = cv2.dnn.readNetFromCaffe (protoFile, weightsFile) step 2: read the image and prepare for input to the network

First, we need to use the blobFromImage function to convert the image from OpenCV format to Caffe blob format so that it can be input into the network. These parameters are provided in the blobFromImage function. Because both OpenCV and Caffe use the BGR format, there is no need to exchange R and B channels.

# Read imageframe = cv2.imread ("image.jpg") # Specify the input image dimensionsinWidth = 368inHeight = 364th Prepare the frame to be fed to the networkinpBlob = cv2.dnn.blobFromImage (frame, 1.0 / 255,( inWidth, inHeight), (0,0,0), swapRB=False, crop=False) # Set the prepared object as the input blob of the networknet.setInput (inpBlob) step 3: make a prediction and analyze the key points

Once the image is passed to the model, it can be predicted using the forward method of the DNN class in OpenCV, which is passed forward through the network, which is just another way to say that it is making a prediction.

Output = net.forward ()

Output to 4D matrix:

The first dimension is the picture ID (if you send multiple pictures to the network).

The second dimension indicates the index of the key point. The model generates a confidence map (the probability distribution on the image that represents the confidence of the joint position at each pixel) and the affinity map of all connected parts. For the COCO model, it consists of 57 parts-18 key point confidence map + 1 background + 19 * 2 partial affinity map. Again, for MPI, it produces 44 points. We will use only the first few points corresponding to the key points.

The third dimension is the height of the output graph.

The fourth dimension is the width of the output graph.

Then we check to see if each key point exists in the image. We get the location of the key point by finding the maximum value of the confidence map of the key point. We also use thresholds to reduce error detection.

Confidence map

Once a key point is detected, we draw it on the image.

H = out.shape [2] W = out.shape [3] # Empty list to store the detected keypointspoints = [] for i in range (len ()): # confidence map of corresponding body's part. ProbMap = output [0, I,:,:] # Find global maxima of the probMap. MinVal, prob, minLoc, point = cv2.minMaxLoc (probMap) # Scale the point to fit on the original image x = (frameWidth * point [0]) / W y = (frameHeight * point [1]) / H if prob & gt Threshold: cv2.circle (frame, (int (x), int (y)), 15, (0255,255), thickness=-1, lineType=cv.FILLED) cv2.putText (frame, "{}" .format (I), (int (x), int (y)), cv2.FONT_HERSHEY_SIMPLEX, 1.4,( 0,0,255), 3 LineType=cv2.LINE_AA) # Add the point to the list if the probability is greater than the threshold points.append ((int (x), int (y)) else: points.append (None) cv2.imshow ("Output-Keypoints", frame) cv2.waitKey (0) cv2.destroyAllWindows () step 4: draw the skeleton

Since we have drawn the key points, we now only need to connect two pairs to draw the skeleton.

For pair in POSE_PAIRS: partA = pair [0] partB = pair [1] if points [partA] and points [partB]: cv2.line (frameCopy, points [partA], points [partB], (0,255,0), 3) result

The output shown above shows us the exact posture of the athlete at a particular time.

After reading the above, do you have any further understanding of how to use OpenCV to detect the posture of athletes? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report