吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3307|回复: 17
收起左侧

[Python 原创] python 图片识别

  [复制链接]
雪辉 发表于 2022-5-31 13:34
本帖最后由 雪辉 于 2022-6-8 11:14 编辑

10楼有实际操作演示
[Python] 纯文本查看 复制代码
# 案例为本地图库与桌面区域(区域截屏)做图片对比

import time
import os
import cv2 as cv
import numpy as np
from mss import mss

# 1:启用对比图展示
ImgContrast = 1
# 1:启用对比次数提示
DiscernShow = 1
# 对比间隔(毫秒)
DiscernInterval = 50
# 截屏间隔时间(毫秒)
ScreenshotInterval = 200
# 对比图片列表
gun_list = []
# 对比图片地址
imgfile = "image"
# 匹配相似度级别
DiscernLevel = 60
# 截屏范围
box = (1540, 956, 1723, 1026)


# 对比图片特征点
def get_Img_Similarity_Opencv(img1, img2, img1name):
    Matches_good = 0
    Matches_all = 0
    image1 = cv.imread(img1)
    image2 = cv.cvtColor(img2, cv.COLOR_RGB2GRAY)
    ret1, thresh1 = cv.threshold(image1, 250, 255, cv.THRESH_BINARY)
    ret2, thresh2 = cv.threshold(image2, 250, 255, cv.THRESH_BINARY)
    # 创建sift检测器
    sift = cv.SIFT_create()
    # 查找监测点和匹配符
    kp1, des1 = sift.detectAndCompute(thresh1, None)
    kp2, des2 = sift.detectAndCompute(thresh2, None)
    if des1 is None:
        print(img1name + "图片像素成色太低,无法识别")
        return 0, 0
    if des2 is None:
        print("截图区域像素成色太低,无法对比")
        return 0, 0
    try:
        # 获取flann匹配器
        FLANN_INDEX_TREE = 0
        indexParams = dict(algorithm=FLANN_INDEX_TREE, trees=5)
        searchParams = dict(checks=50)
        flann = cv.FlannBasedMatcher(indexParams, searchParams)
        # 进行匹配
        matches = flann.knnMatch(des1, des2, k=2)
        # 准备空的掩膜 画好的匹配项
        if ImgContrast == "1":
            matchesMask = [[0, 0] for i in range(len(matches))]

        for i, (m, n) in enumerate(matches):
            Matches_all += 1
            if m.distance < 0.7 * n.distance:
                if ImgContrast == "1":
                    matchesMask[i] = [1, 0]
                Matches_good += 1

        if ImgContrast == "1":
            drawPrams = dict(matchColor=(0, 255, 0), singlePointColor=(255, 0, 0), matchesMask=matchesMask, flags=0)
            img3 = cv.drawMatchesKnn(thresh1, kp1, thresh2, kp2, matches, None, **drawPrams)
            cv.imshow("matches", img3)
            cv.waitKey(0)

        if DiscernShow == "1":
            print(img1name + "匹配成功次数:" + str(Matches_good) + "    总匹配次数:" + str(Matches_all))
        time.sleep(DiscernInterval)
    except:
        print("对比图片特征点异常\n\n")
        return 0, 0
    else:
        return Matches_good, Matches_all


# 匹配图片
def get_Img_Similarity(im):
    for gun_name in gun_list:
        result, result_all = get_Img_Similarity_Opencv(r""+imgfile+"\\" + gun_name + ".png", im, gun_name)
        if result == 0 and result_all == 0:
            print("对比失败")
            return
        if result >= int(result_all * DiscernLevel) and (result != 0 or result_all != 0):
            print("对比成功")
            return
        else:
            print("对比失败")
            return


# 获取桌面截屏区域图片
def get_ScreenShot(box_img):
    try:
        with mss() as sct:
            shot = sct.grab(box_img)
            return shot
    except:
        print("截图范围错误")


# 获取桌面截屏匹配
def get_Screen():
    img = get_ScreenShot(box)
    arr = np.array(img.pixels, dtype=np.uint8)
    get_Img_Similarity(arr)
    time.sleep(ScreenshotInterval)


# 获取需要对比的图片信息
def get_ImgList(img_dir):
    for files in os.listdir(img_dir):
        if os.path.splitext(files)[1] == '.png':
            gun_list.append(os.path.splitext(files)[0])


def main():
    get_ImgList(imgfile)
    while True:
        cv.destroyAllWindows()
        get_Screen()

if __name__ == '__main__':
    main()

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

zhcj66 发表于 2022-5-31 14:04
[Python] 纯文本查看 复制代码
import time
import os
import cv2 as cv
import numpy as np
from mss import mss

# 1:启用对比图展示
ImgContrast = 1
# 1:启用对比次数提示
DiscernShow = 1
# 对比间隔(毫秒)
DiscernInterval = 50
# 截屏间隔时间(毫秒)
ScreenshotInterval = 200
# 对比图片列表
gun_list = []
# 对比图片地址
imgfile = "image"
# 匹配相似度级别
DiscernLevel = 60
# 截屏范围
box = (1540, 956, 1723, 1026)


# 对比图片特征点
def get_Img_Similarity_Opencv(img1, img2, img1name):
    Matches_good = 0
    Matches_all = 0
    image1 = cv.imread(img1)
    image2 = cv.cvtColor(img2, cv.COLOR_RGB2GRAY)
    ret1, thresh1 = cv.threshold(image1, 250, 255, cv.THRESH_BINARY)
    ret2, thresh2 = cv.threshold(image2, 250, 255, cv.THRESH_BINARY)
    # 创建sift检测器
    sift = cv.SIFT_create()
    # 查找监测点和匹配符
    kp1, des1 = sift.detectAndCompute(thresh1, None)
    kp2, des2 = sift.detectAndCompute(thresh2, None)
    if des1 is None:
        print(img1name + "图片像素成色太低,请使用截图保存武器功能 获取正确的武器图片")
        return 0, 0
    if des2 is None:
        print("截图区域像素成色太低,无法对比")
        return 0, 0
    try:
        # 获取flann匹配器
        FLANN_INDEX_TREE = 0
        indexParams = dict(algorithm=FLANN_INDEX_TREE, trees=5)
        searchParams = dict(checks=50)
        flann = cv.FlannBasedMatcher(indexParams, searchParams)
        # 进行匹配
        matches = flann.knnMatch(des1, des2, k=2)
        # 准备空的掩膜 画好的匹配项
        if ImgContrast == "1":
            matchesMask = [[0, 0] for i in range(len(matches))]

        for i, (m, n) in enumerate(matches):
            Matches_all += 1
            if m.distance < 0.7 * n.distance:
                if ImgContrast == "1":
                    matchesMask = [1, 0]
                Matches_good += 1

        if ImgContrast == "1":
            drawPrams = dict(matchColor=(0, 255, 0), singlePointColor=(255, 0, 0), matchesMask=matchesMask, flags=0)
            img3 = cv.drawMatchesKnn(thresh1, kp1, thresh2, kp2, matches, None, **drawPrams)
            cv.imshow("matches", img3)
            cv.waitKey(0)

        if DiscernShow == "1":
            print(img1name + "匹配成功次数:" + str(Matches_good) + "    总匹配次数:" + str(Matches_all))
        time.sleep(DiscernInterval)
    except:
        print("对比图片特征点异常\n\n")
        return 0, 0
    else:
        return Matches_good, Matches_all


# 匹配图片
def get_Img_Similarity(im):
    for gun_name in gun_list:
        result, result_all = get_Img_Similarity_Opencv(r""+imgfile+"\\" + gun_name + ".png", im, gun_name)
        if result == 0 and result_all == 0:
            print("对比失败")
            return
        if result >= int(result_all * DiscernLevel) and (result != 0 or result_all != 0):
            print("对比成功")
            return
        else:
            print("对比失败")
            return


# 获取桌面截屏区域图片
def get_ScreenShot(box_img):
    try:
        with mss() as sct:
            shot = sct.grab(box_img)
            return shot
    except:
        print("截图范围错误")


# 获取桌面截屏匹配
def get_Screen():
    img = get_ScreenShot(box)
    arr = np.array(img.pixels, dtype=np.uint8)
    get_Img_Similarity(arr)
    time.sleep(ScreenshotInterval)


# 获取需要对比的图片信息
def get_ImgList(img_dir):
    for files in os.listdir(img_dir):
        if os.path.splitext(files)[1] == '.png':
            gun_list.append(os.path.splitext(files)[0])


def main():
    get_ImgList(imgfile)
    while True:
        cv.destroyAllWindows()
        get_Screen()

main()
 楼主| 雪辉 发表于 2022-6-6 20:39
盛世美颜贾队长 发表于 2022-6-2 16:55
请问主要用于识别什么的呢?感觉比较轻量级

比如10楼,就是我写个一个游戏的武器识别效果图。其他任何有需要用到2个图片对比的都能用得到吧
hnlrzy 发表于 2022-5-31 16:01
你是我的人 发表于 2022-5-31 16:38
感谢分享
huaxiaoron 发表于 2022-5-31 17:03
学习了,感谢分享
盛世美颜贾队长 发表于 2022-6-2 16:55
请问主要用于识别什么的呢?感觉比较轻量级
minblack3 发表于 2022-6-3 21:14
收藏了,楼主有成品图吗
 楼主| 雪辉 发表于 2022-6-6 13:59
minblack3 发表于 2022-6-3 21:14
收藏了,楼主有成品图吗

成品图指什么?实际作品?
minblack3 发表于 2022-6-6 18:37
雪辉 发表于 2022-6-6 13:59
成品图指什么?实际作品?

呃不是,就是实际运行的时候的效果图
 楼主| 雪辉 发表于 2022-6-6 20:38
minblack3 发表于 2022-6-6 18:37
呃不是,就是实际运行的时候的效果图

这是一个识别武器的实际效果图
1.png
2.png
3.png
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 05:29

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表