1import os 2 3import cv2 4import numpy 5from PIL import Image 6from devicetest.api import Asserts 7from devicetest.aw.OpenHarmony import CommonOH 8from devicetest.core.test_case import TestCase, Step 9 10 11class ITestCase(TestCase): 12 13 def __init__(self, controllers): 14 self.TAG = self.__class__.__name__ 15 TestCase.__init__(self, self.TAG, controllers) 16 self.device_name = self.Phone1.device_sn 17 # path 相关 18 self.device_save_path = '/data/local/tmp/screen_test/' 19 self.testcases_path = os.path.dirname(__file__) 20 self.local_resource_path = os.path.join(os.path.dirname(self.testcases_path), 'resource') 21 self.local_save_path = self.cur_case.case_screenshot_dir 22 if not os.path.exists(self.local_save_path): 23 os.makedirs(self.local_save_path, exist_ok=True) 24 # framework utils 25 self.STANDARD_SIMILARITY = 55 26 self.common_oh = CommonOH 27 self.step = Step 28 self.asserts = Asserts() 29 30 def take_picture_to_local(self, picture_name): 31 """ 32 将图片从设备上传回本地 33 :param picture_name: 34 :return: 35 """ 36 self.common_oh.removeFolderByCMD(self.Phone1, '{}*{}'.format(self.device_save_path, picture_name)) 37 self.common_oh.takePictureByCMD(self.Phone1, '{}{}_{}'.format(self.device_save_path, self.device_name, picture_name)) 38 self.common_oh.pullFile(self.Phone1, '{}{}_{}'.format(self.device_save_path, self.device_name, picture_name), self.local_save_path) 39 self.common_oh.wait(self.Phone1, 1) 40 41 def crop_picture(self, picture, crop_range=None): 42 """ 43 对图片进行裁剪 44 :param picture:待裁剪的图片路径 45 :param crop_range: 裁剪的尺寸,如[80, 1200, 0, 720] 表示纵向80~1200,横向0~720的裁剪范围,基本就是去掉上面的状态栏和下面的导航栏 46 :return: 47 """ 48 picture = os.path.join(self.local_save_path, '{}_{}'.format(self.device_name, picture)) 49 if crop_range is None: 50 crop_range = [80, 1200, 0, 720] 51 img = cv2.imread(picture) 52 img = img[crop_range[0]: crop_range[1], crop_range[2]: crop_range[3]] 53 cv2.imwrite(picture, img) 54 55 def compare_image_similarity(self, picture_name): 56 src_image_path = os.path.join(self.local_save_path, '{}_{}'.format(self.device_name, picture_name)) 57 target_image_path = os.path.join(self.local_resource_path, picture_name) 58 size = (256, 256) 59 image1 = Image.open(src_image_path) 60 image2 = Image.open(target_image_path) 61 image1 = cv2.cvtColor(numpy.asarray(image1), cv2.COLOR_RGB2BGR) 62 image2 = cv2.cvtColor(numpy.asarray(image2), cv2.COLOR_RGB2BGR) 63 image1 = cv2.resize(image1, size) 64 image2 = cv2.resize(image2, size) 65 sub_image1 = cv2.split(image1) 66 sub_image2 = cv2.split(image2) 67 sub_data = 0 68 for im1, im2 in zip(sub_image1, sub_image2): 69 sub_data += self.__calculate__(im1, im2) 70 sub_data = sub_data / 3 71 72 if isinstance(sub_data, numpy.ndarray): 73 sub_data = sub_data[0] 74 return sub_data * 100 75 76 def __calculate__(self, img1, img2): 77 image1 = cv2.cvtColor(numpy.asarray(img1), cv2.COLOR_RGB2BGR) 78 image2 = cv2.cvtColor(numpy.asarray(img2), cv2.COLOR_RGB2BGR) 79 hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0]) 80 hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0]) 81 degree = 0 82 for i in range(len(hist1)): 83 if hist1[i] != hist2[i]: 84 degree = degree + (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i])) 85 else: 86 degree = degree + 1 87 degree = degree / len(hist1) 88 return degree 89