def test_shot(): size = utils.check_os() 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'] backup_img = None if os.path.exists('image/backup.png'): backup_img = Image.open('image/backup.png') else: print('image/backup.png位置图片不存在') exit(-1) utils.crop_image(backup_img, question_area_point, 'image/crop_test.png') utils.crop_image(backup_img, blank_area_point, 'image/blank_test.png')
def test_shot(is_ios): size = utils.check_os(is_ios) 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'] backup_img = None if os.path.exists('image/backup.png'): backup_img = Image.open('image/backup.png') else: utils.pull_from_screen_ios() backup_img = Image.open('image/backup.png') utils.crop_image(backup_img, question_area_point, 'image/crop_test.png') utils.crop_image(backup_img, blank_area_point, 'image/blank_test.png')
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
def test_get_pixel(): size = 1440, 2560 config = utils.get_pixel_config(size) print(config)
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()