TensorFlow Object Detection CS:GO aimbot

In this part, we’re going to change our code, that we could detect enemies, move our mouse and shoot them.

Welcome to part 7 of our TensorFlow Object Detection API tutorial series. First, you can download the code on my GitHub page. In this part, we’re going to change our code, that we could find the center of detected rectangles on our enemies, move our mouse to the center and shoot them.

In this tutorial, we are working with the same files as we used in the 6th tutorial. To achieve desired goals for this tutorial, we’ll need to add several lines to the code. At first, we start with importing pyautogui library:

This library will be used to move our mouse in the game. But some games may not allow you to move the mouse, and then you will need to start a python script with administrator rights, the same as I am doing for CSGO in my YouTube video tutorial.

Next, we are changing the defined monitor size line to the following below. We are doing this because we will use our window width and height in other places to calculate the right coordinates for our game. So to avoid mistakes and not to write the same values in many places, we are defining our window size accordingly:

Before moving to our main while loop, we define a new function, which we’ll use to aim and shoot enemies. As you can see in the following function, we are calculating y differently from x. In my YouTube tutorial, we’ll see that we are shooting above the head when we calculate y in the same way as x. So we are removing that difference by dividing our desired screen height by 9 and adding it to standard y height.

Next, we are improving our code while working in our main while loop. So we create the following for loop. At first, we initialize the array_ch array, where we will place all our ch objects. Then we are going through the boxes[0] array, and if we find our needed classes, we check it for detection percentage. For example in our case classes[0][i] == 2 is equal to ch and if scores[0][i] >= 0.5 of this class is equal or more than 50 percent we assume that we detected our object. In this case, we are taking boxes array numbers, where:

While subtracting the same axis start coordinates and dividing them by two, we receive the center of two axes. This way, we can calculate the center of our detected rectangle. And at the last line, we are drawing a dot in a center:

These few lines of code were only for one object, and we do this for all four objects:

After this, we are making shooting function. So as a team = “t” we choose who we will be shooting at. In this case, we are trying to shoot terrorists. So at first, we check if we have detected terrorist heads. We call Shoot(mid_x, mid_y) function with needed coordinates if we have detected at least one head. If we don’t have heads detected, we check maybe we have detected terrorist bodies. If we did, we call the same shooting function. Otherwise, we don’t call the Shooting function.

Note that we change “t” to “c” at the first line if we would like to shoot at counter-terrorists.

This was only a short explanation of code, full code you can download from the above files. In my YouTube video, you can see how my CSGO aimbot model is working. For now, I am really disappointed about our FPS because no one can play at these numbers… But I am glad that our bot can target enemies quite accurate and shoot them. So maybe for the next tutorial, I will think about what we could do to make it work faster.

Machine learning enthusiast