如何将我的准星移动到我的模型创建的边界框。

huangapple go评论124阅读模式
英文:

How to move my crosshair to the bounding box my model creates

问题

我正在参与一个项目,涉及创建一个AI驱动的机器人,可以自己完全玩Counter-Strike。我有一个工作脚本,使用YOLOv8目标检测算法来识别屏幕上的玩家,并在它们周围绘制边界框。

我遇到了一个问题,即当屏幕上检测到敌人时,机器人会一直向下看。我已经尝试调整鼠标移动计算和实施准星调整,但问题仍然存在。我甚至看了一个类似的使用win32的实现,当我尝试将那个代码实现到我的项目中时,机器人仍然会尽量往下看。有人遇到过类似的行为或有关可能原因的建议吗?我将非常感激任何见解、建议或策略,可以帮助我找出并解决问题的原因。

谢谢!

以下是我的代码:

  1. import os
  2. import time
  3. from ultralytics import YOLO
  4. import cv2
  5. import numpy as np
  6. from windowcap import WindowCapture
  7. import win32gui, win32ui, win32con, win32api
  8. import pydirectinput
  9. wincap = WindowCapture('Counter-Strike: Global Offensive - Direct3D 9')
  10. model_path = os.path.join('.', 'runs', 'detect', 'train10', 'weights', 'last.pt')
  11. model = YOLO(model_path)
  12. while True:
  13. frame = wincap.get_screenshot()
  14. H, W, _ = frame.shape
  15. threshold = 0.5
  16. results = model(frame)[0]
  17. for result in results.boxes.data.tolist():
  18. x1, y1, x2, y2, score, class_id = result
  19. if score > threshold:
  20. cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 4)
  21. cv2.putText(frame, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)),
  22. cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)
  23. box_center_x = (x1 + x2) / 2
  24. box_center_y = (y1 + y2) / 2
  25. screen_width = win32api.GetSystemMetrics(0)
  26. screen_height = win32api.GetSystemMetrics(1)
  27. # 将准星移动到模型框
  28. target_x = int(screen_width * box_center_x / W)
  29. target_y = int(screen_height * box_center_y / H)
  30. pydirectinput.moveTo(target_x, target_y)
  31. cv2.imshow("COMPUTER VISION", frame)
  32. if cv2.waitKey(1) == ord('q'):
  33. cv2.destroyAllWindows()
英文:

I am working on a project involving creating an AI-powered bot to full play Counter-Strike by itself. I have a working script that uses the YOLOv8 object detection algorithm to identify players on the screen, and draw a the bounding box around them.

I'm encountering an issue where the bot will consistently look downward when an enemy is detected on the screen. I have tried adjusting the mouse movement calculations and implementing crosshair adjustments, but the issue persists. I even looked at a similar implementation using win32, when I tried implementing that code to my project it still would have the bot look as far down as it could. Has anyone encountered a similar behavior or have suggestions on what might be causing this? I would greatly appreciate any insights, advice, or strategies that could help me pinpoint and address the cause of the issue.

Thank You!

Here is my code below:

  1. import os
  2. import time
  3. from ultralytics import YOLO
  4. import cv2
  5. import numpy as np
  6. from windowcap import WindowCapture
  7. import win32gui, win32ui, win32con, win32api
  8. import pydirectinput
  9. wincap = WindowCapture('Counter-Strike: Global Offensive - Direct3D 9')
  10. model_path = os.path.join('.','runs','detect','train10','weights','last.pt')
  11. model = YOLO(model_path)
  12. while True:
  13. frame = wincap.get_screenshot()
  14. H, W, _ = frame.shape
  15. threshold = 0.5
  16. results = model(frame)[0]
  17. for result in results.boxes.data.tolist():
  18. x1, y1, x2, y2, score, class_id = result
  19. if score > threshold:
  20. cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 4)
  21. cv2.putText(frame, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)),
  22. cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)
  23. box_center_x = (x1 + x2) / 2
  24. box_center_y = (y1 + y2) / 2
  25. screen_width = win32api.GetSystemMetrics(0)
  26. screen_height = win32api.GetSystemMetrics(1)
  27. # moving crosshair to model box
  28. target_x = int(screen_width * box_center_x / W)
  29. target_y = int(screen_height * box_center_y / H)
  30. pydirectinput.moveTo(target_x, target_y)
  31. cv2.imshow("COMPUTER VISION",frame)
  32. if cv2.waitKey(1) == ord('q'):
  33. cv2.destroyAllWindows()

答案1

得分: 0

从xyxy框格式获取框中心坐标:

  1. box_center_x = x1 + (x2 - x1) / 2
  2. box_center_y = y1 + (y2 - y1) / 2

或者你可以直接从结果对象中获取它们。对于单个检测,可以这样做:

  1. x_centre, y_centre, w, h = results[0].boxes[0].xywh.tolist()
英文:

The box center coordinates from xyxy box format:

  1. box_center_x = x1 + (x2 - x1) / 2
  2. box_center_y = y1 + (y2 - y1) / 2

Or you can get them directly from the results object. For the single detection it will be:

  1. x_centre, y_centre, w, h = results[0].boxes[0].xywh.tolist()

huangapple
  • 本文由 发表于 2023年8月4日 05:14:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定