News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

2024-11(6)

Xiao Yuan oral calculation python

posted on 2024-11-02 14:00     read(604)     comment(0)     like(12)     collect(1)


Pyhton pure visual solution, capture screenshots according to the simulator position for comparison, for two screenshots, change the position by yourself, need to download pytesseract ocr to the local machine, and also need to change the capture screen area, go area, only for learning, speed up this solution is not feasible
  1. import cv2
  2. import pytesseract
  3. import pyautogui
  4. import numpy as np
  5. import time
  6. from concurrent.futures import ThreadPoolExecutor
  7. # Tesseract OCR路径配置
  8. pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
  9. def capture_screenshot(region=None):
  10. # 捕捉屏幕截图
  11. screenshot = pyautogui.screenshot(region=region) # 捕捉指定区域
  12. img = np.array(screenshot) # 转换为numpy数组
  13. img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # 转换为BGR格式
  14. return img
  15. def extract_text_from_image(img):
  16. # 图像中提取文本
  17. custom_config = r'--oem 3 --psm 6' # Tesseract配置
  18. text = pytesseract.image_to_string(img, config=custom_config)
  19. print("识别出的文本:", text) # 调试输出
  20. return text.strip()
  21. def wait_for_go(region):
  22. # 等待识别到go字样后继续执行
  23. print("等待识别到 'go' 字样...")
  24. while True:
  25. img = capture_screenshot(region)
  26. text = extract_text_from_image(img)
  27. if 'go' in text.lower(): # 如果识别到 "go",开始执行主程序
  28. print("'go' 已识别,开始执行主程序")
  29. break
  30. time.sleep(0.1) # 每秒检查一次
  31. def extract_number_from_image(img):
  32. # 从图像中提取数字
  33. custom_config = r'--oem 3 --psm 6 outputbase digits' # Tesseract配置
  34. details = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT, config=custom_config)
  35. # 提取识别出的文本信息
  36. numbers = []
  37. for i, text in enumerate(details['text']):
  38. if text.isdigit(): # 只提取数字
  39. numbers.append(int(text))
  40. print("提取的数字:", numbers) # 调试输出
  41. return numbers
  42. def draw_comparison_sign(result, start_position):
  43. # 模拟鼠标滑动绘制比较符号
  44. pyautogui.moveTo(start_position[0], start_position[1]) # 移动到起始位置
  45. # 绘制符号
  46. if result == ">":
  47. # 绘制大于号
  48. pyautogui.dragTo(210, 710, button='left', duration=0.00001) # 上半部分
  49. pyautogui.dragTo(200, 720, button='left', duration=0.00001) # 下半部分
  50. elif result == "<":
  51. # 绘制小于号
  52. pyautogui.dragTo(190, 710, button='left', duration=0.00001) # 上半部分
  53. pyautogui.dragTo(200, 720, button='left', duration=0.00001) # 下半部分
  54. def process_image(region):
  55. # 捕捉图像并提取数字
  56. img = capture_screenshot(region)
  57. numbers = extract_number_from_image(img)
  58. return numbers
  59. def main(region1, region2):
  60. #主循环 捕捉、识别、比较并绘制符号
  61. iterations = 13 # 设定循环次数
  62. with ThreadPoolExecutor(max_workers=2) as executor: # 创建线程池,设置2个并发工作线程
  63. for i in range(iterations):
  64. # 使用线程并行处理两个图像捕捉与数字提取
  65. future1 = executor.submit(process_image, region1)
  66. future2 = executor.submit(process_image, region2)
  67. # 获取提取结果
  68. numbers1 = future1.result()
  69. numbers2 = future2.result()
  70. if len(numbers1) > 0 and len(numbers2) > 0:
  71. num1 = numbers1[0] # 假设第一张图像提取第一个数字
  72. num2 = numbers2[0] # 假设第二张图像提取第一个数字
  73. print(f"第一张识别到数字: {num1},第二张识别到数字: {num2}")
  74. # 比较数字并绘制符号
  75. if num1 > num2:
  76. draw_comparison_sign(">", (200, 700)) # 在指定位置绘制大于号
  77. elif num1 < num2:
  78. draw_comparison_sign("<", (200, 700)) # 在指定位置绘制小于号
  79. else:
  80. print("两个数字相等,无需绘制符号。")
  81. time.sleep(0.3) # 暂停0.5秒再进行下一次截图
  82. # 示例:定义捕捉屏幕的区域
  83. region1 = (100, 300, 100, 100) # 第一张图像捕捉区域 (x, y, width, height)
  84. region2 = (290, 310, 100, 100) # 第二张图像捕捉区域 (x, y, width, height)
  85. # 定义 'go' 字样的区域
  86. go_region = (190, 425, 120, 65) # 假设 'go' 字样出现在这个区域 (x, y, width, height)
  87. # 等待识别到 'go' 字样后启动主程序
  88. wait_for_go(go_region)
  89. # 启动主程序
  90. try:
  91. main(region1, region2)
  92. except KeyboardInterrupt:
  93. print("程序已停止")
  94. except Exception as e:
  95. print("发生错误:", e)



Category of website: technical article > Blog

Author:cindy

link:http://www.pythonblackhole.com/blog/article/245801/b412d6e1cc06488df5fc/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

12 0
collect article
collected

Comment content: (supports up to 255 characters)