Exemplo n.º 1
0
def main():
    pro_start = datetime.datetime.now()
    check_os()
    config = get_config()
    is_auto = config['auto']

    while True:
        while True:
            img = analyze.tell_and_get_image()
            if img is not None:
                break
            if is_auto:
                print('没有发现题目页面')
                exit(-1)
            time.sleep(1)  # 不是题目页面,休眠1秒后继续判断
        # 获取题目及选项
        start = datetime.datetime.now()  # 开始时间
        question, option_arr = analyze.image_to_str(img)  # 图片转文字
        question, is_negative = analyze.get_question(question)  # 得到题目、选项及题目正反
        result_list = search.search(question)  # 搜索结果
        print question
        best_result = analyze.get_result(result_list, option_arr, question,
                                         is_negative)  # 分析结果
        if best_result is None:
            print('\n没有答案')
        else:
            print('最佳答案是: \033[1;31m{}\033[0m'.format(best_result))
        run_time = (datetime.datetime.now() - start).seconds
        print('本次运行时间为:{}秒'.format(run_time))
        if is_auto:
            time.sleep(10)  # 每一道题结束,休息10秒
        else:
            break
    total_time = (datetime.datetime.now() - pro_start).seconds
    print('脚本运行共运行{}秒'.format(total_time))
Exemplo n.º 2
0
def main_start():

    # 获取屏幕尺寸
    size = tools.check_os()
    print u'当前使用手机的尺寸为:' + str(size)

    # 获取配置文件
    config = tools.get_config()
    is_auto = config['auto']
    is_yd_ocr = config['yd_ocr']
    # is_debug = config['debug']
    my_str = config['my_str']

    # 根据尺寸及配置文件,获取到当前问题区和空白区
    pixel_json = tools.get_pixel_config(size)
    blank_area = pixel_json['blank_area']
    # question_area = pixel_json['question_area']
    blank_area_point = blank_area['x1'], blank_area['y1'], blank_area[
        'x2'], blank_area['y2']
    # question_area_point = question_area['x1'], question_area['y1'], question_area['x2'], question_area['y2']

    # 循环判断是否为答题页面
    question_num = 0
    crop_img_name = 'image/now.png'
    while True:
        while False:
            img = analyze.tell_and_get_image(is_auto, blank_area_point)
            if img is not None:
                question_num += 1
                break
            else:  # 若不是答题页
                if not is_auto:
                    print('没有发现题目页面')
                    exit(-1)
                print('没有发现答题页面,继续')
                time.sleep(0.8)  # 不是题目页面,休眠0.8秒后继续判断

        img = Image.open('image/backup.png')

        # 获取题目及选项
        start = datetime.datetime.now()  # 记录开始时间
        print u'开始时间为:' + str(start)
        tools.crop_image(img, crop_img_name)
        question, a1, a2, a3 = analyze.image_to_str(crop_img_name, is_yd_ocr,
                                                    my_str)  # 图片转文字
        if question is None or question == '':
            print('\n没有识别题目')
            exit(-1)
        result_list = search.geturl(question, a1, a2, a3)  # 搜索结果
        for x in result_list:
            print x

        run_time = (datetime.datetime.now() - start).seconds
        print('本次运行时间为:{}秒'.format(run_time))
        if is_auto:
            time.sleep(25)  # 每一道题结束,休息10秒
        else:
            break
Exemplo n.º 3
0
def main():
    img = analyze.tell_and_get_image()
    question, option_arr = analyze.image_to_str(img)  # 图片转文字
    question, is_negative = analyze.get_question(question)  # 得到题目、选项及题目正反
    result_list = search.search(question)  # 搜索结果
    print
    print question
    print "选项:", ','.join(option_arr)
    best_result = analyze.get_result(result_list, option_arr, question,
                                     is_negative)  # 分析结果
    if best_result is None:
        print('\n没有答案')
    else:
        print('最佳答案是: \033[1;31m{}\033[0m'.format(best_result))
Exemplo n.º 4
0
def main():
    text = analyze.image_to_str()  # 图片转文字
    question, option_arr, is_negative = analyze.get_question(
        text)  # 得到题目、选项及题目正反
    result_list = []

    wd = urllib.request.quote(question)
    url = 'https://zhidao.baidu.com/search?ct=17&pn=0&tn=ikaslist&rn=10&fr=wwwt&word={}'.format(
        wd)

    # open chrome to get answer if you use mac..
    chrome_path = 'open -a /Applications/Google\ Chrome.app %s'
    if is_windows():
        # TODO replace your local chrome install path
        chrome_path = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'

    if is_linux():
        chrome_path = '/usr/bin/google-chrome %s'

    webbrowser.get(chrome_path).open(url)

    print(url)
    result = urlopen(url)
    # 解析html页面
    body = BeautifulSoup(result.read(), 'html5lib')
    good_result_div = body.find(class_='list-header').find(class_='dd answer')
    second_result_div = body.find(class_='list-inner').find(class_='list')
    if good_result_div is not None:
        good_result = good_result_div.get_text()
        result_list.append(good_result)
        print(good_result.strip())

    if second_result_div is not None:
        second_result_10 = second_result_div.findAll(
            'dl')  # .find(class_='answer').get_text()
        if second_result_10 is not None and len(second_result_10) > 0:
            for index, each_result in enumerate(second_result_10):
                result_dd = each_result.dd.get_text()
                result_list.append(result_dd)
                if index < 3:
                    print(result_dd)

    best_result = analyze.get_result(result_list, option_arr, question,
                                     is_negative)
    if best_result is None:
        print('\n没有答案')
    else:
        print('最佳答案是: \033[1;31m{}\033[0m'.format(best_result))
Exemplo n.º 5
0
def main():
    size = utils.check_os()
    config = get_config()
    is_auto = config['auto']
    is_baidu_ocr = config['baidu_ocr']
    is_debug = config['debug']
    baidu_ocr_clint = None
    if is_baidu_ocr:
        baidu_cor_config = config['baidu_ocr_config']
        baidu_ocr_clint = utils.init_baidu_ocr(baidu_cor_config)
    pixel_json = utils.get_pixel_config(size)
    blank_area = pixel_json['blank_area']
    question_area = pixel_json['question_area']

    blank_area_point = blank_area['x1'], blank_area['y1'], blank_area['x2'], blank_area['y2']
    question_area_point = question_area['x1'], question_area['y1'], question_area['x2'], question_area['y2']

    question_num = 0
    crop_img_name = 'image/crop.png'

    while True:
        while True:
            print('开始答题')
            img = analyze.tell_and_get_image(is_auto, blank_area_point)
            if img is not None:
                question_num += 1
                break
            else:  # 若不是答题页
                if not is_auto:
                    print('没有发现题目页面')
                    exit(-1)
                print('没有发现答题页面,继续')
                time.sleep(0.8)  # 不是题目页面,休眠0.8秒后继续判断

        # 获取题目及选项
        start = datetime.datetime.now()  # 记录开始时间
        crop_obj = utils.crop_image(img, question_area_point, crop_img_name)
        question, option_arr, is_negative = analyze.image_to_str(crop_obj, is_baidu_ocr, baidu_ocr_clint)  # 图片转文字
        print('搜索的题目是:{} '.format(question))
        print('选项为:{} '.format(option_arr))
        if question is None or question == '':
            print('没有识别题目')
            continue
        result_list = search.search(question, option_arr, is_negative)  # 搜索结果

        if result_list is None:
            print('\n没有答案')
        else:
            print('最佳答案是: \033[1;31m{}\033[0m'.format(result_list))
        run_time = (datetime.datetime.now() - start).seconds
        print('本次运行时间为:{}秒'.format(run_time))
        crop_img = crop_obj[0]
        if is_debug:
            img.save('image/question_{}.png'.format(question_num))
        crop_img.close()
        img.close()
        if is_auto:
            print('休息:{}秒后继续'.format(question_sleep_time))
            time.sleep(question_sleep_time)  # 每一道题结束,休息10秒
        else:
            break
Exemplo n.º 6
0
def main():
    pro_start = datetime.datetime.now()
    size = utils.check_os()
    config = get_config()
    is_auto = config['auto']
    is_baidu_ocr = config['baidu_ocr']
    baidu_ocr_clint = None
    if is_baidu_ocr:
        baidu_cor_config = config['baidu_ocr_config']
        baidu_ocr_clint = utils.init_baidu_ocr(baidu_cor_config)
    pixel_json = utils.get_pixel_config(size)
    blank_area = pixel_json['blank_area']
    question_area = pixel_json['question_area']

    blank_area_point = blank_area['x1'], blank_area['y1'], blank_area[
        'x2'], blank_area['y2']
    question_area_point = question_area['x1'], question_area[
        'y1'], question_area['x2'], question_area['y2']

    question_num = 0
    crop_img_name = 'image/crop.png'

    while True:
        while True:
            img = analyze.tell_and_get_image(is_auto, blank_area_point)
            if img is not None:
                question_num += 1
                break
            else:  # 若不是答题页
                if not is_auto:
                    print('没有发现题目页面')
                    exit(-1)
                print('没有发现答题页面,继续')
                time.sleep(0.5)  # 不是题目页面,休眠0.5秒后继续判断

        # 获取题目及选项
        start = datetime.datetime.now()  # 记录开始时间
        crop_img = utils.crop_image(img, question_area_point, crop_img_name)
        question, option_arr, is_negative = analyze.image_to_str(
            crop_img, is_baidu_ocr, baidu_ocr_clint)  # 图片转文字
        if question is None or question == '':
            print('\n没有识别题目')
            exit(-1)
        result_list = search.search(question)  # 搜索结果

        best_result = search.get_result(result_list, option_arr, question,
                                        is_negative)  # 分析结果
        if best_result is None:
            print('\n没有答案')
        else:
            print('最佳答案是: \033[1;31m{}\033[0m'.format(best_result))
        run_time = (datetime.datetime.now() - start).seconds
        print('本次运行时间为:{}秒'.format(run_time))
        if is_auto:
            time.sleep(10)  # 每一道题结束,休息10秒
        else:
            break
    total_time = (datetime.datetime.now() - pro_start).seconds
    print('脚本运行共运行{}秒'.format(total_time))
    img.save('image/answer{}.png'.format(question_num))
    img.close()