YouTube2Text

How to make advanced image recognition bots using python — Transcript

by Kian Brose · 3,112 words · 419 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Hey, so today I'll teach you how to make
  2. 0:02some bots. We'll be covering how to make
  3. 0:03a simple piano tail bot, how to make an
  4. 0:05aim booster bot that can track an
  5. 0:06object. Okay, so let's get started. Step
  6. 0:09one, download Python and remember to
  7. 0:11check the add to path checkbox when
  8. 0:13installing. Step two, open the console
  9. 0:15with administrator privileges and run
  10. 0:17the following commands. You can find
  11. 0:18them in the video description. These are
  12. 0:20all Python libraries that we are going
  13. 0:22to use throughout the tutorial. I'll
  14. 0:23explain what they do a bit more into the
  15. 0:25video. Step three. Now that we have
  16. 0:27completed this setup, create a new
  17. 0:28folder and create a text file named b.
  18. 0:30py. You can name it to whatever you want
  19. 0:32as long as it ends with the py
  20. 0:34extension. Now begins the interesting
  21. 0:36part of the tutorial. Right click your
  22. 0:37newly created Python file, edit with
  23. 0:39idle, edit with idle, and paste this
  24. 0:41into the code editor. You can find this
  25. 0:43in the description. These are the
  26. 0:45libraries we downloaded a second ago.
  27. 0:46Let's find tiles online. This one should
  28. 0:49do. For this game, we have four rows
  29. 0:51where black squares will come down from
  30. 0:53each row. The fastest and easiest way to
  31. 0:55bought this game is to check a single
  32. 0:57pixel from each row. We will check if
  33. 0:59that pixel is black, and if it is, we're
  34. 1:01going to click it. Okay, so first we
  35. 1:03need to know the RGB value of black,
  36. 1:05which is the tile's color. For this,
  37. 1:07open a new Python shell by pressing
  38. 1:08Windows search Python and open. Write
  39. 1:11import pi autogi and then pi autog gui
  40. 1:15display mouse position. The shell will
  41. 1:17display the pixel you're pointing at and
  42. 1:19its RGB value. Hover over any black part
  43. 1:22of the screen. You can see the color for
  44. 1:24black is 0 red, zero green, and zero
  45. 1:26blue. Go back to the shell and press
  46. 1:28Ctrl C to stop it from running. We now
  47. 1:31want to figure out the X aka horizontal
  48. 1:34position for each row. Open the Python
  49. 1:36shell again and type import pygi and
  50. 1:38then pygi display moss position. Hover
  51. 1:42over the first, second, third, and
  52. 1:44fourth row. Go back to the shell and
  53. 1:46copy the data from each respective row.
  54. 1:48We will only be using one Y vertical
  55. 1:50position. So I'll put mine to around
  56. 1:52400, which is around the middle of the
  57. 1:54game. For me, the first house pixel is x
  58. 1:57597. Second x676, and so on. It will be
  59. 2:01different for you, but don't worry. Open
  60. 2:03the bot.py file. There are two ways to
  61. 2:05perform a click. The first is by using
  62. 2:07pygi click function, which looks like
  63. 2:09this. The the downside for this is that
  64. 2:12it's pretty slow. So instead, we will
  65. 2:14use the wind32 API, which runs much
  66. 2:16faster. To do this, we start the script
  67. 2:19by creating a click function like so.
  68. 2:21This uses the Win32 API library to move
  69. 2:23the mouse to XY position with set cursor
  70. 2:26pause, pressing down, holding for 0.01
  71. 2:29seconds, and releasing. We want the 0.01
  72. 2:32second delay since clicks sometimes fail
  73. 2:34to execute if you click too fast. We
  74. 2:36then write while keyboard is pressed Q
  75. 2:38equal equal false. This line checks if
  76. 2:41the key Q is pressed down. It returns
  77. 2:43true if it's pressed down and false if
  78. 2:44it's not. We're going to use this to
  79. 2:46stop the bot when we want to stop it.
  80. 2:49Okay. So now what we'll do is check the
  81. 2:51RGB value of the first row's pixel. To
  82. 2:53do this, we use the if pi autogy.pixel
  83. 2:56and then your pixel's location 0 equal
  84. 2:59equals 0. This will return the RGB value
  85. 3:01of the pixel located in X 597 and Y 400.
  86. 3:06The zero checks for the R value. Whereas
  87. 3:08if you were to switch the zero for a
  88. 3:10one, it would check for the G value. And
  89. 3:11if you were to switch it for a two, it
  90. 3:13would check the B value. There's no need
  91. 3:15to check for more than one in our case.
  92. 3:16So the R value is enough. Now that the
  93. 3:19script can tell when the pixel is black,
  94. 3:21we want to click it immediately when it
  95. 3:23does. For this, we call the click
  96. 3:24function we created and make it click
  97. 3:26the first tile's position, which is the
  98. 3:28same position we scanned. We do this by
  99. 3:30writing click and then your pixel's
  100. 3:32position. All right, so repeat this
  101. 3:33process for the remaining three tiles.
  102. 3:35And don't forget to add the correct
  103. 3:36pixel location. And there you go. Press
  104. 3:38run, run module, switch back to piano
  105. 3:40tiles, and press start.
  106. 3:45So, as you can see, yay, it works.
  107. 3:49[Applause]
  108. 3:50[Music]
  109. 3:52[Applause]
  110. 3:59Now that you know how to make a piano
  111. 4:00tile spot, let's move on to the aim
  112. 4:02booster bot. Start by going to
  113. 4:03aimbooster.com. Enable flash player if
  114. 4:05it's not enabled and press reload. In
  115. 4:07this game, you have to press the circles
  116. 4:09that appear randomly in this area of the
  117. 4:11screen.
  118. 4:12There are two approaches to solving this
  119. 4:14game. The first is to take a picture of
  120. 4:16the circle and locating it on the
  121. 4:18screen. This approach would be too slow.
  122. 4:20So instead, we will take a screenshot of
  123. 4:22the game area and check every one out of
  124. 4:24five pixels to see if that pixel is the
  125. 4:27color of the center of the target.
  126. 4:28Before doing this, however, I'll
  127. 4:30demonstrate how to do image recognition.
  128. 4:32Okay, so through my amazing artistic
  129. 4:33talent, I have made what appears to be a
  130. 4:35stickman in paint. What I'll teach you
  131. 4:37now is how to detect if the stickman is
  132. 4:39on the screen or not. Go back to the
  133. 4:41folder you created with the bot.python
  134. 4:43file. Create a new file and name it
  135. 4:45stickman.py.
  136. 4:46Edit the file with idle like we did
  137. 4:48before and paste in the same libraries
  138. 4:50as in the piano tiles bot. Go back to
  139. 4:52the stick man you drew. In my case, this
  140. 4:54one. On Windows, press shift Windows S.
  141. 4:57This will open up a screenshot tool that
  142. 4:58allows you to select a part of the
  143. 5:00screen to screenshot. Drag over the
  144. 5:02stick man you drew to save the
  145. 5:03screenshot. Open a new paint file and
  146. 5:05paste the stickman. You now want to trim
  147. 5:07it as small as possible by selecting the
  148. 5:09area that contains the stick man and
  149. 5:11cutting excess like so. Once you have
  150. 5:13cut the excess, press file, save as, and
  151. 5:15save it as stickman.png in the same
  152. 5:18folder as the script. Saving it in the
  153. 5:20same location as the script is
  154. 5:21important. Open the stickman script you
  155. 5:23just created. We will now create a while
  156. 5:25loop with while one. Inside this loop,
  157. 5:29we will write the script that is
  158. 5:30actually going to find the image. If pi
  159. 5:33go.locate on screen stickman.png png is
  160. 5:36not equal none. This line uses pi autogi
  161. 5:38to locate the stickman on the screen. If
  162. 5:41it doesn't find anything, the output of
  163. 5:43this line will be none. So if it's not
  164. 5:45none, it means it found it. So if you
  165. 5:48write the following script, if it finds
  166. 5:50the image on the screen, it will print
  167. 5:51out I can see it and then wait for 0.5
  168. 5:54seconds. The delay so that the script
  169. 5:56doesn't spam the console. And if it
  170. 5:58doesn't find the image, it writes I am
  171. 5:59unable to see it. Run the script by
  172. 6:02pressing run module. And as you can see,
  173. 6:04the script can see the image. But if you
  174. 6:06move the window over the stickman, it
  175. 6:08stops working.
  176. 6:10Now, one of the questions you might have
  177. 6:11is what if the background of the image
  178. 6:13I'm trying to detect changes? For
  179. 6:15example, if I were to put this
  180. 6:17disturbance here, the script wouldn't
  181. 6:19detect the stickman since it's not an
  182. 6:21identical match. To fix this, we simply
  183. 6:23add the confidence parameter in the
  184. 6:25locate on screen like this. Let me
  185. 6:27explain. When the bot looks at the
  186. 6:29screen, it checks every pixel and gives
  187. 6:30a percentage chance probability of
  188. 6:32finding a match to the image you're
  189. 6:34asking to search for. By default, pgy
  190. 6:37only gives an output when it's 100% sure
  191. 6:39of having a match. But by using
  192. 6:41confidence 0.8, we tell it that if it's
  193. 6:4480% sure or more to have found the
  194. 6:46image, it finds it. You can, of course,
  195. 6:48adjust the value to whatever you need.
  196. 6:50But wait, there's more. Now, you might
  197. 6:51be asking yourself, how can I make the
  198. 6:53script run faster? The first and most
  199. 6:55obvious way is by checking a smaller
  200. 6:57region of the screen. So instead of
  201. 6:59searching for the image on the entire
  202. 7:01screen, we have to tell it where to
  203. 7:02look. Finding the correct region is a
  204. 7:05bit of a process, but I'll explain as
  205. 7:06best as I can. Create a new Python
  206. 7:08script in the folder. Copy the path to
  207. 7:10the folder by clicking here and copying
  208. 7:12this. Open the script and write the
  209. 7:14following code. It's in the description
  210. 7:16as well. But remember to change this
  211. 7:18path to your folder's path you copied a
  212. 7:20second ago. This script saves the image
  213. 7:22on the region 0 0 100 100 as saved
  214. 7:25image.png. The first number is the x
  215. 7:28position that the screenshot starts on.
  216. 7:30The second number is the y position that
  217. 7:32the screenshot starts on. The third
  218. 7:34number is the width and the fourth is
  219. 7:36the height. So let's say I do region 100
  220. 7:38200 500 500. The script will take a
  221. 7:41screenshot starting 100 pixels from the
  222. 7:43right, 200 pixels down, and have a width
  223. 7:45and height of 500 pixels. Now we just
  224. 7:48need to tweak the values manually. Run
  225. 7:50the script. Look at the output until we
  226. 7:52find the region of the stickman. So as
  227. 7:55you can see, running the script saves an
  228. 7:57image with the region I gave it. I found
  229. 7:59that the region for the stickman in my
  230. 8:00case is around 150, 175, 350, and 600.
  231. 8:04Keep in mind, it doesn't have to be too
  232. 8:06close to the object. It's fine to check
  233. 8:08the area around the stickman as well.
  234. 8:10Open the stickman script again and write
  235. 8:12region equal your region and then
  236. 8:14grayscale equals true. The grayscale
  237. 8:16parameter gives a speed boost of around
  238. 8:1830%, so it's always a nice thing to
  239. 8:20have. Do not use grayscale if you're
  240. 8:22getting fake results, but in our case,
  241. 8:24it's fine. Run the newly modified
  242. 8:26script, and as you can see, it can
  243. 8:28detect the stickman even though parts of
  244. 8:29it are hidden and or modified.
  245. 8:34All right, so back to the aim booster
  246. 8:35bot. Create a new Python file in the
  247. 8:37same folder as all the other files we
  248. 8:39made. Name it aimooster. py. Paste in
  249. 8:42the libraries we used in the two last
  250. 8:43examples and then rewrite the same click
  251. 8:45function we used in piano tiles.
  252. 8:47Remember to add a timesleep 2 to give
  253. 8:50you two seconds to switch over to aim
  254. 8:51booster when starting the script. So as
  255. 8:54I explained a bit earlier, we will be
  256. 8:55taking a screenshot of this region of
  257. 8:57the screen where the circles appear and
  258. 8:58then check every one out of five pixels
  259. 9:00to see if the color of that pixel
  260. 9:02matches the color of the center of the
  261. 9:03target. So first we want to figure out
  262. 9:06what the region for the game area is. To
  263. 9:08do this, open the script you created
  264. 9:10during the stickman demo that takes a
  265. 9:11screenshot of a region and saves it and
  266. 9:14tweak the region values until you get
  267. 9:15this region, which is the region where
  268. 9:17the circles are going to be appearing.
  269. 9:20Now that you know the game region, let's
  270. 9:22start the script with the while keyboard
  271. 9:24is pressed equal equal false so that we
  272. 9:26can stop the bot from moving the mouse
  273. 9:27when we want it to stop. Write the
  274. 9:29following line. This line uses pi autogy
  275. 9:32screenshot function to take a screenshot
  276. 9:33of the screen and store it in the pick
  277. 9:35variable. In our case, it takes a
  278. 9:37screenshot of the region we give it,
  279. 9:38which for me is 660, 350, 600, 400. Your
  280. 9:43region will be different, so don't mind
  281. 9:44it if it's not exactly the same. We then
  282. 9:47use the size function to get the width
  283. 9:48and height of the pick variable, like
  284. 9:50so. This will store the width in the
  285. 9:52variable width and height in the
  286. 9:54variable height. Pretty
  287. 9:55self-explanatory.
  288. 9:57Now, we want to check what the RGB value
  289. 9:59of the center of the target is. For
  290. 10:01this, I took a screenshot of the game
  291. 10:03while playing, pasted the picture in
  292. 10:05Microsoft Paint, opened the Python
  293. 10:07shell, wrote import pi autogy and pi
  294. 10:09autogy the display mouse position. I
  295. 10:12then hover over the center of the target
  296. 10:14and the console will print out the RGB
  297. 10:16value of the pixel I am pointing to. The
  298. 10:18center of the target has an RGB value of
  299. 10:20255, 219, and 195. Copy the RGB values.
  300. 10:25Go back to the script and paste it
  301. 10:26anywhere as a comment. Note to make a
  302. 10:28comment in Python, use the hashtag
  303. 10:30symbol. Okay, so since checking every
  304. 10:32single pixel in our screenshot for a
  305. 10:34match is a bit overkill, we will instead
  306. 10:36check for the first pixel, then the
  307. 10:37fifth, then the 10th, etc., etc. But
  308. 10:39first, let me explain how a for loop
  309. 10:42works. Let's take this example for x in
  310. 10:44range 0 10 1, print x. This will output
  311. 10:48every single number starting from 0 up
  312. 10:50to 9. This means that the script will
  313. 10:51set x to zero, print out x, so zero, and
  314. 10:55then increase x by 1, which is the third
  315. 10:57value here. It will do this while x is
  316. 10:59smaller than 10. The maximum value this
  317. 11:01can output is 9. If we were to run 4x in
  318. 11:04range 0 151, print x, the script will
  319. 11:08print out every number from 0 to 14. If
  320. 11:10we were to increase the third value here
  321. 11:12to two for x in range 0 152 print x, we
  322. 11:16will instead get increments of two
  323. 11:17instead of 1. So the output would be 0 2
  324. 11:204 6 8 10 12 14. So as you might have
  325. 11:25already guessed we will be using
  326. 11:26increments of five for our image. So for
  327. 11:28example let's say that our image has a
  328. 11:30width of 500 pixels and a height of 600
  329. 11:32pixels. We write for x in range 0 5005
  330. 11:36for y in range 0 65. RGB equal pick.get
  331. 11:41pixel xy.
  332. 11:44The script will save the RGB values of
  333. 11:46the pixel in position XY of our picture
  334. 11:48pick thanks to the get pixel function.
  335. 11:50So for example, the script will first
  336. 11:52check the pixel x0 y 0 then x0 y5 then
  337. 11:56x0 y 10 then x0 y15 etc etc until it
  338. 12:00hits the maximum value of y 595 since we
  339. 12:04have increments of five and y has to be
  340. 12:06smaller than 600 and store the red value
  341. 12:08in r, green value in g and a blue value
  342. 12:10in b. All right. So, your script should
  343. 12:13now look like this. Since my game has a
  344. 12:15region width of 600 and a height of 400,
  345. 12:18I will use the width variable here and
  346. 12:20the height variable here instead of
  347. 12:21hard-coded numbers. You can use
  348. 12:23hard-coded numbers if you want, but for
  349. 12:25simplicity, I will use the variables.
  350. 12:27So, now that we can successfully check
  351. 12:28the color of every fifth pixel, we want
  352. 12:30to perform a click if the color matches
  353. 12:32the center of the target's color. You
  354. 12:34can code this in two different ways. In
  355. 12:36my case, the center of the color of the
  356. 12:38target is always going to be the same.
  357. 12:40So we can add a if b which is the blue
  358. 12:42value equal equal 195. This checks if
  359. 12:45the blue value of the pixel located in
  360. 12:47xy is equal equal to the value 195 which
  361. 12:51is the blue value of the center of our
  362. 12:53target. To check for the red and green
  363. 12:54values change it to either r for red and
  364. 12:57g for green. But in this case blue is
  365. 12:59more than enough. But if you wanted to
  366. 13:02create a bot similar to this one where
  367. 13:04the center is not always going to be the
  368. 13:06same color then you can use range again.
  369. 13:08If you were to use range, the loop would
  370. 13:10look like this. If B in range 180 210,
  371. 13:14this will check if the blue value stored
  372. 13:16in B is between 180 and 210.
  373. 13:20In my case, checking for a range is
  374. 13:22unnecessary. So I will just stick for if
  375. 13:25B equal equal 195.
  376. 13:28Okay. So now that the script can tell
  377. 13:30when the color of the pixel is the color
  378. 13:32we want it to be, we want to click. For
  379. 13:34this, simply call the click x + 660 y +
  380. 13:38350.
  381. 13:39If you wonder why I have these values,
  382. 13:41remember that the click function will
  383. 13:43click a pixel based on your screen
  384. 13:45resolution. So a click 0 0 will click at
  385. 13:48the top left of the screen. But in our
  386. 13:50case, we want to perform a click
  387. 13:51relative to the screenshots region.
  388. 13:53Since our region's first pixel, aka
  389. 13:55pixel 000 of our screenshot starts at
  390. 13:58660 pixels to the right and 350 pixels
  391. 14:01down, we want the click function to also
  392. 14:03be 660 pixels right and 350 pixels down.
  393. 14:08Since the script runs a bit too fast, it
  394. 14:10might click the circles a bit too fast.
  395. 14:12So, we add a time sleep after the click
  396. 14:14to prevent misclicks. I'll put mine to
  397. 14:160.05. We then end the script with a
  398. 14:18break to stop the script from clicking
  399. 14:20the same place again.
  400. 14:22Save the script. Run the script. Tab
  401. 14:24over to aim booster and press challenge.
  402. 14:27And voila, there you have it. A working
  403. 14:29aim booster bot. Remember that you have
  404. 14:30to hold Q to stop the bot. If you
  405. 14:33experience accuracy issues, just
  406. 14:34slightly tweak the delay after the click
  407. 14:36so the bot doesn't have the time to
  408. 14:38click twice. So before ending the video,
  409. 14:41I just wanted to ask if the fast pace of
  410. 14:43this video was okay or if it was way too
  411. 14:45hard to follow and if you want a more
  412. 14:46detailed in-depth video about botting.
  413. 14:49All right, so I hope you enjoyed this
  414. 14:50fast-paced tutorial. If it helped you
  415. 14:52out, please consider subscribing and
  416. 14:53leaving a like. If you have any tips on
  417. 14:56how I can improve the video, do leave
  418. 14:57them in the video description. Have a
  419. 14:59great day.

About this transcript

This page contains the full transcript of How to make advanced image recognition bots using python by Kian Brose, generated from the public captions YouTube serves with the video. The transcript has 3,112 words across 419 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.