日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

图像识别从零写出dnf脚本关键要点

發(fā)布時間:2024/8/1 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图像识别从零写出dnf脚本关键要点 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

思路:

  • 知道游戲角色在哪個坐標
  • 知道怪物和門在哪個坐標
  • 驅(qū)動級鍵鼠操作讓角色走到怪物坐標然后攻擊釋放技能。
  • 滿足第一和第二條就要求必須實時讀入圖像以及能夠識別標志性圖像然后給出坐標。

    實時讀入圖像

    沒精力玩python了,還是好好學Java吧

    廢話不多說直接上代碼

    import cv2 from PIL import ImageGrab import numpy as npwhile True:im = ImageGrab.grab()imm = cv2.cvtColor(np.array(im), cv2.COLOR_RGB2BGR)imm = imm[0:500, 0:500]imm = cv2.resize(imm, None, fx=0.5, fy=0.5)cv2.imshow("capture", imm)if cv2.waitKey(1) & 0xFF == ord('q'): # q鍵推出break cv2.destroyAllWindows()

    圖像識別

    我使用的是yolov5。

    python版本是:3.10.5

    前邊簡單說一下吧,省的以后我用了再忘記了

    前置工作

  • 用pycharm創(chuàng)建虛擬環(huán)境
  • 在github把項目克隆下來GitHub - ultralytics/yolov5: YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
  • 下載依賴(項目里的requirements.txt):pip install -r requirements.txt
  • 在克隆下載的目錄級創(chuàng)建這樣一個目錄,其實創(chuàng)建在哪都無所謂,就是方便(作用就是放圖片,放標簽,放配置)
  • 開始截圖(把截下來的圖都放在images里)
  • 開始標注(這里我用的是labelImg)
  • 訓練
  • 預測
  • ?

    6.標注

    python版本:3.7.8(高版本可能不兼容,但我忘記哪里不兼容了,降低就對了)

    從github下載克隆GitHub - heartexlabs/labelImg: 🖍? LabelImg is a graphical image annotation tool and label object bounding boxes in images

    ?安裝:pip install PyQt5

    安裝:pip install lxml

    進入克隆目錄執(zhí)行:pyrcc5 -o resources.py resources.qrc

    把resource.py放入libs目錄

    執(zhí)行:python?labelImg.py 后啟動窗口

    ?我們直接打開目錄(指的是你的圖片存放目錄),然后指定改變存放目錄(指的是你的label目錄),重要的是:別忘了改為yolo。標注技巧:w是標注,d是下一張

    ?

    ?然后現(xiàn)在你的

    ?這個目錄應該是有東西的(除了yaml文件還沒寫)

    7.訓練

    多么痛的領悟,我用我電腦訓練了一天也沒訓練完。

    我選擇使用?colab去訓練(需要谷歌賬號,需要翻墻)

    Colaboratory 簡稱“Colab”,Google Research 團隊開發(fā),任何人都可以通過瀏覽器編寫和執(zhí)行任意 Python 代碼,尤其適合機器學習、數(shù)據(jù)分析、教育目的。Colab 是一種托管式 Jupyter 筆記本服務,用戶無需設置,就可直接使用,還能免費使用 GPU/TPU計算資源。

    ?我賬號異常了,沒辦法截圖細說了:Colaboratory( 簡稱"Colab")介紹與使用 - 知乎

    簡單來說就是創(chuàng)建?Colaboratory,然后掛載文件,把yolo_A打包放上去,然后在上邊解壓縮

    ,搭建yolov5環(huán)境,選擇GPU之后,可以像在本地一樣訓練模型。

    ?訓練的時候需要用到上邊的A.yaml.案例寫法

    # train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/] train: ../yolo_A/images/ val: ../yolo_A/images/ # number of classes 類型的數(shù)量,幾個類型名字就填幾 nc: 1# class names 類型的名字,可以有多個 names: ['be']

    ?訓練模型代碼實例:yolov5s.pt可以在Releases · ultralytics/yolov5 · GitHub找到并下載

    ?python train.py --img 640 --batch 54 --epochs 100 --data A.yaml --weights yolov5s.pt --nosave --cache

    訓練完的文件我們需要best.pt,在yolov5-master\runs\train里邊。直接拿到本地使用。

    8.預測

    因為我需要拿到預測后的結(jié)果,以及我傳進去的是cv2.imread()后的numpy數(shù)組,所以我對detect源碼文件的run函數(shù)做了精簡和修改。修改后的文件比較冗余,因為我只是玩玩,所以就沒浪費時間整理代碼,大家參考即可。新增的imMy參數(shù)就是傳入的數(shù)組,source沒啥用,但是你得傳入一個本地存在的圖片路徑(這個冗余,沒修改)

    # YOLOv5 🚀 by Ultralytics, GPL-3.0 license """ Run inference on images, videos, directories, streams, etc.Usage - sources:$ python path/to/detect.py --weights yolov5s.pt --source 0 # webcamimg.jpg # imagevid.mp4 # videopath/ # directorypath/*.jpg # glob'https://youtu.be/Zgi9g1ksQHc' # YouTube'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP streamUsage - formats:$ python path/to/detect.py --weights yolov5s.pt # PyTorchyolov5s.torchscript # TorchScriptyolov5s.onnx # ONNX Runtime or OpenCV DNN with --dnnyolov5s.xml # OpenVINOyolov5s.engine # TensorRTyolov5s.mlmodel # CoreML (macOS-only)yolov5s_saved_model # TensorFlow SavedModelyolov5s.pb # TensorFlow GraphDefyolov5s.tflite # TensorFlow Liteyolov5s_edgetpu.tflite # TensorFlow Edge TPU """import argparse import os import platform import sys from pathlib import Path import numpy as np import torch import torch.backends.cudnn as cudnnfrom utils.augmentations import letterboxFILE = Path(__file__).resolve() ROOT = FILE.parents[0] # YOLOv5 root directory if str(ROOT) not in sys.path:sys.path.append(str(ROOT)) # add ROOT to PATH ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relativefrom models.common import DetectMultiBackend from utils.dataloaders import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams from utils.general import (LOGGER, check_file, check_img_size, check_imshow, check_requirements, colorstr, cv2,increment_path, non_max_suppression, print_args, scale_coords, strip_optimizer, xyxy2xywh) from utils.plots import Annotator, colors, save_one_box from utils.torch_utils import select_device, time_sync@torch.no_grad() def run(imMy = None,weights=ROOT / 'yolov5s.pt', # model.pt path(s)source=ROOT / 'data/images', # file/dir/URL/glob, 0 for webcamdata=ROOT / 'data/coco128.yaml', # dataset.yaml pathimgsz=(640, 640), # inference size (height, width)conf_thres=0.25, # confidence thresholdiou_thres=0.45, # NMS IOU thresholdmax_det=1000, # maximum detections per imagedevice='', # cuda device, i.e. 0 or 0,1,2,3 or cpuclasses=None, # filter by class: --class 0, or --class 0 2 3agnostic_nms=False, # class-agnostic NMSaugment=False, # augmented inferencevisualize=False, # visualize featureshide_labels=False, # hide labelshide_conf=False, # hide confidenceshalf=False, # use FP16 half-precision inferencednn=False, # use OpenCV DNN for ONNX inference ):source = str(source)# Load modeldevice = select_device(device)print(device)model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)stride, names, pt = model.stride, model.names, model.ptimgsz = check_img_size(imgsz, s=stride) # check image sizedataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt)bs = 1 # batch_size# Run inferencemodel.warmup(imgsz=(1 if pt else bs, 3, *imgsz)) # warmupfor path, im, im0s, vid_cap, s in dataset:im0s = imMy# Padded resizeimg = letterbox(im0s, (800,608), stride=32, auto=True)[0]# Convertimg = img.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGBimg = np.ascontiguousarray(img)im = imgim = torch.from_numpy(im).to(device)im = im.half() if model.fp16 else im.float() # uint8 to fp16/32im /= 255 # 0 - 255 to 0.0 - 1.0if len(im.shape) == 3:im = im[None] # expand for batch dim# Inferencevisualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else Falsepred = model(im, augment=augment, visualize=visualize)# pred = model(im, augment=augment, visualize=False)# NMSpred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)# Second-stage classifier (optional)# pred = utils.general.apply_classifier(pred, classifier_model, im, im0s)# Process predictionsres = []for i, det in enumerate(pred): # per imageim0 = im0s.copy()if len(det):# Rescale boxes from img_size to im0 sizedet[:, :4] = scale_coords(im.shape[2:], det[:, :4], im0.shape).round()# Write resultsfor *xyxy, conf, cls in reversed(det):parm = []c = int(cls)label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')p1, p2 = (int(xyxy[0]), int(xyxy[1])), (int(xyxy[2]), int(xyxy[3]))parm.append(label)parm.append(p1)parm.append(p2)res.append(parm)return resdef parse_opt():parser = argparse.ArgumentParser()parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolov5s.pt', help='model path(s)')parser.add_argument('--source', type=str, default=ROOT / 'data/images', help='file/dir/URL/glob, 0 for webcam')parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='(optional) dataset.yaml path')parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w')parser.add_argument('--conf-thres', type=float, default=0.25, help='confidence threshold')parser.add_argument('--iou-thres', type=float, default=0.45, help='NMS IoU threshold')parser.add_argument('--max-det', type=int, default=1000, help='maximum detections per image')parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')parser.add_argument('--view-img', action='store_true', help='show results')parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')parser.add_argument('--save-crop', action='store_true', help='save cropped prediction boxes')parser.add_argument('--nosave', action='store_true', help='do not save images/videos')parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --classes 0, or --classes 0 2 3')parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')parser.add_argument('--augment', action='store_true', help='augmented inference')parser.add_argument('--visualize', action='store_true', help='visualize features')parser.add_argument('--update', action='store_true', help='update all models')parser.add_argument('--project', default=ROOT / 'runs/detect', help='save results to project/name')parser.add_argument('--name', default='exp', help='save results to project/name')parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)')parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels')parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')opt = parser.parse_args()opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expandprint_args(vars(opt))return optdef main(opt):check_requirements(exclude=('tensorboard', 'thop'))run(**vars(opt))if __name__ == "__main__":opt = parse_opt()main(opt)

    使用:

    res = detect.run(weights='./dnf/best.pt',source="yolo_A/images/2a.jpg",data="yolo_A/A.yaml",imgsz=(800, 608),imMy=imm)

    ?這樣,結(jié)合前邊實時捕獲桌面,就可以實現(xiàn)大部分的功能了。

    基于gpu預測

    最后我嫌棄預測太慢,想指定GPU預測,但是發(fā)現(xiàn)一直false

    print(torch.cuda.is_available()) // False

    ?然后我安裝了cuda,參考:CUDA安裝教程(超詳細)_Billie使勁學的博客-CSDN博客_cuda安裝

    ?然后從虛擬環(huán)境卸載torch和torchversion(pip uninstall一下即可)

    從Start Locally | PyTorch找到適合你電腦的版本,以下是我的

    ?然后從https://download.pytorch.org/whl/torch_stable.html安裝wheel文件

    ?安裝torch和torchvision,具體安裝cp多少(我是根據(jù)安裝yolo環(huán)境時pip install -r requirements.txt時控制臺打印安裝的torch對應的cp)

    然后安裝wheel文件。

    運行發(fā)現(xiàn)

    print(torch.cuda.is_available()) // True

    ?我的版本device指定為空并gpu可用時則選擇gpu

    完結(jié)撒花。

    驅(qū)動級鍵盤操作繞過游戲檢測

    查了一堆博客驅(qū)動級操作,甚至加群問別人怎么做都沒問出來,真。。。

    最后試到凌晨,廢話不多說直接上正確案例,需要用到pywin32,自己pip一下(運行時需要管理員運行)

    import time import win32api import win32con import ctypes import win32guiVK_CODE = {'backspace': 0x08,'tab': 0x09,'clear': 0x0C,'enter': 0x0D,'shift': 0x10,'ctrl': 0x11,'alt': 0x12,'pause': 0x13,'caps_lock': 0x14,'esc': 0x1B,'spacebar': 0x20,'page_up': 0x21,'page_down': 0x22,'end': 0x23,'home': 0x24,'left_arrow': 0x25,'up_arrow': 0x26,'right_arrow': 0x27,'down_arrow': 0x28,'select': 0x29,'print': 0x2A,'execute': 0x2B,'print_screen': 0x2C,'ins': 0x2D,'del': 0x2E,'help': 0x2F,'0': 0x30,'1': 0x31,'2': 0x32,'3': 0x33,'4': 0x34,'5': 0x35,'6': 0x36,'7': 0x37,'8': 0x38,'9': 0x39,'a': 0x41,'b': 0x42,'c': 0x43,'d': 0x44,'e': 0x45,'f': 0x46,'g': 0x47,'h': 0x48,'i': 0x49,'j': 0x4A,'k': 0x4B,'l': 0x4C,'m': 0x4D,'n': 0x4E,'o': 0x4F,'p': 0x50,'q': 0x51,'r': 0x52,'s': 0x53,'t': 0x54,'u': 0x55,'v': 0x56,'w': 0x57,'x': 0x58,'y': 0x59,'z': 0x5A,'numpad_0': 0x60,'numpad_1': 0x61,'numpad_2': 0x62,'numpad_3': 0x63,'numpad_4': 0x64,'numpad_5': 0x65,'numpad_6': 0x66,'numpad_7': 0x67,'numpad_8': 0x68,'numpad_9': 0x69,'multiply_key': 0x6A,'add_key': 0x6B,'separator_key': 0x6C,'subtract_key': 0x6D,'decimal_key': 0x6E,'divide_key': 0x6F,'F1': 0x70,'F2': 0x71,'F3': 0x72,'F4': 0x73,'F5': 0x74,'F6': 0x75,'F7': 0x76,'F8': 0x77,'F9': 0x78,'F10': 0x79,'F11': 0x7A,'F12': 0x7B,'F13': 0x7C,'F14': 0x7D,'F15': 0x7E,'F16': 0x7F,'F17': 0x80,'F18': 0x81,'F19': 0x82,'F20': 0x83,'F21': 0x84,'F22': 0x85,'F23': 0x86,'F24': 0x87,'num_lock': 0x90,'scroll_lock': 0x91,'left_shift': 0xA0,'right_shift ': 0xA1,'left_control': 0xA2,'right_control': 0xA3,'left_menu': 0xA4,'right_menu': 0xA5,'browser_back': 0xA6,'browser_forward': 0xA7,'browser_refresh': 0xA8,'browser_stop': 0xA9,'browser_search': 0xAA,'browser_favorites': 0xAB,'browser_start_and_home': 0xAC,'volume_mute': 0xAD,'volume_Down': 0xAE,'volume_up': 0xAF,'next_track': 0xB0,'previous_track': 0xB1,'stop_media': 0xB2,'play/pause_media': 0xB3,'start_mail': 0xB4,'select_media': 0xB5,'start_application_1': 0xB6,'start_application_2': 0xB7,'attn_key': 0xF6,'crsel_key': 0xF7,'exsel_key': 0xF8,'play_key': 0xFA,'zoom_key': 0xFB,'clear_key': 0xFE,'+': 0xBB,',': 0xBC,'-': 0xBD,'.': 0xBE,'/': 0xBF,';': 0xBA,'[': 0xDB,'\\': 0xDC,']': 0xDD,"'": 0xDE,'`': 0xC0}# handle = win32gui.FindWindow(None, '地下城與勇士') # win32gui.SetForegroundWindow(handle)while True:time.sleep(5)MapVirtualKey = ctypes.windll.user32.MapVirtualKeyAtime.sleep(0.3)win32api.keybd_event(VK_CODE["numpad_8"], win32api.MapVirtualKey(VK_CODE["numpad_8"], 0), 0, 0) # 0time.sleep(0.3)win32api.keybd_event(VK_CODE["numpad_8"], win32api.MapVirtualKey(VK_CODE["numpad_8"], 0), win32con.KEYEVENTF_KEYUP, 0)time.sleep(0.3)win32api.keybd_event(VK_CODE["numpad_5"], win32api.MapVirtualKey(VK_CODE["numpad_5"], 0), 0, 0) # 0time.sleep(0.3)win32api.keybd_event(VK_CODE["numpad_5"], win32api.MapVirtualKey(VK_CODE["numpad_5"], 0), win32con.KEYEVENTF_KEYUP,0)time.sleep(0.3)win32api.keybd_event(VK_CODE["numpad_4"], win32api.MapVirtualKey(VK_CODE["numpad_4"], 0), 0, 0) # 0time.sleep(0.3)win32api.keybd_event(VK_CODE["numpad_4"], win32api.MapVirtualKey(VK_CODE["numpad_4"], 0), win32con.KEYEVENTF_KEYUP, 0)time.sleep(0.3)win32api.keybd_event(VK_CODE["numpad_6"], win32api.MapVirtualKey(VK_CODE["numpad_6"], 0), 0, 0) # 0time.sleep(0.3)win32api.keybd_event(VK_CODE["numpad_6"], win32api.MapVirtualKey(VK_CODE["numpad_6"], 0), win32con.KEYEVENTF_KEYUP, 0)# win32api.keybd_event(0x0D, 0, 0, 0) # enter# win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)

    總結(jié)

    以上是生活随笔為你收集整理的图像识别从零写出dnf脚本关键要点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。