Best Python code snippet using avocado_python
utils.py
Source:utils.py
1import os2import cv23import numpy as np4from PIL import Image5def load_train(image_size=33, stride=33, scale=3,dirname=r'dataset\train'):6 dir_list = os.listdir(dirname)7 images = [cv2.cvtColor(cv2.imread(os.path.join(dirname,img)),cv2.COLOR_BGR2GRAY) for img in dir_list]8 #==========================9 #è¿éå¤æéæ ·æ¥é¿ æ¯å¦è½è¢«æ´é¤10 #=========================11 images = [img[0:img.shape[0]-np.remainder(img.shape[0],scale),0:img.shape[1]-np.remainder(img.shape[1],scale)] for img in images]12 trains = images.copy()13 labels = images.copy()14 #========================================15 #对train image è¿è¡æ¹æ³ç¼©å° 产çä¸æ¸
æ°çå¾å16 #========================================17 trains = [cv2.resize(img, None, fx=1/scale, fy=1/scale, interpolation=cv2.INTER_CUBIC) for img in trains]18 trains = [cv2.resize(img, None, fx=scale/1, fy=scale/1, interpolation=cv2.INTER_CUBIC) for img in trains]19 sub_trains = []20 sub_labels = []21 22 #========================================23 #éè¿éæ ·å½¢ææ ç¾ åè®ç»æ°æ®ï¼24 # ä¸å¼ å¾ç éè¿éæ ·ï¼å¯ä»¥åæå¾å¤ä¸ªå¾ååï¼ä½ä¸ºè®ç»æ°æ®ï¼ä¸°å¯æ ·æ¬25 #========================================26 for train, label in zip(trains, labels):27 v, h = train.shape28 print(train.shape)29 for x in range(0,v-image_size+1,stride):30 for y in range(0,h-image_size+1,stride):31 sub_train = train[x:x+image_size,y:y+image_size]32 sub_label = label[x:x+image_size,y:y+image_size]33 sub_train = sub_train.reshape(image_size,image_size,1)34 sub_label = sub_label.reshape(image_size,image_size,1)35 sub_trains.append(sub_train)36 sub_labels.append(sub_label)37 #========================================38 #ç¼ç 为numpy array39 #========================================40 sub_trains = np.array(sub_trains)41 sub_labels = np.array(sub_labels)42 return sub_trains, sub_labels43def load_test(scale=3,dirname=r'dataset\test'):44 #========================================45 # çææµè¯æ°æ®çæ¹å¼ä¸è®ç»æ°æ®ç¸å46 # pre_tests æ¯ç¨æ¥ä¿å缩å°åçå¾ç47 #========================================48 dir_list = os.listdir(dirname)49 images = [cv2.cvtColor(cv2.imread(os.path.join(dirname,img)),cv2.COLOR_BGR2GRAY) for img in dir_list]50 images = [img[0:img.shape[0]-np.remainder(img.shape[0],scale),0:img.shape[1]-np.remainder(img.shape[1],scale)] for img in images]51 tests = images.copy()52 labels = images.copy()53 54 pre_tests = [cv2.resize(img, None, fx=1/scale, fy=1/scale, interpolation=cv2.INTER_CUBIC) for img in tests]55 tests = [cv2.resize(img, None, fx=scale/1, fy=scale/1, interpolation=cv2.INTER_CUBIC) for img in pre_tests]56 57 pre_tests = [img.reshape(img.shape[0],img.shape[1],1) for img in pre_tests]58 tests = [img.reshape(img.shape[0],img.shape[1],1) for img in tests]59 labels = [img.reshape(img.shape[0],img.shape[1],1) for img in labels]60 return pre_tests, tests, labels61#========================================62# ä¸é¢å½æ°ç¨æ¥è®¡ç®éæååçå¾çææ 63#========================================64def mse(y, t):65 return np.mean(np.square(y - t))66def psnr(y, t):67 return 20 * np.log10(255) - 10 * np.log10(mse(y, t))68def ssim(x, y):69 mu_x = np.mean(x)70 mu_y = np.mean(y)71 var_x = np.var(x)72 var_y = np.var(y)73 cov = np.mean((x - mu_x) * (y - mu_y))74 c1 = np.square(0.01 * 255)75 c2 = np.square(0.03 * 255)...
lib.py
Source:lib.py
1import os2import cv23import numpy as np4from PIL import Image5import sys6import matplotlib.pyplot as plt7#Two simple function to load the training and testing data8def load_train(image_size=33, stride=33, scale=3, dim=3, load_txt=False):9 dirname = './data'10 dir_list = os.listdir(dirname)11 images = []12 #load the data13 images = [cv2.imread(os.path.join(dirname,img)) for img in dir_list if img != ".DS_Store"]14 images = [img[0:img.shape[0]-np.remainder(img.shape[0],scale),0:img.shape[1]-np.remainder(img.shape[1],scale)] for img in images]15 X_train = images.copy()16 Y_train = images.copy()17 #downsample and upsample to create the LR images18 X_train = [cv2.resize(img, None, fx=1/scale, fy=1/scale, interpolation=cv2.INTER_CUBIC) for img in X_train]19 X_train = [cv2.resize(img, None, fx=scale/1, fy=scale/1, interpolation=cv2.INTER_CUBIC) for img in X_train]20 sub_X_train = []21 sub_Y_train = []22 #Creating the sub images of 33x33 23 for train, label in zip(X_train, Y_train):24 v = train.shape[0]25 h = train.shape[1]26 for x in range(0,v-image_size+1,stride):27 for y in range(0,h-image_size+1,stride):28 sub_train = train[x:x+image_size,y:y+image_size]29 sub_label = label[x:x+image_size,y:y+image_size]30 if dim == 3: 31 sub_train = sub_train.reshape(image_size,image_size,3)32 sub_label = sub_label.reshape(image_size,image_size,3)33 else:34 sub_train = sub_train.reshape(image_size,image_size,1)35 sub_label = sub_label.reshape(image_size,image_size,1)36 sub_X_train.append(sub_train)37 sub_Y_train.append(sub_label)38 # ========= VERIFICATION ===========39 # cv2.imshow('image',sub_train)40 # cv2.waitKey(0)41 #convert to numpy array42 sub_X_train = np.array(sub_X_train)43 sub_Y_train = np.array(sub_Y_train)44 return sub_X_train, sub_Y_train45def load_test(scale=3, dim=3):46 dirname = './input/'47 dir_list = os.listdir(dirname)48 #load the data49 images = [cv2.cvtColor(cv2.imread(os.path.join(dirname,img)),cv2.IMREAD_COLOR) for img in dir_list if img != ".DS_Store"]50 images = [img[0:img.shape[0]-np.remainder(img.shape[0],scale),0:img.shape[1]-np.remainder(img.shape[1],scale)] for img in images]51 X_test = images.copy()52 Y_test = images.copy()53 #downsample and upsample to create the LR images54 pre_tests = [cv2.resize(img, None, fx=1/scale, fy=1/scale, interpolation=cv2.INTER_CUBIC) for img in X_test]55 X_test = [cv2.resize(img, None, fx=scale/1, fy=scale/1, interpolation=cv2.INTER_CUBIC) for img in pre_tests]56 #reshape images to add the third channel57 if dim == 3: 58 pre_tests = [img.reshape(img.shape[0],img.shape[1],3) for img in pre_tests]59 X_test = [img.reshape(img.shape[0],img.shape[1],3) for img in X_test] 60 Y_test = [img.reshape(img.shape[0],img.shape[1],3) for img in Y_test] 61 else:62 pre_tests = [img.reshape(img.shape[0],img.shape[1],1) for img in pre_tests]63 X_test = [img.reshape(img.shape[0],img.shape[1],1) for img in X_test] 64 Y_test = [img.reshape(img.shape[0],img.shape[1],1) for img in Y_test] ...
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!!