CoolFace
Apppublic

sunilsarolkar/ISL-SignLanguageTranslation

sourceHugging Facemitupdated 5mo agoView on Hugging Face
2likes
util.py456 linesDownload Raw Back to root
1import numpy as np2import math3import cv24import matplotlib5from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas6from matplotlib.figure import Figure7import numpy as np8import matplotlib.pyplot as plt9import cv210import copy11import seaborn as sns12 13def padRightDownCorner(img, stride, padValue):14    h = img.shape[0]15    w = img.shape[1]16 17    pad = 4 * [None]18    pad[0] = 0 # up19    pad[1] = 0 # left20    pad[2] = 0 if (h % stride == 0) else stride - (h % stride) # down21    pad[3] = 0 if (w % stride == 0) else stride - (w % stride) # right22 23    img_padded = img24    pad_up = np.tile(img_padded[0:1, :, :]*0 + padValue, (pad[0], 1, 1))25    img_padded = np.concatenate((pad_up, img_padded), axis=0)26    pad_left = np.tile(img_padded[:, 0:1, :]*0 + padValue, (1, pad[1], 1))27    img_padded = np.concatenate((pad_left, img_padded), axis=1)28    pad_down = np.tile(img_padded[-2:-1, :, :]*0 + padValue, (pad[2], 1, 1))29    img_padded = np.concatenate((img_padded, pad_down), axis=0)30    pad_right = np.tile(img_padded[:, -2:-1, :]*0 + padValue, (1, pad[3], 1))31    img_padded = np.concatenate((img_padded, pad_right), axis=1)32 33    return img_padded, pad34 35# transfer caffe model to pytorch which will match the layer name36def transfer(model, model_weights):37    transfered_model_weights = {}38    for weights_name in model.state_dict().keys():39        if len(weights_name.split('.'))>4:  # body2540            transfered_model_weights[weights_name] = model_weights['.'.join(41                weights_name.split('.')[3:])]42        else:43            transfered_model_weights[weights_name] = model_weights['.'.join(44                weights_name.split('.')[1:])]45    return transfered_model_weights46 47# draw the body keypoint and lims48def draw_bodypose(canvas, candidate, subset, model_type='body25'):49    stickwidth = 450    if model_type == 'body25':51        limbSeq = [[1,0],[1,2],[2,3],[3,4],[1,5],[5,6],[6,7],[1,8],[8,9],[9,10],\52                [10,11],[8,12],[12,13],[13,14],[0,15],[0,16],[15,17],[16,18],\53                [11,24],[11,22],[14,21],[14,19],[22,23],[19,20]]54        njoint = 2555    else:56        limbSeq = [[1, 2], [1, 5], [2, 3], [3, 4], [5, 6], [6, 7], [1, 8], [8, 9], \57                    [9, 10], [1, 11], [11, 12], [12, 13], [1, 0], [0, 14], [14, 16], \58                    [0, 15], [15, 17], [2, 16], [5, 17]]59        njoint = 1860 61    # colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0], \62    #           [0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255], \63    #           [170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85]]64 65    colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0], \66            [0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255], \67            [170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85], [255,255,0], [255,255,85], [255,255,170],\68                [255,255,255],[170,255,255],[85,255,255],[0,255,255]]69 70    for i in range(njoint):71        for n in range(len(subset)):72            index = int(subset[n][i])73            if index == -1:74                continue75            x, y = candidate[index][0:2]76            cv2.circle(canvas, (int(x), int(y)), 4, colors[i], thickness=-1)77    for i in range(njoint-1):78        for n in range(len(subset)):79            index = subset[n][np.array(limbSeq[i])]80            if -1 in index:81                continue82            cur_canvas = canvas.copy()83            Y = candidate[index.astype(int), 0]84            X = candidate[index.astype(int), 1]85            mX = np.mean(X)86            mY = np.mean(Y)87            length = ((X[0] - X[1]) ** 2 + (Y[0] - Y[1]) ** 2) ** 0.588            angle = math.degrees(math.atan2(X[0] - X[1], Y[0] - Y[1]))89            # print('original (mX,mY,length,angle)',(mX,mY,length,angle))90            # print(f'original cv2.ellipse2Poly((int({mY}), int({mX})), (int({length} / 2), {stickwidth}), int({angle}), 0, 360, 1)')91            polygon = cv2.ellipse2Poly((int(mY), int(mX)), (int(length / 2), stickwidth), int(angle), 0, 360, 1)92            # print(f'cv2.fillConvexPoly(cur_canvas, polygon, colors[i])')93            cv2.fillConvexPoly(cur_canvas, polygon, colors[i])94            canvas = cv2.addWeighted(canvas, 0.4, cur_canvas, 0.6, 0)95    # plt.imsave("preview.jpg", canvas[:, :, [2, 1, 0]])96    # plt.imshow(canvas[:, :, [2, 1, 0]])97    return canvas98#subsets [[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, -1.0, 11.0, 12.0, -1.0, 13.0, 14.0, 15.0, 16.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 26.650803712300775, 17.0]]99#candidates [[983.0, 172.0, 0.8991263508796692, 0.0], [980.0, 352.0, 0.930037796497345, 1.0], [848.0, 342.0, 0.8652207255363464, 2.0], [811.0, 598.0, 0.8107873797416687, 3.0], [806.0, 817.0, 0.7464589476585388, 4.0], [1120.0, 361.0, 0.8538270592689514, 5.0], [1148.0, 601.0, 0.6797391176223755, 6.0], [1149.0, 834.0, 0.5189468264579773, 7.0], [968.0, 757.0, 0.6468111276626587, 8.0], [876.0, 756.0, 0.6387956142425537, 9.0], [854.0, 1072.0, 0.4211728572845459, 10.0], [1057.0, 759.0, 0.6311940550804138, 11.0], [1038.0, 1072.0, 0.38531172275543213, 12.0], [955.0, 146.0, 0.925083339214325, 13.0], [1016.0, 151.0, 0.9023998379707336, 14.0], [909.0, 167.0, 0.9096773862838745, 15.0], [1057.0, 173.0, 0.8605436086654663, 16.0]]100def  get_bodypose(candidate, subset, model_type='coco'):101    stickwidth = 4102    if model_type == 'body25':103        limbSeq = [[1,0],[1,2],[2,3],[3,4],[1,5],[5,6],[6,7],[1,8],[8,9],[9,10],\104                [10,11],[8,12],[12,13],[13,14],[0,15],[0,16],[15,17],[16,18],\105                [11,24],[11,22],[14,21],[14,19],[22,23],[19,20]]106        njoint = 25107    else:108        limbSeq = [[1, 2], [1, 5], [2, 3], [3, 4], [5, 6], [6, 7], [1, 8], [8, 9], \109                    [9, 10], [1, 11], [11, 12], [12, 13], [1, 0], [0, 14], [14, 16], \110                    [0, 15], [15, 17], [2, 16], [5, 17]]111        njoint = 18112 113    # colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0], \114    #           [0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255], \115    #           [170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85]]116 117    colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0], \118            [0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255], \119            [170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85], [255,255,0], [255,255,85], [255,255,170],\120                [255,255,255],[170,255,255],[85,255,255],[0,255,255]]121 122    x_y_circles=[]123    for i in range(njoint):124        for n in range(len(subset)):125            index = int(subset[n][i])126            if index == -1:127                continue128            x, y = candidate[index][0:2] # 983.0, 172.0129            x_y_circles.append((x, y))130            # cv2.circle(canvas, (int(x), int(y)), 4, colors[i], thickness=-1)131 132    x_y_sticks=[]133    for i in range(njoint-1):134        for n in range(len(subset)):135            index = subset[n][np.array(limbSeq[i])] #0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, -1.0, 11.0, 12.0, -1.0, 13.0, 14.0, 15.0, 16.0, -1.0, -1.0, -1.0, -1.0, -1.0136            if -1 in index:137                continue138            # cur_canvas = canvas.copy()139            Y = candidate[index.astype(int), 0]140            X = candidate[index.astype(int), 1]141            mX = np.mean(X)142            mY = np.mean(Y)143            length = ((X[0] - X[1]) ** 2 + (Y[0] - Y[1]) ** 2) ** 0.5144            angle = math.degrees(math.atan2(X[0] - X[1], Y[0] - Y[1]))145            x_y_sticks.append((mY, mX,angle,length))146            # print('new  (mX,mY,length,angle)',(mX,mY,length,angle))147            # polygon = cv2.ellipse2Poly((int(mY), int(mX)), (int(length / 2), stickwidth), int(angle), 0, 360, 1)148            # cv2.fillConvexPoly(cur_canvas, polygon, colors[i])149            # canvas = cv2.addWeighted(canvas, 0.4, cur_canvas, 0.6, 0)150    # plt.imsave("preview.jpg", canvas[:, :, [2, 1, 0]])151    # plt.imshow(canvas[:, :, [2, 1, 0]])152    return (x_y_circles,x_y_sticks,)153 154#all_hands_peaks[[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [1100, 858], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [858, 859], [868, 894], [873, 938], [0, 0], [802, 920], [807, 961], [821, 977], [836, 992], [0, 0], [781, 955], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]]155def draw_handpose(canvas, all_hand_peaks, show_number=False):156    edges = [[0, 1], [1, 2], [2, 3], [3, 4], [0, 5], [5, 6], [6, 7], [7, 8], [0, 9], [9, 10], \157             [10, 11], [11, 12], [0, 13], [13, 14], [14, 15], [15, 16], [0, 17], [17, 18], [18, 19], [19, 20]]158    fig = Figure(figsize=plt.figaspect(canvas))159 160    fig.subplots_adjust(0, 0, 1, 1)161    fig.subplots_adjust(bottom=0, top=1, left=0, right=1)162    bg = FigureCanvas(fig)163    ax = fig.subplots()164    ax.axis('off')165    ax.imshow(canvas)166 167    width, height = ax.figure.get_size_inches() * ax.figure.get_dpi()168 169    for peaks in all_hand_peaks:170        for ie, e in enumerate(edges):171            if np.sum(np.all(peaks[e], axis=1)==0)==0:172                x1, y1 = peaks[e[0]]173                x2, y2 = peaks[e[1]]174                # print(f'original ax.plot([{x1}, {x2}], [{y1}, {y2}], color=matplotlib.colors.hsv_to_rgb([ie/float({len(edges)}), 1.0, 1.0]))')175                ax.plot([x1, x2], [y1, y2], color=matplotlib.colors.hsv_to_rgb([ie/float(len(edges)), 1.0, 1.0]))176 177        for i, keyponit in enumerate(peaks):178            x, y = keyponit179            # print(f"original ax.plot({x}, {y}, 'r.')")180            ax.plot(x, y, 'r.')181            if show_number:182                ax.text(x, y, str(i))183    # print(f'width = {width}, height={height}')184    bg.draw()185    canvas = np.fromstring(bg.tostring_rgb(), dtype='uint8').reshape(int(height), int(width), 3)186    return canvas187 188def get_handpose(all_hand_peaks, show_number=False):189    edges = [[0, 1], [1, 2], [2, 3], [3, 4], [0, 5], [5, 6], [6, 7], [7, 8], [0, 9], [9, 10], \190             [10, 11], [11, 12], [0, 13], [13, 14], [14, 15], [15, 16], [0, 17], [17, 18], [18, 19], [19, 20]]191    # fig = Figure(figsize=plt.figaspect(canvas))192 193    # fig.subplots_adjust(0, 0, 1, 1)194    # fig.subplots_adjust(bottom=0, top=1, left=0, right=1)195    # bg = FigureCanvas(fig)196    # ax = fig.subplots()197    # ax.axis('off')198    # ax.imshow(canvas)199 200    # width, height = ax.figure.get_size_inches() * ax.figure.get_dpi()201    export_edges=[[],[]]202    export_peaks=[[],[]]203    for idx,peaks in enumerate(all_hand_peaks):204        for ie, e in enumerate(edges):205            if np.sum(np.all(peaks[e], axis=1)==0)==0:206                x1, y1 = peaks[e[0]]207                x2, y2 = peaks[e[1]]208                export_edges[idx].append((ie,(x1, y1),(x2, y2)))209                # ax.plot([x1, x2], [y1, y2], color=matplotlib.colors.hsv_to_rgb([ie/float(len(edges)), 1.0, 1.0]))210 211        for i, keyponit in enumerate(peaks):212            x, y = keyponit213            # ax.plot(x, y, 'r.')214            # if show_number:215            #     ax.text(x, y, str(i))216 217            export_peaks[idx].append((x,y,str(i)))218    # bg.draw()219    # canvas = np.fromstring(bg.tostring_rgb(), dtype='uint8').reshape(int(height), int(width), 3)220    return (export_edges,export_peaks)221 222# image drawed by opencv is not good.223def draw_handpose_by_opencv(canvas, peaks, show_number=False):224    edges = [[0, 1], [1, 2], [2, 3], [3, 4], [0, 5], [5, 6], [6, 7], [7, 8], [0, 9], [9, 10], \225             [10, 11], [11, 12], [0, 13], [13, 14], [14, 15], [15, 16], [0, 17], [17, 18], [18, 19], [19, 20]]226    # cv2.rectangle(canvas, (x, y), (x+w, y+w), (0, 255, 0), 2, lineType=cv2.LINE_AA)227    # cv2.putText(canvas, 'left' if is_left else 'right', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)228    for ie, e in enumerate(edges):229        if np.sum(np.all(peaks[e], axis=1)==0)==0:230            x1, y1 = peaks[e[0]]231            x2, y2 = peaks[e[1]]232            cv2.line(canvas, (x1, y1), (x2, y2), matplotlib.colors.hsv_to_rgb([ie/float(len(edges)), 1.0, 1.0])*255, thickness=2)233 234    for i, keyponit in enumerate(peaks):235        x, y = keyponit236        cv2.circle(canvas, (x, y), 4, (0, 0, 255), thickness=-1)237        if show_number:238            cv2.putText(canvas, str(i), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 0), lineType=cv2.LINE_AA)239    return canvas240 241# detect hand according to body pose keypoints242# please refer to https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/src/openpose/hand/handDetector.cpp243def handDetect(candidate, subset, oriImg):244    # right hand: wrist 4, elbow 3, shoulder 2245    # left hand: wrist 7, elbow 6, shoulder 5246    ratioWristElbow = 0.33247    detect_result = []248    249    image_height, image_width = oriImg.shape[0:2]250    #print(f'handDetect ---------- {image_height}, {image_width}')251    for person in subset.astype(int):252        # if any of three not detected253        has_left = np.sum(person[[5, 6, 7]] == -1) == 0254        has_right = np.sum(person[[2, 3, 4]] == -1) == 0255        if not (has_left or has_right):256            continue257        hands = []258        #left hand259        if has_left:260            left_shoulder_index, left_elbow_index, left_wrist_index = person[[5, 6, 7]]261            x1, y1 = candidate[left_shoulder_index][:2]262            x2, y2 = candidate[left_elbow_index][:2]263            x3, y3 = candidate[left_wrist_index][:2]264            hands.append([x1, y1, x2, y2, x3, y3, True])265        # right hand266        if has_right:267            right_shoulder_index, right_elbow_index, right_wrist_index = person[[2, 3, 4]]268            x1, y1 = candidate[right_shoulder_index][:2]269            x2, y2 = candidate[right_elbow_index][:2]270            x3, y3 = candidate[right_wrist_index][:2]271            hands.append([x1, y1, x2, y2, x3, y3, False])272 273        for x1, y1, x2, y2, x3, y3, is_left in hands:274            # pos_hand = pos_wrist + ratio * (pos_wrist - pos_elbox) = (1 + ratio) * pos_wrist - ratio * pos_elbox275            # handRectangle.x = posePtr[wrist*3] + ratioWristElbow * (posePtr[wrist*3] - posePtr[elbow*3]);276            # handRectangle.y = posePtr[wrist*3+1] + ratioWristElbow * (posePtr[wrist*3+1] - posePtr[elbow*3+1]);277            # const auto distanceWristElbow = getDistance(poseKeypoints, person, wrist, elbow);278            # const auto distanceElbowShoulder = getDistance(poseKeypoints, person, elbow, shoulder);279            # handRectangle.width = 1.5f * fastMax(distanceWristElbow, 0.9f * distanceElbowShoulder);280            x = x3 + ratioWristElbow * (x3 - x2)281            y = y3 + ratioWristElbow * (y3 - y2)282            distanceWristElbow = math.sqrt((x3 - x2) ** 2 + (y3 - y2) ** 2)283            distanceElbowShoulder = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)284            width = 1.5 * max(distanceWristElbow, 0.9 * distanceElbowShoulder)285            # x-y refers to the center --> offset to topLeft point286            # handRectangle.x -= handRectangle.width / 2.f;287            # handRectangle.y -= handRectangle.height / 2.f;288            x -= width / 2289            y -= width / 2  # width = height290            # overflow the image291            if x < 0: x = 0292            if y < 0: y = 0293            width1 = width294            width2 = width295            if x + width > image_width: width1 = image_width - x296            if y + width > image_height: width2 = image_height - y297            width = min(width1, width2)298            # the max hand box value is 20 pixels299            if width >= 20:300                detect_result.append([int(x), int(y), int(width), is_left])301 302    '''303    return value: [[x, y, w, True if left hand else False]].304    width=height since the network require squared input.305    x, y is the coordinate of top left 306    '''307    return detect_result308 309def drawStickmodel(oriImg, x_ytupple, x_y_sticks, export_edges, export_peaks):310    canvas = copy.deepcopy(oriImg)311 312    colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0],313              [85, 255, 0], [0, 255, 0], [0, 255, 85], [0, 255, 170], [0, 255, 255],314              [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255], [170, 0, 255],315              [255, 0, 255], [255, 0, 170], [255, 0, 85], [255,255,0], [255,255,85],316              [255,255,170], [255,255,255],[170,255,255],[85,255,255],[0,255,255]]317    stickwidth = 4318 319    for idx, (mX, mY, angle, length) in enumerate(x_y_sticks):320        cur_canvas = canvas.copy()321        polygon = cv2.ellipse2Poly((int(mX), int(mY)), (int(length / 2), stickwidth),322                                   int(angle), 0, 360, 1)323        cv2.fillConvexPoly(cur_canvas, polygon, colors[idx])324        canvas = cv2.addWeighted(canvas, 0.4, cur_canvas, 0.6, 0)325 326    for idx, (x, y) in enumerate(x_ytupple):327        cv2.circle(canvas, (int(x), int(y)), 4, colors[idx], thickness=-1)328 329    # Handpose330    fig = Figure(figsize=plt.figaspect(canvas))331    fig.subplots_adjust(0, 0, 1, 1)332    ax = fig.subplots()333    ax.axis('off')334    ax.imshow(canvas)335 336    edges = [[0, 1], [1, 2], [2, 3], [3, 4], [0, 5], [5, 6], [6, 7], [7, 8], [0, 9],337             [9, 10], [10, 11], [11, 12], [0, 13], [13, 14], [14, 15], [15, 16],338             [0, 17], [17, 18], [18, 19], [19, 20]]339 340    for both_hand_edges in export_edges:341        for (ie, (x1, y1), (x2, y2)) in both_hand_edges:342            ax.plot([x1, x2], [y1, y2],343                    color=matplotlib.colors.hsv_to_rgb([ie/float(len(edges)), 1.0, 1.0]))344 345    for both_hand_peaks in export_peaks:346        for (x, y, text) in both_hand_peaks:347            ax.plot(x, y, 'r.')348 349    # Convert figure to numpy array350    bg = FigureCanvas(fig)351    bg.draw()352 353    width, height = fig.get_size_inches() * fig.get_dpi()354    buf = bg.buffer_rgba()355    canvas = np.frombuffer(buf, dtype=np.uint8).reshape(int(height), int(width), 4)356    canvas = canvas[:, :, :3]  # keep only RGB357 358    plt.close(fig)  # clean up359    return cv2.resize(canvas, (math.ceil(width), math.ceil(height)))360 361def draw_bar_plot_below_image(image, predictions, title, origImg):362  """363  Draws a bar plot of predictions below an image using OpenCV and Matplotlib.364 365  Args:366      image (numpy.ndarray): The image to display.367      predictions (numpy.ndarray): Array containing prediction probabilities.368  """369 370 371 372  fig, ax = plt.subplots(figsize=(origImg.shape[1]/100,origImg.shape[0]/200), dpi=100)373  plt.title(title)374  # Create a figure and plot the bar chart375  labels = list(predictions.keys())376  probabilities = list(predictions.values())377 378  # Create a Seaborn bar plot379  sns.barplot(x=labels, y=probabilities,ax=ax)  # Default color palette used380  plt.close(fig)  # Close plot to avoid memory leaks381  fig.canvas.draw()382  # Convert the plot to a NumPy array for manipulation383  plot_image = np.array(fig.canvas.renderer.buffer_rgba())[:, :, :3]  # Remove alpha channel384 385  # Resize the plot image to match the width of the original image386#   plot_image = cv2.resize(plot_image, (image.shape[1], math.ceil(image.shape[0] * 0.8)))  # Adjust height ratio as needed387 388  # Combine the image and plot image vertically (stacking)389  combined_image = np.vstack((image, cv2.resize(plot_image,(image.shape[1],plot_image.shape[0]))))390 391  return combined_image392 393def add_padding_to_bottom(image, pad_value, pad_height):394  """395  Adds padding to the bottom of an image with a specified value.396 397  Args:398      image (numpy.ndarray): The input image.399      pad_value (tuple or int): The color value to fill the padding area.400      pad_height (int): The height of the padding to add at the bottom.401 402  Returns:403      numpy.ndarray: The image with padding added.404  """405 406  # Get image dimensions407  height, width, channels = image.shape408  padding=np.zeros((pad_height, width, channels), dtype=image.dtype)409  padding[:,:,:]=pad_value410#   # Create a new image with the desired height411#   padded_image = np.zeros((height + pad_height, width, channels), dtype=image.dtype)412 413#   # Copy the original image to the top of the padded image414#   padded_image[:height, :, :] = image415 416#   # Fill the padding area with the specified value417#   if isinstance(pad_value, tuple):  # Check for multiple color values (e.g., BGR)418#       padded_image[height:, :, :] = pad_value419#   else:  # Single value for all channels (e.g., black)420#       padded_image[height:, :, :] = np.full((pad_height, width, 1), pad_value, dtype=image.dtype)421 422  return np.vstack((image, padding))423 424def crop_to_drawing(image):425  """426  Crops an image to the tight bounding rectangle of non-zero pixels.427 428  Args:429      image: A NumPy array representing the image.430 431  Returns:432      A cropped image (NumPy array) containing only the drawing area.433  """434  image=np.transpose(image, (2, 0, 1))435  united_x,united_h=0,0436  for channel in np.arange(image.shape[0]):437    x, y, w, h = cv2.boundingRect(image[channel])438    if x>united_x:439        united_x=x440 441    if h>united_h:442        united_h=h443 444  for channel in np.arange(image.shape[0]):445    # Crop the image446    image[channel] = image[channel][y:y+united_h, x:x+united_x]447  return image.transpose(image, (1,2,0))448 449# get max index of 2d array450def npmax(array):451    arrayindex = array.argmax(1)452    arrayvalue = array.max(1)453    i = arrayvalue.argmax()454    j = arrayindex[i]455    return i, j456