Tuesday, June 8, 2021

opencv 60 openpose heatmap


heat map of neck

nose

right hip

right knee

right big toe
#project structure
pose_multi.py
googleNet
    body_25
        pose_deploy.prototxt
        pose_iter_584000.caffemodel

#pose_multi.py
import cv2
import numpy as np

protoFile = "googleNet/body_25/pose_deploy.prototxt"
weightsFile = "googleNet/body_25/pose_iter_584000.caffemodel"
net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)

image1 = cv2.imread("assets/people_group.jpg")
frameHeight, frameWidth, channel = image1.shape

# Fix the input Height and get the width according to the Aspect Ratio
inHeight = 368
inWidth = int((inHeight / frameHeight) * frameWidth)

inpBlob = cv2.dnn.blobFromImage(image1, 1.0 / 255, (inWidth, inHeight),
                                (0, 0, 0), swapRB=False, crop=False)

net.setInput(inpBlob)
output = net.forward()

#probMap shows probility of each pixel being a part of nose
#i = 0, 0 represent nose in trained model, 1 represent neck...
"""
// Result for BODY_25 (25 body parts consisting of COCO + foot)
// const std::map<unsigned int, std::string> POSE_BODY_25_BODY_PARTS {
//     {0,  "Nose"},
//     {1,  "Neck"},
//     {2,  "RShoulder"},
//     {3,  "RElbow"},
//     {4,  "RWrist"},
//     {5,  "LShoulder"},
//     {6,  "LElbow"},
//     {7,  "LWrist"},
//     {8,  "MidHip"},
//     {9,  "RHip"},
//     {10, "RKnee"},
//     {11, "RAnkle"},
//     {12, "LHip"},
//     {13, "LKnee"},
//     {14, "LAnkle"},
//     {15, "REye"},
//     {16, "LEye"},
//     {17, "REar"},
//     {18, "LEar"},
//     {19, "LBigToe"},
//     {20, "LSmallToe"},
//     {21, "LHeel"},
//     {22, "RBigToe"},
//     {23, "RSmallToe"},
//     {24, "RHeel"},
//     {25, "Background"}
// };
"""

i = 22
probMap = output[0, i, :, :]
probMap = cv2.resize(probMap, (frameWidth, frameHeight))
probMap_RGB = cv2.cvtColor(probMap, cv2.COLOR_GRAY2RGB)

#probablity is between 0 and 1, multiply 255 to show heat map
probMap_RGB = np.multiply(probMap_RGB, 255).astype("uint8")
#print(probMap_RGB.shape, image1.shape)
#print(probMap_RGB, image1)

blend = cv2.addWeighted(image1.astype("uint8"), 0.5, probMap_RGB, 0.5, 0)

cv2.imshow("probMap", blend)
cv2.waitKey(0)

reference:

download model

No comments:

Post a Comment