英文:
How to move my crosshair to the bounding box my model creates
问题
我正在参与一个项目,涉及创建一个AI驱动的机器人,可以自己完全玩Counter-Strike。我有一个工作脚本,使用YOLOv8目标检测算法来识别屏幕上的玩家,并在它们周围绘制边界框。
我遇到了一个问题,即当屏幕上检测到敌人时,机器人会一直向下看。我已经尝试调整鼠标移动计算和实施准星调整,但问题仍然存在。我甚至看了一个类似的使用win32的实现,当我尝试将那个代码实现到我的项目中时,机器人仍然会尽量往下看。有人遇到过类似的行为或有关可能原因的建议吗?我将非常感激任何见解、建议或策略,可以帮助我找出并解决问题的原因。
谢谢!
以下是我的代码:
import os
import time
from ultralytics import YOLO
import cv2
import numpy as np
from windowcap import WindowCapture
import win32gui, win32ui, win32con, win32api
import pydirectinput
wincap = WindowCapture('Counter-Strike: Global Offensive - Direct3D 9')
model_path = os.path.join('.', 'runs', 'detect', 'train10', 'weights', 'last.pt')
model = YOLO(model_path)
while True:
frame = wincap.get_screenshot()
H, W, _ = frame.shape
threshold = 0.5
results = model(frame)[0]
for result in results.boxes.data.tolist():
x1, y1, x2, y2, score, class_id = result
if score > threshold:
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 4)
cv2.putText(frame, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)),
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)
box_center_x = (x1 + x2) / 2
box_center_y = (y1 + y2) / 2
screen_width = win32api.GetSystemMetrics(0)
screen_height = win32api.GetSystemMetrics(1)
# 将准星移动到模型框
target_x = int(screen_width * box_center_x / W)
target_y = int(screen_height * box_center_y / H)
pydirectinput.moveTo(target_x, target_y)
cv2.imshow("COMPUTER VISION", frame)
if cv2.waitKey(1) == ord('q'):
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:
import os
import time
from ultralytics import YOLO
import cv2
import numpy as np
from windowcap import WindowCapture
import win32gui, win32ui, win32con, win32api
import pydirectinput
wincap = WindowCapture('Counter-Strike: Global Offensive - Direct3D 9')
model_path = os.path.join('.','runs','detect','train10','weights','last.pt')
model = YOLO(model_path)
while True:
frame = wincap.get_screenshot()
H, W, _ = frame.shape
threshold = 0.5
results = model(frame)[0]
for result in results.boxes.data.tolist():
x1, y1, x2, y2, score, class_id = result
if score > threshold:
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 4)
cv2.putText(frame, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)),
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)
box_center_x = (x1 + x2) / 2
box_center_y = (y1 + y2) / 2
screen_width = win32api.GetSystemMetrics(0)
screen_height = win32api.GetSystemMetrics(1)
# moving crosshair to model box
target_x = int(screen_width * box_center_x / W)
target_y = int(screen_height * box_center_y / H)
pydirectinput.moveTo(target_x, target_y)
cv2.imshow("COMPUTER VISION",frame)
if cv2.waitKey(1) == ord('q'):
cv2.destroyAllWindows()
答案1
得分: 0
从xyxy框格式获取框中心坐标:
box_center_x = x1 + (x2 - x1) / 2
box_center_y = y1 + (y2 - y1) / 2
或者你可以直接从结果对象中获取它们。对于单个检测,可以这样做:
x_centre, y_centre, w, h = results[0].boxes[0].xywh.tolist()
英文:
The box center coordinates from xyxy box format:
box_center_x = x1 + (x2 - x1) / 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:
x_centre, y_centre, w, h = results[0].boxes[0].xywh.tolist()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论