Best Python code snippet using Airtest
stitch.py
Source:stitch.py  
...20import matplotlib.pyplot as plt 21import copy22import random as rand23from PIL import Image24def match_keypoints(kp1,kp2,des1,des2,src_img,des_img):25    26    """27    MATCH THE KEYPOINTS OF LEFT IMAGE AND RIGHT IMAGE28    INPUT: KEYPOINTS OF LEFT AND RIGHT IMAGE, DESCRIPTORS OF LEFT AND RIGHT IMAGE, MATCH KEYOINTS ON! 29    OUTPUT : SHOWS THE MATCH KEYPOINTS IN IMAGE 1 (LEFT ) AND IMAGE 2 (RIGHT). 30    """31    32# --------------------  SMALLEST DISTANCE MEASURED USING SSD -----------------------33    des_length1 = des1.shape[0]34    des_length2 = des2.shape[0]35    print("DES1",des_length1)36    print("DES2",des_length2)37    match_points = []38    for ix,iv in enumerate(des1):39        dist = np.linalg.norm(des1[ix] - des2[0])40        best_dist = dist41        sec_best = dist42        dist_best_idx = [ix,0]43        # print(dist_best_idx)44        for jx,jv in enumerate(des2):45            dist = np.linalg.norm(des1[ix]- des2[jx])46            if dist < best_dist:47                sec_best = best_dist48                best_dist = dist49                dist_best_idx = [ix, jx]50            elif dist < sec_best:51                sec_best = dist52        if (best_dist/sec_best) < 0.8:53            match_points.append(dist_best_idx)54    55    # print(match_points)56    match_points = np.array(match_points)57    match_points_img1 = match_points[:,0]58    match_points_img2 = match_points[:,1]59    # print(match_points_img1)60    # print(match_points_img2)61    keypoint_coord_img_1 = []62    keypoint_coord_img_2 = []63    64    for ii_idx,ii_val in enumerate(match_points_img1):65        keypoint_coord_img_1.append(kp1[ii_val].pt)66    for jj_idx,jj_val in enumerate(match_points_img2):67        keypoint_coord_img_2.append(kp2[jj_val].pt)68# ----------------------- MERGING ALL THE MATCHED KEYPOINTS -----------------------------------------69    keypoints_match = np.concatenate((np.array(keypoint_coord_img_1),np.array(keypoint_coord_img_2)),axis= 1 )70    # print(keypoints_match)71    keypoint_coord_img_1_x = []72    keypoint_coord_img_1_y = []73    74    keypoint_coord_img_2_x = []75    keypoint_coord_img_2_y = []76    77    for xx,yy in keypoint_coord_img_1:78        keypoint_coord_img_1_x.append(xx)79        keypoint_coord_img_1_y.append(yy)80    for xx1,yy1 in keypoint_coord_img_2:81        keypoint_coord_img_2_x.append(xx1)82        keypoint_coord_img_2_y.append(yy1)83    keypoint_coord_img_1_x  = np.asarray(keypoint_coord_img_1_x)84    keypoint_coord_img_1_y  = np.asarray(keypoint_coord_img_1_y)85    keypoint_coord_img_2_x  = np.asarray(keypoint_coord_img_2_x)86    keypoint_coord_img_2_y  = np.asarray(keypoint_coord_img_2_y)    87    88    # print(type(keypoint_coord_img_1_x))89    # plt.figure(figsize = (16,14))90    # plt.subplot(2,1,1)91    # plt.imshow(src_img)92    # plt.scatter(keypoint_coord_img_1_x,keypoint_coord_img_1_y)93    # plt.subplot(2,1,2)94    # plt.imshow(des_img)95    # plt.scatter(keypoint_coord_img_2_x,keypoint_coord_img_2_y)96    # plt.show()97    98    print("Press CTRL+W TO CLOSE THE IF THE IMAGE WINDOW SHOWING KEYPOINTS DISPLAY!! ")99    return keypoints_match100    101def RANSAC(match_keypoints,iterations):102    """103    GETS THE HOMOGRAPHY MATRIX IN ORDER TO REMOVE THE OUTLIERS! 104    INPUT: MATCHKEYPOINTS (N x 4) MATRIX, ITERATIONS105    OUTPUT: HOMOGRAPHY MATRIX (3X3)106    """107    no_of_keypoints = match_keypoints.shape[0]108    ones_ = np.ones((no_of_keypoints,1))109    a_ = match_keypoints110    best_H = []111    highest_inlier_count = 0112    corresponding_points = np.zeros((no_of_keypoints,2))113    114    115    point_1_ = np.append(match_keypoints[:,0:2],ones_,axis = 1)116    point_2_ = match_keypoints[:,2:4]117    for iteration in range(iterations):118        a = rand.choice(match_keypoints)119        b = rand.choice(match_keypoints)120        c = rand.choice(match_keypoints)121        d = rand.choice(match_keypoints)122        random_matchpairs = np.concatenate(([a],[b],[c],[d]),axis= 0)123        # print('HI',random_matchpairs)124        point_1 = np.float32(random_matchpairs[:,0:2])125        point_2 = np.float32(random_matchpairs[:,2:4])126        H = cv2.getPerspectiveTransform(point_1,point_2)127        current_inlier_count = 0128        # --- Solve for Ah = b ------- #129        xx_index = list()130        good_match = list()131        for i in range(no_of_keypoints):132            check_ = np.matmul(H,point_1_[i])133            corresponding_points[i] = (check_/check_[-1])[0:2]134            check = np.square(np.linalg.norm(corresponding_points[i] - point_2_[i]))135            if check < 0.6:136                current_inlier_count = current_inlier_count + 1137                xx_index.append(i)138                good_match.append([ a_[i][0], a_[i][1], a_[i][2], a_[i][3]])139        if current_inlier_count > highest_inlier_count:140            highest_inlier_count = current_inlier_count141            best_match = good_match142            print('Best matching.....',len(best_match))143            best_H = H144            145    146    print("Best matchin ..... DONE")147    print("RANSAC DONE!")148    return best_H149def main():150    all_images = []151    all_images.extend(glob.glob('sys.argv[1]' + '*.jpg'))152    all_images.sort()153    print(all_images)154    inp_img = []155    folder = sys.argv[1]156    for filename in os.listdir(folder):157        if filename == "panorama.jpg":158            print('Panorama Image already found, ignoring that image for stitching')159            continue160        img = cv2.imread(os.path.join(folder, filename))161        print(filename)162        dimension = (500,400)163        n = cv2.resize(img,dimension,interpolation=cv2.INTER_AREA)164        inp_img.append(n)165    # inp_img.sort()166    # print(inp_img)167    168    print(len(inp_img))169    sift = cv2.xfeatures2d.SIFT_create()170    # ---------------- DETECT AND DRAW THE KEYPOINTS USING SIFT --------------------171    img_nos = list()172    for i in range(len(inp_img)):173        img = kps, des = sift.detectAndCompute(inp_img[i],None)174        img_nos.append(img)175    176    img_match_coor = []177    print("IMAGE ORDERING......")178    #------------ CHECK FOR THE CORRESPONDING KEYPOINTS MATCH WITH RESPECT TO ONE IMAGE --------------------------179    for k in range(len(inp_img) - 1):180        for l in range(k+1, len(inp_img)):181            a01 = match_keypoints(img_nos[k][0],img_nos[l][0],img_nos[k][1],img_nos[l][1],inp_img[k],inp_img[l])182            if a01.shape == (1,4):183                print("N0 Points Match")184                continue185            print("KP1", len(img_nos[k][0]))186            print(a01.shape)187            print((k,l),a01.shape[0]/len(img_nos[k][0]))188            x = a01.shape[0]/len(img_nos[k][0])189            y = a01.shape[0]/len(img_nos[l][0])190            if (x > 0.04 and x < 0.9) and (y > 0.04 and y < 0.9):191                print("Selected")192                img_match_coor.append((k,l))193    print(img_match_coor)    194    print('Matching Image Pairs Achieved.')195    #------------------ ONCE WE GOT UNIQUE POINTS, CHECK FOR ENDPOINTS FROM THAT --------------------------------196    all_index = list()197    for a in range(len(img_match_coor)):198        for b in range(2):199            # print((a,b), "CHECK", img_match_coor[a][b])  #TO CHECK THE INDEX OF EACH ELEMENT OF THE TUPLE200            all_index.append(img_match_coor[a][b])201    # print(all_index) 202    all_index = set(all_index)203    print(all_index) #GIVES YOU UNIQUE ELEMENT OF THE TUPLE204    205    #------------------- CHECK FOR RESPECTIVE END POINTS POSSIBILITY--------------------------206    print("Checking for endpoints ......")207    end_points = list()208    for c in all_index:209        count = 0210        for x in range(len(img_match_coor)):211            if c in img_match_coor[x]:212                print('Current Point:',c)213                count = count + 1 214                print('Count',count)215                if count > 1:216                    print("More than required, remove: ", c)217                    end_points.remove(c)218                    continue219                else: 220                    print('check for end points.....',c)221                    end_points.append(c)222                223    print(end_points)224    print("Endpoints Acquired")225    final_order_list = []226    print("Arranging Image pairs in acatual(panoramic) order .................")227    for x in end_points:228        final_order_list.append(x)229        # if len(img_match_coor) == 1:230        #     print("NIKAL LAVDE")231        #     break232        for y in range(len(img_match_coor)):233            if len(img_match_coor) == 1:234                break235            for z in range(len(img_match_coor[y])):236                # print((y,z))237                # if len(img_match_coor) == 1:238                #     print("CONTROLL")239                #     break240                if x in img_match_coor[y]:241                    # print("CHECK", img_match_coor[y])242                    if x != img_match_coor[y][z]:243                        x_cores = img_match_coor[y][z]244                        final_order_list.append(x_cores)245                        print(x,"CHRCK",img_match_coor[y],"YEHS HAI X CORES", x_cores)246                        img_match_coor.remove(img_match_coor[y])247                        print(img_match_coor)248                        for io_ in range(len(img_match_coor)):249                            if len(img_match_coor) == 1:250                                break251                            for iz in range(len(img_match_coor[io_])):252                                if x_cores in img_match_coor[io_]:253                                    if x_cores != img_match_coor[io_][iz]:254                                        x_cores_up = img_match_coor[io_][iz]255                                        final_order_list.append(x_cores_up)256                                        print(x_cores,"CHECK NEW",img_match_coor[io_],"YEH HAI UPDATED",x_cores_up)257                                        img_match_coor.remove(img_match_coor[io_])258                                        print(img_match_coor)259                                        for io1_ in range(len(img_match_coor)):260                                            if len(img_match_coor) == 1:261                                                break262                                            for iz1 in range(len(img_match_coor[io1_])):263                                                if x_cores_up in img_match_coor[io1_]:264                                                    if x_cores_up != img_match_coor[io1_][iz1]:265                                                        x_cores_up_up = img_match_coor[io1_][iz1]266                                                        final_order_list.append(x_cores_up_up)267                                                        print(x_cores_up,"CHECK EKDUM NEW",img_match_coor[io1_],"YEH HAI UDDDD",x_cores_up_up)268                                                        img_match_coor.remove(img_match_coor[io1_])269                                                        print(img_match_coor)270                                        if len(img_match_coor) == 1:271                                            break272                        print("LEN", len(img_match_coor))273                        if len(img_match_coor) == 1:274                            break275                        276    277    print(final_order_list)278    homography_list = list()279    print("Arranging Image pairs in acatual(panoramic) order ................. In process")280    281    len_ord_fin = len(final_order_list)282    tkp1 = img_nos[final_order_list[0]][0]283    tdes1 = img_nos[final_order_list[0]][1]284    tkp2 = img_nos[final_order_list[1]][0]285    tdes2 = img_nos[final_order_list[1]][1]286    x1 = match_keypoints(tkp1,tkp2,tdes1,tdes2,inp_img[final_order_list[0]],inp_img[final_order_list[1]])287    y1 = (RANSAC(x1,400))288    check1_  = np.matmul(y1, np.array([0, 0, 1]))289    if check1_[0]/check1_[2] < 0:290        print(check1_[0]/check1_[2])291    else:292        final_order_list.reverse()293        print(check1_[0]/check1_[2])294    # print(f'This is final list now: {final_order_list}')295    print("This is final list now:", final_order_list)296    # t_match = match_keypoints()297    print("Image ORDERING ....... COMPLETED")298    ########################------------------------- MAIN STICHING PROGRAM STARTS HERE ---------------------------------------------------##########################299    print("Image Stitching ..... Started")300    def panorama_(i1, i2):301        """302        THIS FUNCTION DOES THE STITCHING OF TWO IMAGES303        INPUT: TWO IMAGE304        OUTPUT: RESULTED IMAGE305        """306        kp1, des1 = sift.detectAndCompute(i1,None)307        kp2, des2 = sift.detectAndCompute(i2,None)308        correspondence_ = match_keypoints(kp1,kp2,des1,des2,i1,i2)309        H  = RANSAC(correspondence_,400)310        H = np.linalg.inv(H)311        stitched_ = cv2.warpPerspective(i2,H,(i1.shape[1]+ i2.shape[1],i1.shape[0]))312        stitched_[0:i1.shape[0],0:i1.shape[1]] = i1313        # plt.figure(figsize=(16,14)) 314        # plt.imshow(stitched_)315        # plt.show()        316        return stitched_317    final_order_list.reverse()318    panorama = inp_img[final_order_list[0]]319    320    img_width = 0321    img_height = 0322    for index_,image_ in enumerate(final_order_list[:-1]):323        kp_last_image,des_last_image = sift.detectAndCompute(panorama,None)324        previous_image = inp_img[final_order_list[index_+1]]325        kp_previous_image, descriptor_previous_image  = sift.detectAndCompute(previous_image,None)326        xx = match_keypoints(kp_last_image,kp_previous_image,des_last_image,descriptor_previous_image,panorama,previous_image)327        H = RANSAC(xx,500)328        # print(h)329        img_width += panorama.shape[1] + previous_image.shape[1]330        img_height = previous_image.shape[0]331        panorama = cv2.warpPerspective(panorama,(H),(img_width,img_height))332        panorama[0:previous_image.shape[0], 0:previous_image.shape[1]] = previous_image 333        # plt.figure()334        # plt.title("JOILE")335        # plt.imshow(panorama)336        # print("LE MARO")337    plt.figure()338    plt.title("Magnifincento!")339    plt.imshow(panorama)340    plt.show()...4-draw-matches.py
Source:4-draw-matches.py  
1#!/usr/bin/env python32import argparse3import os4import tempfile5import cv26import utils7if __name__ == '__main__':8    parser = argparse.ArgumentParser()9    parser.add_argument('gw_20p_wannot_dirpath')10    parser.add_argument('matches_filepath')11    args = parser.parse_args()12    matches = utils.load_matches(args.matches_filepath)13    output_dirpath = tempfile.mkdtemp()14    # extract top-k matches15    k = 2016    print(f'Extracting top-{k} matches in each page...')17    for page_filename, page_matches in matches.items():18        top_k_page_matches = sorted(page_matches, key=lambda x: x[1])[:k]19        assert len(top_k_page_matches) <= k20        page_image = cv2.imread(f'{args.gw_20p_wannot_dirpath}/{page_filename}')21        for i, match_scored in enumerate(top_k_page_matches):22            match = match_scored[0]23            # keep only non-empty keypoints24            match_keypoints = [keypoint for keypoint in match if keypoint]25            match_keypoints = utils.namedtuple_keypoints_to_cv2(match_keypoints)26            # draw keypoints on the original page27            page_image = cv2.drawKeypoints(28                page_image, match_keypoints, None, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)29            # draw a box around the match30            offset_pixel = 2531            x_min = float('inf')32            y_min = float('inf')33            x_max = 034            y_max = 035            for keypoint in match_keypoints:36                x = int(keypoint.pt[0])37                y = int(keypoint.pt[1])38                if x < x_min:39                    x_min = x40                if x > x_max:41                    x_max = x42                if y < y_min:43                    y_min = y44                if y > y_max:45                    y_max = y46            x_min += -offset_pixel47            y_min += -offset_pixel48            x_max += +offset_pixel49            y_max += +offset_pixel50            cv2.rectangle(page_image, (x_min, y_min), (x_max, y_max), (0, 0, 255), 2)51        page_image_filepath = f'{output_dirpath}/{page_filename}.png'52        print(f'Saving: {page_image_filepath}')...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
