我正在尝试编写一段代码来读取游戏日志并根据其内容发送消息以进行不和谐。到目前为止,代码运行良好,但我在 OCR 方面遇到了问题。有时,其准确性较差会导致消息发送次数超过 1 次。我需要 OCR 尽可能准确,以避免消息重复。我研究并发现在尝试 OCR 之前进行二值化/阈值处理以在白色背景上获取黑色文本,尽管这只会使结果变得更糟。在这种情况下我可以使用什么策略来提高其准确性?日志如下所示:
代码如下:
import cv2
import numpy as np
import pytesseract
import time
import requests
import json
import pyautogui
# Discord webhook URL
webhook_url = "https://discord.com/api/webhooks/1111838770568888401/GJeNJ9YrMgq6_Mn3XMcX2JQlvmlZsmGhNLp1sNSRpu1RxRXEwkctIUe_iRanurRz1pS8"
# Phrases to monitor and their corresponding Discord messages
target_phrases = {
"was killed": "<@&1184910793871994920> Day{message}",
"Tamed": "<@&1184962484675809340> Day{message}",
}
# OCR configuration
tesseract_path = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
pytesseract.pytesseract.tesseract_cmd = tesseract_path
# Time interval between OCR checks (in seconds)
check_interval = 5
# Set to store previous occurrences of the target phrases
previous_occurrences = set()
# Define the screen region where the list is located
list_region = (750, 198, 400, 400) # Replace with the coordinates of the list region
# Function to send a message to Discord webhook
def send_discord_message(message):
data = {"content": message}
headers = {"Content-Type": "application/json"}
response = requests.post(webhook_url, data=json.dumps(data), headers=headers)
if response.status_code != 204:
print("Failed to send Discord message:", response.text)
else:
print("Discord message sent successfully")
# Main loop
while True:
# Take a screenshot of the defined screen region
screenshot = pyautogui.screenshot(region=list_region)
screenshot = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)
# Apply OCR to extract text from the screenshot
extracted_text = pytesseract.image_to_string(screenshot)
print("Extracted Text:\n", extracted_text)
# Split the extracted text into messages based on the occurrence of "Day"
messages = extracted_text.strip().split('Day')
# Process each message individually
for message in messages:
# Check if any target phrase appears in the message
for target_phrase, discord_message in target_phrases.items():
if target_phrase.lower() in message.lower() and message.strip() not in previous_occurrences:
# Send a message for the new occurrence
print(f"Phrase '{target_phrase}' detected")
send_discord_message(discord_message.format(message=message))
# Update the set of previous occurrences
previous_occurrences.add(message.strip())
# Wait for the next OCR check
time.sleep(check_interval)
我们可以通过以下两种方式来提高ocr输出结果的准确性。
请找到下面的 tessdata 链接,