You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检测全屏截图中是否存在指定图像?含需求与过往实现参考

Hey there! Let's build on the image detection logic you used for Diamond Dash—this problem is a perfect fit for template matching, which aligns exactly with what you were doing back then. Here's a practical, Python-based solution to detect that yellow success bar in your Facebook post screenshots and trigger the right Telegram notifications:

解决方案:模板匹配检测 + Telegram通知

核心逻辑梳理

Your old script focused on "find single diamond → check surrounding → click". For this task, we can simplify it to "find fixed yellow bar → send corresponding Telegram alert"—since the bar hasn't changed in 5 years, it's a rock-solid success marker.


具体实现方案

I'll share two options based on your needs:

Option 1: PyAutoGUI (Quick & Simple, Fixed Resolution)

If your screenshots always have the same resolution, PyAutoGUI is super straightforward (you might already be familiar with it from the Diamond Dash days):

  1. Prepare your template: Take a clear screenshot of the yellow success bar and save it as success_bar.png—make sure it matches the resolution of your post-screenshots.
  2. Code implementation:
import pyautogui
import requests

# Fill in your Telegram details
TELEGRAM_BOT_TOKEN = "your_bot_token_here"
TELEGRAM_CHAT_ID = "your_chat_id_here"

def send_telegram_msg(text):
    url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
    payload = {"chat_id": TELEGRAM_CHAT_ID, "text": text}
    requests.post(url, data=payload)

def send_telegram_screenshot(screenshot_path):
    url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendPhoto"
    with open(screenshot_path, "rb") as img_file:
        files = {"photo": img_file}
        payload = {"chat_id": TELEGRAM_CHAT_ID, "caption": "Post Status Screenshot"}
        requests.post(url, data=payload, files=files)

def check_post_success(screenshot_path, template_path):
    try:
        # Look for the template in the screenshot (confidence adjusts strictness)
        match_location = pyautogui.locateOnScreen(template_path, confidence=0.8)
        return match_location is not None
    except pyautogui.ImageNotFoundException:
        return False

# Main workflow
screenshot_file = "facebook_post_screenshot.png"  # Path to your full-screen screenshot
success_template = "success_bar.png"

if check_post_success(screenshot_file, success_template):
    send_telegram_msg("✅ Your content was published successfully!")
    send_telegram_screenshot(screenshot_file)
else:
    send_telegram_msg("❌ Content publish failed—please check the process!")
    send_telegram_screenshot(screenshot_file)

Install dependencies first: pip install pyautogui requests opencv-python (the confidence parameter needs OpenCV under the hood)


Option 2: OpenCV (More Accurate, Variable Resolution)

If your screenshots might have varying resolutions, OpenCV's multi-scale template matching handles this better:

import cv2
import numpy as np
import requests

# Fill in your Telegram details
TELEGRAM_BOT_TOKEN = "your_bot_token_here"
TELEGRAM_CHAT_ID = "your_chat_id_here"

def send_telegram_msg(text):
    url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
    payload = {"chat_id": TELEGRAM_CHAT_ID, "text": text}
    requests.post(url, data=payload)

def send_telegram_screenshot(screenshot_path):
    url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendPhoto"
    with open(screenshot_path, "rb") as img_file:
        files = {"photo": img_file}
        payload = {"chat_id": TELEGRAM_CHAT_ID, "caption": "Post Status Screenshot"}
        requests.post(url, data=payload, files=files)

def check_success_with_opencv(screenshot_path, template_path):
    # Load images
    main_img = cv2.imread(screenshot_path)
    template_img = cv2.imread(template_path)
    template_h, template_w = template_img.shape[:2]

    # Search for template across different scales
    best_match = None
    for scale in np.linspace(0.8, 1.2, 20)[::-1]:
        resized_img = cv2.resize(main_img, (int(main_img.shape[1] * scale), int(main_img.shape[0] * scale)))
        scale_ratio = main_img.shape[1] / float(resized_img.shape[1])
        
        # Stop if resized image is smaller than template
        if resized_img.shape[0] < template_h or resized_img.shape[1] < template_w:
            break
        
        # Run template matching
        match_result = cv2.matchTemplate(resized_img, template_img, cv2.TM_CCOEFF_NORMED)
        _, max_match_val, _, max_match_loc = cv2.minMaxLoc(match_result)
        
        # Update best match if current is better
        if best_match is None or max_match_val > best_match[0]:
            best_match = (max_match_val, max_match_loc, scale_ratio)

    # Return True if match confidence is high enough
    return best_match is not None and best_match[0] >= 0.8

# Main workflow
screenshot_file = "facebook_post_screenshot.png"
success_template = "success_bar.png"

if check_success_with_opencv(screenshot_file, success_template):
    send_telegram_msg("✅ Your content was published successfully!")
    send_telegram_screenshot(screenshot_file)
else:
    send_telegram_msg("❌ Content publish failed—please check the process!")
    send_telegram_screenshot(screenshot_file)

Install dependencies: pip install opencv-python numpy requests


Key Tips for Reliability

  • Perfect your template: Capture the entire yellow bar without extra surrounding elements—this drastically reduces false matches.
  • Tweak the confidence threshold: Adjust the 0.8 value in both scripts. Higher = stricter matching (less false positives), lower = more lenient (catches slightly distorted bars).
  • Test with edge cases: Run a few test runs with failed post screenshots to make sure the script doesn't incorrectly flag success.

内容的提问来源于stack exchange,提问作者Geppettvs D'Constanzo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:02:36