From 93b809d65085460dc3a26b60d07f1f387efa146b Mon Sep 17 00:00:00 2001 From: vikas Date: Thu, 27 Sep 2018 22:46:58 +0530 Subject: [PATCH] added smart resizing for input blob --- HandPose/handPoseImage.cpp | 8 ++++++-- HandPose/handPoseImage.py | 12 +++++------- HandPose/handPoseVideo.cpp | 7 +++++-- HandPose/handPoseVideo.py | 13 ++++++++----- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/HandPose/handPoseImage.cpp b/HandPose/handPoseImage.cpp index 7b05bc7e0..680f6f421 100644 --- a/HandPose/handPoseImage.cpp +++ b/HandPose/handPoseImage.cpp @@ -34,8 +34,6 @@ int main(int argc, char **argv) imageFile = argv[1]; } - int inWidth = 368; - int inHeight = 368; float thresh = 0.01; Mat frame = imread(imageFile); @@ -43,6 +41,12 @@ int main(int argc, char **argv) int frameWidth = frame.cols; int frameHeight = frame.rows; + float aspect_ratio = frameWidth/(float)frameHeight; + int inHeight = 368; + int inWidth = (int(aspect_ratio*inHeight) * 8) / 8; + + cout << "inWidth = " << inWidth << " ; inHeight = " << inHeight << endl; + double t = (double) cv::getTickCount(); Net net = readNetFromCaffe(protoFile, weightsFile); diff --git a/HandPose/handPoseImage.py b/HandPose/handPoseImage.py index 3de3598bf..2d0620ac6 100644 --- a/HandPose/handPoseImage.py +++ b/HandPose/handPoseImage.py @@ -6,20 +6,21 @@ weightsFile = "hand/pose_iter_102000.caffemodel" nPoints = 22 POSE_PAIRS = [ [0,1],[1,2],[2,3],[3,4],[0,5],[5,6],[6,7],[7,8],[0,9],[9,10],[10,11],[11,12],[0,13],[13,14],[14,15],[15,16],[0,17],[17,18],[18,19],[19,20] ] - +net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile) frame = cv2.imread("hand.jpg") frameCopy = np.copy(frame) frameWidth = frame.shape[1] frameHeight = frame.shape[0] -threshold = 0.1 +aspect_ratio = frameWidth/frameHeight -net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile) +threshold = 0.1 t = time.time() # input image dimensions for the network -inWidth = 368 inHeight = 368 +inWidth = int(((aspect_ratio*inHeight)*8)//8) + inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop=False) @@ -28,9 +29,6 @@ output = net.forward() print("time taken by network : {:.3f}".format(time.time() - t)) -H = output.shape[2] -W = output.shape[3] - # Empty list to store the detected keypoints points = [] diff --git a/HandPose/handPoseVideo.cpp b/HandPose/handPoseVideo.cpp index aee5a435e..0cd3a0c5a 100644 --- a/HandPose/handPoseVideo.cpp +++ b/HandPose/handPoseVideo.cpp @@ -23,8 +23,6 @@ int nPoints = 22; int main(int argc, char **argv) { - int inWidth = 368; - int inHeight = 368; float thresh = 0.01; cv::VideoCapture cap(0); @@ -38,6 +36,11 @@ int main(int argc, char **argv) Mat frame, frameCopy; int frameWidth = cap.get(CAP_PROP_FRAME_WIDTH); int frameHeight = cap.get(CAP_PROP_FRAME_HEIGHT); + float aspect_ratio = frameWidth/(float)frameHeight; + int inHeight = 368; + int inWidth = (int(aspect_ratio*inHeight) * 8) / 8; + + cout << "inWidth = " << inWidth << " ; inHeight = " << inHeight << endl; VideoWriter video("Output-Skeleton.avi",VideoWriter::fourcc('M','J','P','G'), 10, Size(frameWidth,frameHeight)); diff --git a/HandPose/handPoseVideo.py b/HandPose/handPoseVideo.py index 7e6526b38..b34dda948 100644 --- a/HandPose/handPoseVideo.py +++ b/HandPose/handPoseVideo.py @@ -8,8 +8,6 @@ nPoints = 22 POSE_PAIRS = [ [0,1],[1,2],[2,3],[3,4],[0,5],[5,6],[6,7],[7,8],[0,9],[9,10],[10,11],[11,12],[0,13],[13,14],[14,15],[15,16],[0,17],[17,18],[18,19],[19,20] ] -inWidth = 368 -inHeight = 368 threshold = 0.1 @@ -17,6 +15,14 @@ cap = cv2.VideoCapture(input_source) hasFrame, frame = cap.read() +frameWidth = frame.shape[1] +frameHeight = frame.shape[0] + +aspect_ratio = frameWidth/frameHeight + +inHeight = 368 +inWidth = int(((aspect_ratio*inHeight)*8)//8) + vid_writer = cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 15, (frame.shape[1],frame.shape[0])) net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile) @@ -32,9 +38,6 @@ print("imread = {}".format(time.time() - t)) - frameWidth = frame.shape[1] - frameHeight = frame.shape[0] - inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop=False)