Exemplo n.º 1
0
def assert_file_exist(file, flag=None):
    if flag is None:
        if not os.path.exists(file):
            raise TargetNotFoundError("%s is not exist" % file)
    else:
        index = file.rindex('/')
        start = file[:index]
        end = file[index + 1:]
        for fi in os.listdir(start):
            if end in fi:
                index = -1
        if index != -1:
            raise TargetNotFoundError("%s is not exist" % file)
Exemplo n.º 2
0
def assert_ocr_true(start_x,
                    start_y,
                    end_x,
                    end_y,
                    pre,
                    flag=1,
                    val=1,
                    language=None):
    img = ImageGrab.grab(bbox=(start_x, start_y, end_x, end_y))
    real = ""
    if language is None:
        real = pytesseract.image_to_string(
            cv2.threshold(cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRAY), 0,
                          255, cv2.THRESH_OTSU)[1]).strip()
    else:
        img = cv2.threshold(cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRAY), 0,
                            255, cv2.THRESH_OTSU)[1]
        img = cv2.resize(img, None, fx=8, fy=8)
        real = pytesseract.image_to_string(img, lang='chi_sim').strip()
    if real == pre:
        return
    elif pre in real and flag == 2:
        return
    elif Levenshtein.distance(pre, real) < val and flag == 3:
        return
    raise TargetNotFoundError("The OCR result is{}, not {}".format(real, pre))
Exemplo n.º 3
0
def assert_word_exist(file, row, words_given):
    f = open(file, 'rb')
    offset = -50
    file_size = os.stat(file).st_size
    assert_word_exist_flag = False
    beginning_of_file_flag = False
    while True:
        if -offset > file_size:
            f.seek(0, 0)
            beginning_of_file_flag = True
        else:
            f.seek(offset, 2)
        lines = f.readlines()
        if len(lines) >= row + 1 or (len(lines) == row
                                     and beginning_of_file_flag):
            if row > 1:
                words_in_file = lines[-row:]
                words_in_file_co = ''
                for j in range(len(words_in_file)):
                    words_in_file_co += str(words_in_file[j], encoding='gbk')
                words_in_file_co = words_in_file_co.replace('\r\n', '')
                if words_given in words_in_file_co:
                    assert_word_exist_flag = True
            else:
                words_in_file = lines[-1]
                words_in_file = str(words_in_file, encoding='gbk')
                if words_given in words_in_file:
                    assert_word_exist_flag = True
            break
        else:
            offset *= 2
    if assert_word_exist_flag is False:
        raise TargetNotFoundError(
            "{} is not on the last {} lines of {}".format(
                words_given, row, file))
Exemplo n.º 4
0
def loop_find_vanish(query,
                     timeout=ST.FIND_TIMEOUT,
                     threshold=None,
                     interval=0.5,
                     intervalfunc=None):
    G.LOGGING.info("Try finding: %s", query)
    start_time = time.time()
    while True:
        screen = G.DEVICE.snapshot(filename=None, quality=ST.SNAPSHOT_QUALITY)

        if screen is None:
            G.LOGGING.warning("Screen is None, may be locked")
        else:
            if threshold:
                query.threshold = threshold
            match_pos = query.match_in(screen)
            if match_pos:
                time.sleep(interval)
            else:
                try_log_screen(screen)
                return match_pos

        if intervalfunc is not None:
            intervalfunc()

        # 超时则raise,未超时则进行下次循环:
        if (time.time() - start_time) > timeout:
            try_log_screen(screen)
            raise TargetNotFoundError('Picture %s not vanish in screen' %
                                      query)
        else:
            time.sleep(interval)
Exemplo n.º 5
0
Arquivo: utils.py Projeto: wyy318/yys
def wati_util(v, condition, timeout=60, interval=0.5):
    """等待符合条件的对象
    """
    start_time = time.time()
    while True:
        ret = find_all(v)
        if condition(ret):
            return ret
        if (time.time() - start_time) > timeout:
            raise TargetNotFoundError('Continue %s not found in screen' % v)
        time.sleep(interval)
Exemplo n.º 6
0
Arquivo: utils.py Projeto: wyy318/yys
def select(vs, timeout=60, interval=0.5):
    """等待多个对象,返回第一个匹配到的对象
    """
    start_time = time.time()
    while True:
        for idx, v in enumerate(vs):
            ret = find_all(v)
            if ret:
                return idx, ret
        if (time.time() - start_time) > timeout:
            raise TargetNotFoundError('Continue %s not found in screen' % v)
        time.sleep(interval)
Exemplo n.º 7
0
 def assert_exists(msg=""):
     try:
         r = self.exists()
         if r is False:
             raise TargetNotFoundError("找不到对象:{0}".format(self.kw))
     except TargetNotFoundError:
         if self.is_airtest:
             raise AssertionError(
                 "%s does not exist in screen, message: %s" %
                 (self.airtest_ui, msg))
         else:
             v = self.poco_ui.query
             raise AssertionError(
                 "%s does not exist in screen, message: %s" % (v, msg))
Exemplo n.º 8
0
def assert_client_exist(order):
    data = sock.recv(1024)
    message = data.decode()
    client_message = data
    if len(message) == 18:
        client_message = data[12:18]
        sock.send(client_message)
    else:
        client_not_message = data
        sock.send(client_not_message)
    if client_message.decode() == "CCCCCC":
        pass
    else:
        raise TargetNotFoundError(order)
Exemplo n.º 9
0
Arquivo: cv.py Projeto: smqh-smqh/kwai
def loop_find4(total_pic,
               query,
               timeout=ST.FIND_TIMEOUT,
               threshold=None,
               interval=0.5,
               intervalfunc=None):
    """
    Search for image template in the screen until timeout

    Args:
        query: image template to be found in screenshot
        timeout: time interval how long to look for the image template
        threshold: default is None
        interval: sleep interval before next attempt to find the image template
        intervalfunc: function that is executed after unsuccessful attempt to find the image template

    Raises:
        TargetNotFoundError: when image template is not found in screenshot

    Returns:
        TargetNotFoundError if image template not found, otherwise returns the position where the image template has
        been found in screenshot

    """
    G.LOGGING.info("Try finding:\n%s", query)
    start_time = time.time()
    while True:
        # screen = G.DEVICE.snapshot(filename=None, quality=ST.SNAPSHOT_QUALITY)
        screen = aircv.imread(total_pic)
        if screen is None:
            G.LOGGING.warning("Screen is None, may be locked")
        else:
            if threshold:
                query.threshold = threshold
            match_pos = query.match_in(screen)
            if match_pos:
                try_log_screen(screen)
                return match_pos

        if intervalfunc is not None:
            intervalfunc()

        # 超时则raise,未超时则进行下次循环:
        if (time.time() - start_time) > timeout:
            try_log_screen(screen)
            raise TargetNotFoundError('Picture %s not found in screen' % query)
        else:
            time.sleep(interval)
Exemplo n.º 10
0
def loop_find(query,
              driver=None,
              timeout=10,
              threshold=None,
              interval=0.5,
              intervalfunc=None):
    """
    Search for image template in the screen until timeout

    Args:
        query: image template to be found in screenshot
        timeout: time interval how long to look for the image template
        threshold: default is None
        interval: sleep interval before next attempt to find the image template
        intervalfunc: function that is executed after unsuccessful attempt to find the image template

    Raises:
        TargetNotFoundError: when image template is not found in screenshot

    Returns:
        TargetNotFoundError if image template not found, otherwise returns the position where the image template has
        been found in screenshot

    """
    threshold = 0.6
    start_time = time.time()
    while True:
        screen = driver.screenshot()
        query.resolution = get_resolution(screen)
        if screen is None:
            print("Screen is None, may be locked")
        else:
            if threshold:
                query.threshold = threshold
            match_pos = query.match_in(screen)
            if match_pos:
                try_log_screen(screen)
                return match_pos

        if intervalfunc is not None:
            intervalfunc()

        # 超时则raise,未超时则进行下次循环:
        if (time.time() - start_time) > timeout:
            try_log_screen(screen)
            raise TargetNotFoundError('Picture %s not found in screen' % query)
        else:
            time.sleep(interval)
Exemplo n.º 11
0
def assert_exists(v, msg="", timeout=ST.FIND_TIMEOUT):
    """
    Assert target exists on device screen

    :param v: target to be checked
    :param msg: short description of assertion, it will be recorded in the report
    :raise AssertionError: if assertion fails
    :return: coordinates of the target
    :platforms: Android, Windows, iOS
    :Example:

        >>> assert_exists(Template(r"tpl1607324047907.png"), "assert exists")

    """
    try:
        pos = loop_find(v,
                        timeout=timeout,
                        threshold=ST.THRESHOLD_STRICT or v.threshold)
        return pos
    except TargetNotFoundError:
        raise TargetNotFoundError("%s does not exist in screen, message: %s" %
                                  (v, msg))
Exemplo n.º 12
0
def assert_lcd_true(start_x, start_y, end_x, end_y, pre, flag=1, val=1):
    img = ImageGrab.grab(bbox=(start_x, start_y, end_x, end_y))
    # 定义每一个数字对应的字段
    DIGITS_LOOKUP = {
        (0, 1, 1, 0, 1, 1, 0): '0凹',
        (1, 1, 1, 0, 1, 1, 0): '0反凹',
        (1, 1, 1, 0, 1, 1, 1): 0,
        (0, 0, 1, 0, 0, 1, 0): 1,
        (1, 0, 1, 1, 1, 0, 1): 2,
        (1, 0, 1, 1, 0, 1, 1): 3,
        (0, 1, 1, 1, 0, 1, 0): 4,
        (1, 1, 0, 1, 0, 1, 1): 5,
        (1, 1, 0, 1, 1, 1, 1): 6,
        (1, 0, 1, 0, 0, 1, 0): 7,
        (1, 1, 1, 1, 1, 1, 1): 8,
        (1, 1, 1, 1, 0, 1, 1): 9,
        (0, 0, 0, 0, 0, 0, 0): '',
        (1, 1, 1, 1, 0, 0, 0): '0之横',
        (1, 1, 1, 0, 0, 0, 0): '0之横'
    }
    # 将输入转换为灰度图片
    gray = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRAY)
    # 使用阈值进行二值化
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)[1]
    non_zero = cv2.countNonZero(thresh)
    # 如果不是黑底白字就转为黑底白字
    if non_zero > img.size * 0.5:
        for i in range(img.shape[0]):
            for j in range(img.shape[1]):
                thresh[i, j] = 255 - thresh[i, j]
    kernel = (3, 3)
    if end_x - start_x < 150:
        kernel = (1, 1)
    median = cv2.medianBlur(thresh, 7)
    close = cv2.dilate(median, kernel, iterations=7)
    # 在阈值图像中查找轮廓,然后初始化数字轮廓列表
    cnts = cv2.findContours(close.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    digitCnts = []

    w_sum = 0.
    h_sum = 0.
    # 循环遍历所有的候选区域
    for c in cnts:
        (x, y, w, h) = cv2.boundingRect(c)
        w_sum += w
        h_sum += h
        digitCnts.append(c)
    # 从左到右对这些轮廓进行排序
    w_ave = w_sum / len(digitCnts)
    h_ave = h_sum / len(digitCnts)
    digitCnts = contours.sort_contours(digitCnts, method="left-to-right")[0]
    digits = ""
    dc = cv2.drawContours(np.array(img).copy(), digitCnts, -1, (0, 0, 255), 2)
    # 循环处理每一个数字
    i = 0
    for index, c in enumerate(digitCnts):
        # 获取ROI区域
        (x, y, w, h) = cv2.boundingRect(c)
        roi = thresh[y:y + h, x:x + w]
        if index is 0 and w > h * 2:
            digits += '-'
        if h / w > 6:
            digits += '1'
            continue
        elif 3 < h / w < 5 and h > (h_ave * 3 / 4):
            digits += '半1'
            continue
        elif w <= (w_ave / 2) and h < (h_ave / 2):
            digits += '.'
            continue
        # 分别计算每一段的宽度和高度
        (roiH, roiW) = roi.shape
        (dW, dH) = (int(roiW * 0.25), int(roiH * 0.15))
        dHC = int(roiH * 0.05)

        # 定义一个7段数码管的集合
        segments = [
            ((0, 0), (w, dH)),  # 上
            ((0, 0), (dW, h // 2)),  # 左上
            ((w - dW, 0), (w, h // 2)),  # 右上
            ((0, (h // 2) - dHC), (w, (h // 2) + dHC)),  # 中间
            ((0, h // 2), (dW, h)),  # 左下
            ((w - dW, h // 2), (w, h)),  # 右下
            ((0, h - dH), (w, h))  # 下
        ]
        on = [0] * len(segments)

        # 循环遍历数码管中的每一段
        for (i, ((xA, yA),
                 (xB,
                  yB))) in enumerate(segments):  # 检测分割后的ROI区域,并统计分割图中的阈值像素点
            segROI = roi[yA:yB, xA:xB]
            total = cv2.countNonZero(segROI)
            area = (xB - xA) * (yB - yA)

            # 如果非零区域的个数大于整个区域的一半,则认为该段是亮的
            if area == 0:
                continue
            if total / float(area) > 0.5:
                on[i] = 1
            if total / float(area) == 1.0:
                on[i] = 0

        # 进行数字查询并显示结果
        digit = DIGITS_LOOKUP[tuple(on)]
        digits += str(digit)
    digits = digits.replace('0凹0反凹', '0').replace(
        '半1半10之横0之横半1半1',
        '0').replace('半1半1', '1').replace('0凹',
                                          '0').replace('0反凹',
                                                       '0').replace('半1', '')
    if digits == pre:
        return
    elif digits in pre and flag == 2:
        return
    elif Levenshtein.distance(pre, digits) < val and flag == 3:
        return
    raise TargetNotFoundError(
        "The result of LCD recognization is{}, not {}".format(digits, pre))