|
@@ -0,0 +1,68 @@
|
|
|
+import pyautogui
|
|
|
+import pytesseract
|
|
|
+import cv2
|
|
|
+import numpy as np
|
|
|
+import re
|
|
|
+import time
|
|
|
+
|
|
|
+# 设置 Tesseract OCR 引擎的路径,根据实际情况修改
|
|
|
+pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
|
|
|
+
|
|
|
+def get_price_from_image(image):
|
|
|
+ """
|
|
|
+ 从图像中提取价格数据
|
|
|
+ :param image: 输入的图像
|
|
|
+ :return: 提取到的价格列表
|
|
|
+ """
|
|
|
+ # 图像预处理:灰度化
|
|
|
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
+ # 使用双边滤波降噪
|
|
|
+ blurred = cv2.bilateralFilter(gray, 9, 75, 75)
|
|
|
+ # 自适应阈值处理
|
|
|
+ binary = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
|
|
|
+
|
|
|
+ # 使用 pytesseract 进行 OCR 识别
|
|
|
+ print("正在识别图片中的文本...")
|
|
|
+
|
|
|
+ text = pytesseract.image_to_string(binary, lang='chi_sim+eng')
|
|
|
+ # 使用正则表达式提取价格数据
|
|
|
+ print(text)
|
|
|
+ prices = re.findall(r'[¥¥]?(\d+(?:[\.|\,]\d+)?)', text)
|
|
|
+ prices = [price for price in prices if '.' in price or ',' in price]
|
|
|
+ return prices
|
|
|
+
|
|
|
+def main():
|
|
|
+ # 给用户一些时间将目标窗口切换到前台
|
|
|
+ print("请在 5 秒内将目标窗口切换到前台...")
|
|
|
+ time.sleep(5)
|
|
|
+
|
|
|
+ # 假设已知要点击的位置,这里以 (500, 500) 为例,可根据实际情况修改
|
|
|
+ # click_x, click_y = 500, 500
|
|
|
+ # # 模拟鼠标点击
|
|
|
+ # pyautogui.click(click_x, click_y)
|
|
|
+ # # 等待页面加载或数据更新
|
|
|
+ # time.sleep(2)
|
|
|
+
|
|
|
+ # 截取屏幕
|
|
|
+ screenshot = pyautogui.screenshot()
|
|
|
+ screenshot = np.array(screenshot)
|
|
|
+ screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR)
|
|
|
+
|
|
|
+ # 将截图保存到本地
|
|
|
+ screenshot_path = '../python/tmp/screenshot.png'
|
|
|
+ cv2.imwrite(screenshot_path, screenshot)
|
|
|
+ print(f"截图已保存到 {screenshot_path}")
|
|
|
+
|
|
|
+ # 从截图中提取价格数据
|
|
|
+ prices = get_price_from_image(screenshot)
|
|
|
+
|
|
|
+ # 输出提取到的价格数据
|
|
|
+ if prices:
|
|
|
+ print("提取到的价格数据:")
|
|
|
+ for price in prices:
|
|
|
+ print(f"价格: {price}")
|
|
|
+ else:
|
|
|
+ print("未提取到价格数据。")
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ main()
|