//计算需要实际检测的屏幕区域 //只检测瞄准位置的一小块窗口,提升检测效率 public void CalDetectionRect(int w, int h) { if (w > 0 && h > 0 && this.rawDetectionRect.w > 0 && this.rawDetectionRect.h > 0 && w <= this.rawDetectionRect.w && h <= this.rawDetectionRect.h) { //获取到窗口坐标 this.detectionRect = new DetectionRect(); //裁剪窗口,新算法,先算出屏幕中心点,只检查屏幕正中的一小块区域,提升检测速度 int centX = this.rawDetectionRect.x + this.rawDetectionRect.w / 2; int centy = this.rawDetectionRect.y + this.rawDetectionRect.h / 2; this.detectionRect.x = centX - w / 2; this.detectionRect.w = w; //检测区域在中心点稍微向上偏移 this.detectionRect.y = centy - (h / 2 + h / 10); this.detectionRect.h = h; } if (this.detectGraphics != null) { this.detectGraphics.Dispose(); } if (this.detectBitmap != null) { this.detectBitmap.Dispose(); } //初始化全局检测图像处理对象 this.detectBitmap = new Bitmap(this.detectionRect.w, this.detectionRect.h); this.detectGraphics = Graphics.FromImage(detectBitmap); }
private void button7_Click(object sender, EventArgs e) { if (comboBox_process.Text == null || comboBox_process.Text.Trim().Length <= 0) { MessageBox.Show("请指定游戏进程名称。"); } else { SystemUtil util = new SystemUtil(); DetectionRect srcRect = util.findProcessWindowRect(comboBox_process.Text); if (srcRect.h > 0 && srcRect.w > 0) { objectDetection.SetSrcScreenRect(srcRect); usbDevice.SetProcessName(comboBox_process.Text); systemStatus.processScreen = "" + comboBox_process.Text + ":" + srcRect.x + "," + srcRect.y + "," + srcRect.w + "," + srcRect.h; this.textBox_msg.Text = systemStatus.GetSytemStatus(); } else { MessageBox.Show("没找到运行的进程:" + comboBox_process.Text); systemStatus.processScreen = "没找到运行的进程:" + comboBox_process.Text; this.textBox_msg.Text = systemStatus.GetSytemStatus(); } } }
private void GameMasterForm_Load(object sender, EventArgs e) { objectDetection = new ScreenDetection(this, this.pictureBox1); usbDevice = new UsbDevice(); systemStatus = new SytemStatus(); keyboardMouseHook = new KeyboardMouseHook(usbDevice, this, this.comboBox_bag1GunType, this.comboBox_bag1ScopeType, this.comboBox_bag2GunType, this.comboBox_bag2ScopeType); //缺省启动鼠标右键hook systemStatus.keyMouseHook = true; keyboardMouseHook.StartHook(); this.textBox_msg.Text = systemStatus.GetSytemStatus(); //设置一个缺省的检测位置 DetectionRect srcRect = new DetectionRect(); srcRect.x = 0; srcRect.y = 0; srcRect.h = Screen.PrimaryScreen.Bounds.Height; srcRect.w = Screen.PrimaryScreen.Bounds.Width; if (srcRect.h > 0 && srcRect.w > 0) { objectDetection.SetSrcScreenRect(srcRect); usbDevice.SetProcessName(comboBox_process.Text); systemStatus.processScreen = "" + comboBox_process.Text + ":" + srcRect.x + "," + srcRect.y + "," + srcRect.w + "," + srcRect.h; this.textBox_msg.Text = systemStatus.GetSytemStatus(); } }
public long toltalMillis; //检测执行的时间 public void setValues(DetectionRect rawDetectionRect1, DetectionRect detectionRect1, Mat frameMat1, List <ObjectPosRect> objectPosRects1, ObjectPosRect maxConfidencePos1, int maxPersonW1, long toltalMillis1) { detectionResult4Rect = new DetectionResult4Rect(); detectionResult4Rect.rawDetectionRect = rawDetectionRect1; detectionResult4Rect.detectionRect = detectionRect1; detectionResult4Rect.maxConfidencePos = maxConfidencePos1; detectionResult4Rect.maxPersonW = maxPersonW1; frameMat = frameMat1; objectPosRects = objectPosRects1; toltalMillis = toltalMillis1; }
//设置需要检测的屏幕区域 public void SetSrcScreenRect(DetectionRect srcRect) { this.rawDetectionRect = srcRect; if (srcRect.w > 0 && srcRect.h > 0) { //游戏者的位置的x轴中心点 //对于绝地求生,在屏幕下方靠左一点,大概 860/1920处 playerCentX = srcRect.w * 860 / 1920 - srcRect.x; } //计算需要实际检测的屏幕区域 CalDetectionRect(290, 230); }
// 根据进程名查找窗体区域 public DetectionRect findProcessWindowRect(string processName) { DetectionRect srcRect = new DetectionRect(); //缺省使用使用全屏作为工作屏幕,找不到进程窗体区域就使用缺省值 srcRect.setValues(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Process[] allProcess = Process.GetProcesses(); if (allProcess != null && allProcess.Length > 0) { foreach (Process process in allProcess) { if (process != null && process.ProcessName != null) { string tmpName = process.ProcessName.ToLower(); if (tmpName.StartsWith(processName.ToLower())) { //如果MainWindowHandle不为0,则获取找到的进程的窗口 IntPtr hWnd = process.MainWindowHandle; if (hWnd != IntPtr.Zero) { RECT winRect = new RECT(); GetWindowRect(hWnd, ref winRect); //h为窗口句柄 RECT clientRect = new RECT(); GetClientRect(hWnd, ref clientRect); //h为窗口句柄 srcRect.setValues(winRect.Left + (winRect.Right - winRect.Left - clientRect.Right) / 2, winRect.Top + (winRect.Bottom - winRect.Top - clientRect.Bottom), clientRect.Right, clientRect.Bottom); break; } } } } } return(srcRect); }
public ScreenDetection(Form mainForm, PictureBox outPictureBox) { this.mainForm = mainForm; this.outPictureBox = outPictureBox; this.detectionNet = CvDnn.ReadNetFromTensorflow(modelFile, configFile); //this.detectionNet = CvDnn.ReadNetFromTensorflow(modelFile); //this.detectionNet.SetPreferableBackend(Backend.DEFAULT); //this.detectionNet.SetPreferableTarget(Target.CPU); this.detectionNet.SetPreferableBackend(Backend.CUDA); this.detectionNet.SetPreferableTarget(Target.CUDA); this.labelNames = File.ReadAllLines(labelFile) .Select(line => line.Split('\n').Last()) .ToArray(); //设置初始检测区域 rawDetectionRect = new DetectionRect(); //缺省使用使用全屏作为工作屏幕 rawDetectionRect.setValues(0, 0, 900, 760); SetSrcScreenRect(rawDetectionRect); }