| import numpy as np |
| import cv2 |
| from PIL import ImageGrab |
| from demo import get_XY, classify_hist_with_split |
| import time |
| |
| cap = cv2.VideoCapture(0) |
| |
| |
| def find_picture(target, template): |
| theight, twidth = template.shape[:2] |
| result = cv2.matchTemplate(target, template, cv2.TM_SQDIFF_NORMED) |
| cv2.normalize(result, result, 0, 1, cv2.NORM_MINMAX, -1) |
| min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result) |
| cv2.rectangle(target, min_loc, (min_loc[0] + twidth, min_loc[1] + theight), (0, 0, 225), 2) |
| x = min_loc[0] |
| y = min_loc[1] |
| |
| return x, y |
| |
| |
| def get_xy(A, base='image.jpg'): |
| """获取A在B的坐标, 反返回A的高和宽""" |
| target = cv2.imread(A) |
| w, h, = target.shape[0], target.shape[1] |
| if base == 'image.jpg': |
| template = cv2.imread(base) |
| else: |
| template = base |
| x, y = find_picture(target, template) |
| print(A, "的坐标", x, y, h, w) |
| return x, y, w, h |
| |
| |
| def get_pic_from_pic(x, y, w, h, target): |
| region = target[y:y + h, x:x + w] |
| return region |
| |
| |
| if __name__ == '__main__': |
| |
| |
| |
| |
| while True: |
| screen = ImageGrab.grab(bbox=(0, 0, 800, 600)) |
| print(screen) |
| frame = cv2.cvtColor(np.array(screen), cv2.COLOR_RGB2BGR) |
| |
| x, y, h, w = get_xy("yanjing.jpg", frame) |
| human_box = [x, y, x + w, h + y] |
| |
| |
| |
| cv2.rectangle(frame, (human_box[0], human_box[1]), (human_box[2], human_box[3]), (255, 255, 0), 3) |
| |
| cv2.imshow("output_path", frame) |
| cv2.waitKey(25) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?