|
import numpy as np |
|
import math |
|
import cv2 |
|
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 |
|
import copy |
|
import seaborn as sns |
|
|
|
def padRightDownCorner(img, stride, padValue): |
|
h = img.shape[0] |
|
w = img.shape[1] |
|
|
|
pad = 4 * [None] |
|
pad[0] = 0 |
|
pad[1] = 0 |
|
pad[2] = 0 if (h % stride == 0) else stride - (h % stride) |
|
pad[3] = 0 if (w % stride == 0) else stride - (w % stride) |
|
|
|
img_padded = img |
|
pad_up = np.tile(img_padded[0:1, :, :]*0 + padValue, (pad[0], 1, 1)) |
|
img_padded = np.concatenate((pad_up, img_padded), axis=0) |
|
pad_left = np.tile(img_padded[:, 0:1, :]*0 + padValue, (1, pad[1], 1)) |
|
img_padded = np.concatenate((pad_left, img_padded), axis=1) |
|
pad_down = np.tile(img_padded[-2:-1, :, :]*0 + padValue, (pad[2], 1, 1)) |
|
img_padded = np.concatenate((img_padded, pad_down), axis=0) |
|
pad_right = np.tile(img_padded[:, -2:-1, :]*0 + padValue, (1, pad[3], 1)) |
|
img_padded = np.concatenate((img_padded, pad_right), axis=1) |
|
|
|
return img_padded, pad |
|
|
|
|
|
def transfer(model, model_weights): |
|
transfered_model_weights = {} |
|
for weights_name in model.state_dict().keys(): |
|
if len(weights_name.split('.'))>4: |
|
transfered_model_weights[weights_name] = model_weights['.'.join( |
|
weights_name.split('.')[3:])] |
|
else: |
|
transfered_model_weights[weights_name] = model_weights['.'.join( |
|
weights_name.split('.')[1:])] |
|
return transfered_model_weights |
|
|
|
|
|
def draw_bodypose(canvas, candidate, subset, model_type='body25'): |
|
stickwidth = 4 |
|
if model_type == 'body25': |
|
limbSeq = [[1,0],[1,2],[2,3],[3,4],[1,5],[5,6],[6,7],[1,8],[8,9],[9,10],\ |
|
[10,11],[8,12],[12,13],[13,14],[0,15],[0,16],[15,17],[16,18],\ |
|
[11,24],[11,22],[14,21],[14,19],[22,23],[19,20]] |
|
njoint = 25 |
|
else: |
|
limbSeq = [[1, 2], [1, 5], [2, 3], [3, 4], [5, 6], [6, 7], [1, 8], [8, 9], \ |
|
[9, 10], [1, 11], [11, 12], [12, 13], [1, 0], [0, 14], [14, 16], \ |
|
[0, 15], [15, 17], [2, 16], [5, 17]] |
|
njoint = 18 |
|
|
|
|
|
|
|
|
|
|
|
colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0], \ |
|
[0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255], \ |
|
[170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85], [255,255,0], [255,255,85], [255,255,170],\ |
|
[255,255,255],[170,255,255],[85,255,255],[0,255,255]] |
|
|
|
for i in range(njoint): |
|
for n in range(len(subset)): |
|
index = int(subset[n][i]) |
|
if index == -1: |
|
continue |
|
x, y = candidate[index][0:2] |
|
cv2.circle(canvas, (int(x), int(y)), 4, colors[i], thickness=-1) |
|
for i in range(njoint-1): |
|
for n in range(len(subset)): |
|
index = subset[n][np.array(limbSeq[i])] |
|
if -1 in index: |
|
continue |
|
cur_canvas = canvas.copy() |
|
Y = candidate[index.astype(int), 0] |
|
X = candidate[index.astype(int), 1] |
|
mX = np.mean(X) |
|
mY = np.mean(Y) |
|
length = ((X[0] - X[1]) ** 2 + (Y[0] - Y[1]) ** 2) ** 0.5 |
|
angle = math.degrees(math.atan2(X[0] - X[1], Y[0] - Y[1])) |
|
|
|
|
|
polygon = cv2.ellipse2Poly((int(mY), int(mX)), (int(length / 2), stickwidth), int(angle), 0, 360, 1) |
|
|
|
cv2.fillConvexPoly(cur_canvas, polygon, colors[i]) |
|
canvas = cv2.addWeighted(canvas, 0.4, cur_canvas, 0.6, 0) |
|
|
|
|
|
return canvas |
|
|
|
|
|
def get_bodypose(candidate, subset, model_type='coco'): |
|
stickwidth = 4 |
|
if model_type == 'body25': |
|
limbSeq = [[1,0],[1,2],[2,3],[3,4],[1,5],[5,6],[6,7],[1,8],[8,9],[9,10],\ |
|
[10,11],[8,12],[12,13],[13,14],[0,15],[0,16],[15,17],[16,18],\ |
|
[11,24],[11,22],[14,21],[14,19],[22,23],[19,20]] |
|
njoint = 25 |
|
else: |
|
limbSeq = [[1, 2], [1, 5], [2, 3], [3, 4], [5, 6], [6, 7], [1, 8], [8, 9], \ |
|
[9, 10], [1, 11], [11, 12], [12, 13], [1, 0], [0, 14], [14, 16], \ |
|
[0, 15], [15, 17], [2, 16], [5, 17]] |
|
njoint = 18 |
|
|
|
|
|
|
|
|
|
|
|
colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0], \ |
|
[0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255], \ |
|
[170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85], [255,255,0], [255,255,85], [255,255,170],\ |
|
[255,255,255],[170,255,255],[85,255,255],[0,255,255]] |
|
|
|
x_y_circles=[] |
|
for i in range(njoint): |
|
for n in range(len(subset)): |
|
index = int(subset[n][i]) |
|
if index == -1: |
|
continue |
|
x, y = candidate[index][0:2] |
|
x_y_circles.append((x, y)) |
|
|
|
|
|
x_y_sticks=[] |
|
for i in range(njoint-1): |
|
for n in range(len(subset)): |
|
index = subset[n][np.array(limbSeq[i])] |
|
if -1 in index: |
|
continue |
|
|
|
Y = candidate[index.astype(int), 0] |
|
X = candidate[index.astype(int), 1] |
|
mX = np.mean(X) |
|
mY = np.mean(Y) |
|
length = ((X[0] - X[1]) ** 2 + (Y[0] - Y[1]) ** 2) ** 0.5 |
|
angle = math.degrees(math.atan2(X[0] - X[1], Y[0] - Y[1])) |
|
x_y_sticks.append((mY, mX,angle,length)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
return (x_y_circles,x_y_sticks,) |
|
|
|
|
|
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 |
|
|
|
def get_handpose(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]] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export_edges=[[],[]] |
|
export_peaks=[[],[]] |
|
for idx,peaks in enumerate(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]] |
|
export_edges[idx].append((ie,(x1, y1),(x2, y2))) |
|
|
|
|
|
for i, keyponit in enumerate(peaks): |
|
x, y = keyponit |
|
|
|
|
|
|
|
|
|
export_peaks[idx].append((x,y,str(i))) |
|
|
|
|
|
return (export_edges,export_peaks) |
|
|
|
|
|
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]] |
|
|
|
|
|
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]] |
|
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): |
|
x, y = keyponit |
|
cv2.circle(canvas, (x, y), 4, (0, 0, 255), thickness=-1) |
|
if show_number: |
|
cv2.putText(canvas, str(i), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 0), lineType=cv2.LINE_AA) |
|
return canvas |
|
|
|
|
|
|
|
def handDetect(candidate, subset, oriImg): |
|
|
|
|
|
ratioWristElbow = 0.33 |
|
detect_result = [] |
|
|
|
image_height, image_width = oriImg.shape[0:2] |
|
|
|
for person in subset.astype(int): |
|
|
|
has_left = np.sum(person[[5, 6, 7]] == -1) == 0 |
|
has_right = np.sum(person[[2, 3, 4]] == -1) == 0 |
|
if not (has_left or has_right): |
|
continue |
|
hands = [] |
|
|
|
if has_left: |
|
left_shoulder_index, left_elbow_index, left_wrist_index = person[[5, 6, 7]] |
|
x1, y1 = candidate[left_shoulder_index][:2] |
|
x2, y2 = candidate[left_elbow_index][:2] |
|
x3, y3 = candidate[left_wrist_index][:2] |
|
hands.append([x1, y1, x2, y2, x3, y3, True]) |
|
|
|
if has_right: |
|
right_shoulder_index, right_elbow_index, right_wrist_index = person[[2, 3, 4]] |
|
x1, y1 = candidate[right_shoulder_index][:2] |
|
x2, y2 = candidate[right_elbow_index][:2] |
|
x3, y3 = candidate[right_wrist_index][:2] |
|
hands.append([x1, y1, x2, y2, x3, y3, False]) |
|
|
|
for x1, y1, x2, y2, x3, y3, is_left in hands: |
|
|
|
|
|
|
|
|
|
|
|
|
|
x = x3 + ratioWristElbow * (x3 - x2) |
|
y = y3 + ratioWristElbow * (y3 - y2) |
|
distanceWristElbow = math.sqrt((x3 - x2) ** 2 + (y3 - y2) ** 2) |
|
distanceElbowShoulder = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) |
|
width = 1.5 * max(distanceWristElbow, 0.9 * distanceElbowShoulder) |
|
|
|
|
|
|
|
x -= width / 2 |
|
y -= width / 2 |
|
|
|
if x < 0: x = 0 |
|
if y < 0: y = 0 |
|
width1 = width |
|
width2 = width |
|
if x + width > image_width: width1 = image_width - x |
|
if y + width > image_height: width2 = image_height - y |
|
width = min(width1, width2) |
|
|
|
if width >= 20: |
|
detect_result.append([int(x), int(y), int(width), is_left]) |
|
|
|
''' |
|
return value: [[x, y, w, True if left hand else False]]. |
|
width=height since the network require squared input. |
|
x, y is the coordinate of top left |
|
''' |
|
return detect_result |
|
|
|
def drawStickmodel(oriImg,x_ytupple,x_y_sticks,export_edges,export_peaks): |
|
canvas = copy.deepcopy(oriImg) |
|
|
|
|
|
colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0], |
|
[0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255], |
|
[170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85], [255,255,0], [255,255,85], [255,255,170], |
|
[255,255,255],[170,255,255],[85,255,255],[0,255,255]] |
|
stickwidth=4 |
|
|
|
for idx,(mX,mY,angle,length) in enumerate(x_y_sticks): |
|
cur_canvas = canvas.copy() |
|
|
|
polygon = cv2.ellipse2Poly((int(mX), int(mY)), (int(length / 2), stickwidth), int(angle), 0, 360, 1) |
|
cv2.fillConvexPoly(cur_canvas, polygon, colors[idx]) |
|
canvas = cv2.addWeighted(canvas, 0.4, cur_canvas, 0.6, 0) |
|
|
|
|
|
|
|
for idx,(x,y) in enumerate(x_ytupple): |
|
cv2.circle(canvas, (int(x), int(y)), 4, colors[idx], thickness=-1) |
|
|
|
|
|
|
|
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) |
|
|
|
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]] |
|
|
|
for both_hand_edges in export_edges: |
|
for (ie,(x1, y1),(x2, y2)) in both_hand_edges: |
|
|
|
ax.plot([x1, x2], [y1, y2], color=matplotlib.colors.hsv_to_rgb([ie/float(len(edges)), 1.0, 1.0])) |
|
|
|
width, height = ax.figure.get_size_inches() * ax.figure.get_dpi() |
|
|
|
for both_hand_peaks in export_peaks: |
|
for (x,y,text) in both_hand_peaks: |
|
|
|
ax.plot(x, y, 'r.') |
|
|
|
|
|
|
|
|
|
bg.draw() |
|
|
|
canvas = np.fromstring(bg.tostring_rgb(), dtype='uint8').reshape(int(height), int(width), 3) |
|
|
|
|
|
|
|
|
|
|
|
return cv2.resize(canvas,(math.ceil(width),math.ceil(height))) |
|
|
|
def draw_bar_plot_below_image(image, predictions, title, origImg): |
|
""" |
|
Draws a bar plot of predictions below an image using OpenCV and Matplotlib. |
|
|
|
Args: |
|
image (numpy.ndarray): The image to display. |
|
predictions (numpy.ndarray): Array containing prediction probabilities. |
|
""" |
|
|
|
|
|
|
|
fig, ax = plt.subplots(figsize=(origImg.shape[1]/100,origImg.shape[0]/200), dpi=100) |
|
plt.title(title) |
|
|
|
labels = list(predictions.keys()) |
|
probabilities = list(predictions.values()) |
|
|
|
|
|
sns.barplot(x=labels, y=probabilities,ax=ax) |
|
plt.close(fig) |
|
fig.canvas.draw() |
|
|
|
plot_image = np.array(fig.canvas.renderer.buffer_rgba())[:, :, :3] |
|
|
|
|
|
|
|
|
|
|
|
combined_image = np.vstack((image, cv2.resize(plot_image,(image.shape[1],plot_image.shape[0])))) |
|
|
|
return combined_image |
|
|
|
def add_padding_to_bottom(image, pad_value, pad_height): |
|
""" |
|
Adds padding to the bottom of an image with a specified value. |
|
|
|
Args: |
|
image (numpy.ndarray): The input image. |
|
pad_value (tuple or int): The color value to fill the padding area. |
|
pad_height (int): The height of the padding to add at the bottom. |
|
|
|
Returns: |
|
numpy.ndarray: The image with padding added. |
|
""" |
|
|
|
|
|
height, width, channels = image.shape |
|
padding=np.zeros((pad_height, width, channels), dtype=image.dtype) |
|
padding[:,:,:]=pad_value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return np.vstack((image, padding)) |
|
|
|
def crop_to_drawing(image): |
|
""" |
|
Crops an image to the tight bounding rectangle of non-zero pixels. |
|
|
|
Args: |
|
image: A NumPy array representing the image. |
|
|
|
Returns: |
|
A cropped image (NumPy array) containing only the drawing area. |
|
""" |
|
image=np.transpose(image, (2, 0, 1)) |
|
united_x,united_h=0,0 |
|
for channel in np.arange(image.shape[0]): |
|
x, y, w, h = cv2.boundingRect(image[channel]) |
|
if x>united_x: |
|
united_x=x |
|
|
|
if h>united_h: |
|
united_h=h |
|
|
|
for channel in np.arange(image.shape[0]): |
|
|
|
image[channel] = image[channel][y:y+united_h, x:x+united_x] |
|
return image.transpose(image, (1,2,0)) |
|
|
|
|
|
def npmax(array): |
|
arrayindex = array.argmax(1) |
|
arrayvalue = array.max(1) |
|
i = arrayvalue.argmax() |
|
j = arrayindex[i] |
|
return i, j |
|
|