Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hzzone committed Nov 17, 2018
1 parent 6e71411 commit 0f82a3c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
15 changes: 9 additions & 6 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,28 @@
body_estimation = Body('model/body_pose_model.pth')
hand_estimation = Hand('model/hand_pose_model.pth')

# test_image = 'images/demo.jpg'
test_image = '/Users/hzzone/Desktop/1.jpeg'
test_image = 'images/demo.jpg'
# test_image = '/Users/hzzone/Desktop/1.jpeg'
oriImg = cv2.imread(test_image) # B,G,R order
candidate, subset = body_estimation(oriImg)
# canvas = util.draw_bodypose(oriImg, candidate, subset)
canvas = copy.deepcopy(oriImg)
# detect hand
hands_list = util.handDetect(candidate, subset, oriImg)

all_hand_peaks = []
for x, y, w, is_left in hands_list:
cv2.rectangle(canvas, (x, y), (x+w, y+w), (0, 255, 0), 2, lineType=cv2.LINE_AA)
cv2.putText(canvas, 'left' if is_left else 'right', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# cv2.rectangle(canvas, (x, y), (x+w, y+w), (0, 255, 0), 2, lineType=cv2.LINE_AA)
# cv2.putText(canvas, 'left' if is_left else 'right', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

if is_left:
# plt.imshow(oriImg[y:y+w, x:x+w, :][:, :, [2, 1, 0]])
# plt.show()
peaks = hand_estimation(oriImg[y:y+w, x:x+w, :], x, y)
canvas = util.draw_handpose(canvas, peaks)
all_hand_peaks.append(peaks)

canvas = util.draw_handpose(canvas, all_hand_peaks)

plt.imshow(canvas[:, :, [2, 1, 0]])
plt.show()
# cv2.imwrite('t.jpg', canvas)
cv2.imwrite('t.jpg', canvas)
2 changes: 1 addition & 1 deletion python/hand.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __call__(self, oriImg, initial_x=0, initial_y=0):
hand_estimation = Hand('../model/hand_pose_model.pth')

# test_image = '../images/hand.jpg'
test_image = '/Users/hzzone/Desktop/1.png'
test_image = '../images/hand.jpg'
oriImg = cv2.imread(test_image) # B,G,R order
peaks = hand_estimation(oriImg)
canvas = util.draw_handpose(oriImg, peaks, True)
Expand Down
16 changes: 16 additions & 0 deletions python/hand_model_outputsize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from model import handpose_model
import torch
from tqdm import tqdm
import json

model = handpose_model()

size = {}
for i in tqdm(range(10, 1000)):
data = torch.randn(1, 3, i, i)
if torch.cuda.is_available():
data = data.cuda()
size[i] = model(data).size(2)

with open('hand_model_output_size.json') as f:
json.dump(size, f)
50 changes: 45 additions & 5 deletions python/util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import numpy as np
import math
import cv2
import colorsys
import matplotlib
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import numpy as np
import matplotlib.pyplot as plt
import cv2


def padRightDownCorner(img, stride, padValue):
Expand Down Expand Up @@ -70,17 +74,53 @@ def draw_bodypose(canvas, candidate, subset):
# plt.imshow(canvas[:, :, [2, 1, 0]])
return canvas

def draw_handpose(canvas, peaks, show_number=False):
def draw_handpose(canvas, all_hand_peaks, show_number=False):
edges = [[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]]
fig = Figure(figsize=plt.figaspect(canvas))

fig.subplots_adjust(0, 0, 1, 1)
fig.subplots_adjust(bottom=0, top=1, left=0, right=1)
bg = FigureCanvas(fig)
ax = fig.subplots()
ax.axis('off')
ax.imshow(canvas)

width, height = ax.figure.get_size_inches() * ax.figure.get_dpi()

for peaks in all_hand_peaks:
for ie, e in enumerate(edges):
if np.sum(np.all(peaks[e], axis=1)==0)==0:
x1, y1 = peaks[e[0]]
x2, y2 = peaks[e[1]]
ax.plot([x1, x2], [y1, y2], color=matplotlib.colors.hsv_to_rgb([ie/float(len(edges)), 1.0, 1.0]))

for i, keyponit in enumerate(peaks):
x, y = keyponit
ax.plot(x, y, 'r.')
if show_number:
ax.text(x, y, str(i))
bg.draw()
canvas = np.fromstring(bg.tostring_rgb(), dtype='uint8').reshape(int(height), int(width), 3)
return canvas

# image drawed by opencv is not good.
def draw_handpose_by_opencv(canvas, peaks, show_number=False):
edges = [[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]]
# cv2.rectangle(canvas, (x, y), (x+w, y+w), (0, 255, 0), 2, lineType=cv2.LINE_AA)
# cv2.putText(canvas, 'left' if is_left else 'right', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
for ie, e in enumerate(edges):
if np.sum(np.all(peaks[e], axis=1)==0)==0:
cv2.line(canvas, tuple(peaks[e[0]]), tuple(peaks[e[1]]), matplotlib.colors.hsv_to_rgb([ie/float(len(edges)), 1.0, 1.0])*255, thickness=2)
x1, y1 = peaks[e[0]]
x2, y2 = peaks[e[1]]
cv2.line(canvas, (x1, y1), (x2, y2), matplotlib.colors.hsv_to_rgb([ie/float(len(edges)), 1.0, 1.0])*255, thickness=2)

for i, keyponit in enumerate(peaks):
cv2.circle(canvas, tuple(keyponit), 2, (0, 0, 255), thickness=-1)
x, y = keyponit
cv2.circle(canvas, (x, y), 4, (0, 0, 255), thickness=-1)
if show_number:
cv2.putText(canvas, str(i), tuple(keyponit), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 0), lineType=cv2.LINE_AA)
cv2.putText(canvas, str(i), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 0), lineType=cv2.LINE_AA)
return canvas

# detect hand according to body pose keypoints
Expand Down

0 comments on commit 0f82a3c

Please sign in to comment.