CoolFace
Apppublic

otmanheddouch/house_design

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
util.py165 linesDownload Raw Back to openpose
1import math2import numpy as np3import matplotlib4import cv25 6 7def padRightDownCorner(img, stride, padValue):8    h = img.shape[0]9    w = img.shape[1]10 11    pad = 4 * [None]12    pad[0] = 0 # up13    pad[1] = 0 # left14    pad[2] = 0 if (h % stride == 0) else stride - (h % stride) # down15    pad[3] = 0 if (w % stride == 0) else stride - (w % stride) # right16 17    img_padded = img18    pad_up = np.tile(img_padded[0:1, :, :]*0 + padValue, (pad[0], 1, 1))19    img_padded = np.concatenate((pad_up, img_padded), axis=0)20    pad_left = np.tile(img_padded[:, 0:1, :]*0 + padValue, (1, pad[1], 1))21    img_padded = np.concatenate((pad_left, img_padded), axis=1)22    pad_down = np.tile(img_padded[-2:-1, :, :]*0 + padValue, (pad[2], 1, 1))23    img_padded = np.concatenate((img_padded, pad_down), axis=0)24    pad_right = np.tile(img_padded[:, -2:-1, :]*0 + padValue, (1, pad[3], 1))25    img_padded = np.concatenate((img_padded, pad_right), axis=1)26 27    return img_padded, pad28 29# transfer caffe model to pytorch which will match the layer name30def transfer(model, model_weights):31    transfered_model_weights = {}32    for weights_name in model.state_dict().keys():33        transfered_model_weights[weights_name] = model_weights['.'.join(weights_name.split('.')[1:])]34    return transfered_model_weights35 36# draw the body keypoint and lims37def draw_bodypose(canvas, candidate, subset):38    stickwidth = 439    limbSeq = [[2, 3], [2, 6], [3, 4], [4, 5], [6, 7], [7, 8], [2, 9], [9, 10], \40               [10, 11], [2, 12], [12, 13], [13, 14], [2, 1], [1, 15], [15, 17], \41               [1, 16], [16, 18], [3, 17], [6, 18]]42 43    colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0], \44              [0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255], \45              [170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85]]46    for i in range(18):47        for n in range(len(subset)):48            index = int(subset[n][i])49            if index == -1:50                continue51            x, y = candidate[index][0:2]52            cv2.circle(canvas, (int(x), int(y)), 4, colors[i], thickness=-1)53    for i in range(17):54        for n in range(len(subset)):55            index = subset[n][np.array(limbSeq[i]) - 1]56            if -1 in index:57                continue58            cur_canvas = canvas.copy()59            Y = candidate[index.astype(int), 0]60            X = candidate[index.astype(int), 1]61            mX = np.mean(X)62            mY = np.mean(Y)63            length = ((X[0] - X[1]) ** 2 + (Y[0] - Y[1]) ** 2) ** 0.564            angle = math.degrees(math.atan2(X[0] - X[1], Y[0] - Y[1]))65            polygon = cv2.ellipse2Poly((int(mY), int(mX)), (int(length / 2), stickwidth), int(angle), 0, 360, 1)66            cv2.fillConvexPoly(cur_canvas, polygon, colors[i])67            canvas = cv2.addWeighted(canvas, 0.4, cur_canvas, 0.6, 0)68    # plt.imsave("preview.jpg", canvas[:, :, [2, 1, 0]])69    # plt.imshow(canvas[:, :, [2, 1, 0]])70    return canvas71 72 73# image drawed by opencv is not good.74def draw_handpose(canvas, all_hand_peaks, show_number=False):75    edges = [[0, 1], [1, 2], [2, 3], [3, 4], [0, 5], [5, 6], [6, 7], [7, 8], [0, 9], [9, 10], \76             [10, 11], [11, 12], [0, 13], [13, 14], [14, 15], [15, 16], [0, 17], [17, 18], [18, 19], [19, 20]]77 78    for peaks in all_hand_peaks:79        for ie, e in enumerate(edges):80            if np.sum(np.all(peaks[e], axis=1)==0)==0:81                x1, y1 = peaks[e[0]]82                x2, y2 = peaks[e[1]]83                cv2.line(canvas, (x1, y1), (x2, y2), matplotlib.colors.hsv_to_rgb([ie/float(len(edges)), 1.0, 1.0])*255, thickness=2)84 85        for i, keyponit in enumerate(peaks):86            x, y = keyponit87            cv2.circle(canvas, (x, y), 4, (0, 0, 255), thickness=-1)88            if show_number:89                cv2.putText(canvas, str(i), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 0), lineType=cv2.LINE_AA)90    return canvas91 92# detect hand according to body pose keypoints93# please refer to https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/src/openpose/hand/handDetector.cpp94def handDetect(candidate, subset, oriImg):95    # right hand: wrist 4, elbow 3, shoulder 296    # left hand: wrist 7, elbow 6, shoulder 597    ratioWristElbow = 0.3398    detect_result = []99    image_height, image_width = oriImg.shape[0:2]100    for person in subset.astype(int):101        # if any of three not detected102        has_left = np.sum(person[[5, 6, 7]] == -1) == 0103        has_right = np.sum(person[[2, 3, 4]] == -1) == 0104        if not (has_left or has_right):105            continue106        hands = []107        #left hand108        if has_left:109            left_shoulder_index, left_elbow_index, left_wrist_index = person[[5, 6, 7]]110            x1, y1 = candidate[left_shoulder_index][:2]111            x2, y2 = candidate[left_elbow_index][:2]112            x3, y3 = candidate[left_wrist_index][:2]113            hands.append([x1, y1, x2, y2, x3, y3, True])114        # right hand115        if has_right:116            right_shoulder_index, right_elbow_index, right_wrist_index = person[[2, 3, 4]]117            x1, y1 = candidate[right_shoulder_index][:2]118            x2, y2 = candidate[right_elbow_index][:2]119            x3, y3 = candidate[right_wrist_index][:2]120            hands.append([x1, y1, x2, y2, x3, y3, False])121 122        for x1, y1, x2, y2, x3, y3, is_left in hands:123            # pos_hand = pos_wrist + ratio * (pos_wrist - pos_elbox) = (1 + ratio) * pos_wrist - ratio * pos_elbox124            # handRectangle.x = posePtr[wrist*3] + ratioWristElbow * (posePtr[wrist*3] - posePtr[elbow*3]);125            # handRectangle.y = posePtr[wrist*3+1] + ratioWristElbow * (posePtr[wrist*3+1] - posePtr[elbow*3+1]);126            # const auto distanceWristElbow = getDistance(poseKeypoints, person, wrist, elbow);127            # const auto distanceElbowShoulder = getDistance(poseKeypoints, person, elbow, shoulder);128            # handRectangle.width = 1.5f * fastMax(distanceWristElbow, 0.9f * distanceElbowShoulder);129            x = x3 + ratioWristElbow * (x3 - x2)130            y = y3 + ratioWristElbow * (y3 - y2)131            distanceWristElbow = math.sqrt((x3 - x2) ** 2 + (y3 - y2) ** 2)132            distanceElbowShoulder = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)133            width = 1.5 * max(distanceWristElbow, 0.9 * distanceElbowShoulder)134            # x-y refers to the center --> offset to topLeft point135            # handRectangle.x -= handRectangle.width / 2.f;136            # handRectangle.y -= handRectangle.height / 2.f;137            x -= width / 2138            y -= width / 2  # width = height139            # overflow the image140            if x < 0: x = 0141            if y < 0: y = 0142            width1 = width143            width2 = width144            if x + width > image_width: width1 = image_width - x145            if y + width > image_height: width2 = image_height - y146            width = min(width1, width2)147            # the max hand box value is 20 pixels148            if width >= 20:149                detect_result.append([int(x), int(y), int(width), is_left])150 151    '''152    return value: [[x, y, w, True if left hand else False]].153    width=height since the network require squared input.154    x, y is the coordinate of top left 155    '''156    return detect_result157 158# get max index of 2d array159def npmax(array):160    arrayindex = array.argmax(1)161    arrayvalue = array.max(1)162    i = arrayvalue.argmax()163    j = arrayindex[i]164    return i, j165