Tap to unmute

Coding an AI to Play Fruit Ninja

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 ก.ค. 2025
  • In this video, I use Python and the Ultralytics module to train a custom YOLO V8 CNN for object detection to automatically play Fruit Ninja. If you have any ideas for improvements or things to teach an AI or develop next, leave a comment below; you might find a video with your idea. If you enjoyed this video make sure to like, subscribe, and leave a comment below.
    The idea for this video came from a recommendation to watch "Using Image Recognition to DESTROY Fruit Ninja" by CodeNoodles.
    Link to the video:
    • Using Image Recognitio...
    Link to more in-depth details about CNNs:
    stanford.edu/~...
    Link to the code:
    github.com/Two...
    Thank you for watching!

ความคิดเห็น •

  • @Lilly_PK
    @Lilly_PK ปีที่แล้ว +18

    since the background stays still you can compare the current frame to the still snapshot of the background. now you have an object mask. with this mask you can choose to only detect bombs and if its not a bomb then slice it, if it is a bomb then ignore it. this would only work for the fruits that dont zoom the camera in

    • @TwoPointCode
      @TwoPointCode  ปีที่แล้ว +8

      Yes that would be another solution to this problem. Something I noticed while working on this project is that I could only move the mouse so fast before the movement wouldn’t be registered. With an object mask the sliced fruit, “critical” text and “combo” text would also be detected as something to slice which could cause problems with this speed limit. Would be interesting to see how it compares though!

  • @mathew6041
    @mathew6041 ปีที่แล้ว +4

    I am glad to see such a good implementation of the project. But right I'm crying my eyes out like crazy. I made the same project inspired by the same video using the same architecture word to word, but left it half way. You have beaten me to it. Good job TwoPoint Code

  • @progrstick2966
    @progrstick2966 4 หลายเดือนก่อน +2

    Awesome video! I'll comment so maybe the algorithm picks it up because such quality content deserves more views.

    • @TwoPointCode
      @TwoPointCode  4 หลายเดือนก่อน +1

      Thank you! I really appreciate it!

  • @samsterjam5190
    @samsterjam5190 ปีที่แล้ว +2

    Amazing! Fantastic job explaining the use of the CNN and code while keeping it entertaining! This should blow up

  • @Me-0063
    @Me-0063 11 หลายเดือนก่อน +1

    Your videos are great. You earned yourself a subscriber, and I predict you will have many more

  • @mofalkmusic
    @mofalkmusic ปีที่แล้ว +3

    Rly enjoyed this

  • @rafaelsantiagoaltoe6606
    @rafaelsantiagoaltoe6606 ปีที่แล้ว +1

    Amazing! I am just starting to study AI, hope to soon be able to do stuff like this. I really liked the video, I liked how you explained what you did in a concise manner, but still without hiding the needed information.

  • @maxencehm1764
    @maxencehm1764 10 หลายเดือนก่อน +1

    once again an incredible video

  • @dorol6375
    @dorol6375 10 หลายเดือนก่อน +1

    That game was pretty exciting ngl, I was actually smiling in glee when the AI recovered haha

  • @foxstamp
    @foxstamp ปีที่แล้ว +3

    Great video!! It’s a shame that you don’t get more recognition

  • @Muhammed_English314
    @Muhammed_English314 ปีที่แล้ว +3

    When I watched that video, I was disappointed by the AI's low score and wanted to see a better version. I guess I finally got one.

  • @Lucas-pj9ns
    @Lucas-pj9ns ปีที่แล้ว +1

    Excellent, love the optimizations u did!

  • @jotaro6390
    @jotaro6390 ปีที่แล้ว +6

    Amazing video, I really enjoyed it. I was thinking about scoring combo cuts by slicing once through multiple fruits. I had an idea of predicting trajectories of fruits by first frames, then calculating the the point in time so that the program could cut exactly where fruits are aligned. But I am not sure if it is practical as the program in the video already makes few mistakes. I don't have much of experience to implement it, but I would definitely want somebody to do this.

    • @TwoPointCode
      @TwoPointCode  ปีที่แล้ว

      Thank you, I really appreciate it! The idea of tracking the trajectory of the fruits and bombs to get better combos would be interesting. One thing I noticed while developing this is that the bombs don't always follow an arched trajectory and seem to sometimes move erratically. This can be seen with the rightmost bomb between the times of 14:35 and 14:37, which could make tracking difficult.

    • @bluemax6668
      @bluemax6668 ปีที่แล้ว

      Yes i also want to do that since i saw codeNoodles video, like when a fruit is about to fall down out of the screen we find a path that cuts all the fruits, i think that calculating trajectories is pretty doable but i think you could do that more simply. You look at the current velocity of each fruit and you cut along this velocity vector, so even if the fruit moves a bit it will still be in the cut trajectory.

    • @bluemax6668
      @bluemax6668 ปีที่แล้ว

      and to generate the trajectory maybe do a bezier curve with fruits positions and a point just outside the bombs bounding boxes, and if the trajectory goes through a bomb you select an other point

  • @DefGamer-ek1zt
    @DefGamer-ek1zt ปีที่แล้ว +1

    Very nicely done and well explained!

  • @blue_birb
    @blue_birb ปีที่แล้ว +1

    I've got an idea to make the pathfinding better.
    *When 1 fruit is on screen, find a corner of its bounding box and jump the mouse from the corner to the opposite corner
    *When 2 or more fruits are on the screen, calculate the center of all the fruits and jump the mouse from center to center.
    *When 2 or more fruits and a bomb are on screen, calculate the straight line between the centers of the fruits that you are about to jump between, if they intersect with the bounding box of the bomb, find the closest point on the line to the center of the bomb and calculate a vector from it to the closest corner of the bomb's bounding box, multiply it's magnitude by 2, and make another "ghost fruit" at the new point to add an extra stop between the fruits
    If I wasnt clear about the last case then what I meant it basically find if the path between the fruits intersects with a bomb, if it does, find the closest point on the line to the center of the bomb, and push it out 2 times its distance to the closest corner towards the corner to make sure it gets far from the bomb and add that new point as another checkpoint.
    *2 or more fruits and 2 or more bombs, perform the same thing as last case but do it for every bomb again and again until a solution is found
    All this should take like 2 milliseconds or something if implemented correctly

    • @TwoPointCode
      @TwoPointCode  ปีที่แล้ว

      I like it! If I’m understanding correctly, one thing I’m not sure about is when there are two or more bombs, how do you guarantee that a “ghost fruit” doesn’t get placed inside a different bomb? Or, if it does, how do you handle ignoring the “ghost fruit” and going around the bomb? Depending on the speedup, this could be worth implementing and using for zero or one bomb as it is a very common scenario in this game.

  • @rodi6282
    @rodi6282 5 หลายเดือนก่อน +1

    maybe you culd try using PPO to make an agent to slice the fruit and teach it to make combos, which greatly increase the score. Maybe using the change in score as a reward, but it also may run slower and take very long to train

  • @delfini8135
    @delfini8135 ปีที่แล้ว +1

    Great Video!

  • @paulrouxel7526
    @paulrouxel7526 ปีที่แล้ว +1

    Excellent video !!

  • @joeljacob4546
    @joeljacob4546 ปีที่แล้ว +1

    Cool video

  • @heret1c385
    @heret1c385 ปีที่แล้ว +1

    Great video

  • @MegaTpyn1
    @MegaTpyn1 ปีที่แล้ว +2

    Seems like the model doesn't really like strawberries, I noticed that 4 times it recognized strawberries just a little too late, thus failed to slice them. And in some cases it recognized them late but still was able to slice. Maybe with some tuning in that direction it could achieve an even better result

    • @TwoPointCode
      @TwoPointCode  ปีที่แล้ว

      100% agree. I'm not exactly sure why it struggles with strawberries, but I assume it's because the model uses lower quality images for faster inference and has a hard time distinguishing the difference between uncut and cut strawberries. Could definitely use more examples of strawberries.

  • @owendeheer5893
    @owendeheer5893 11 หลายเดือนก่อน

    Not sure what you did, but more than 2 seconds inference on cpu is very long. I have implemented yolo models in industry settings with inference of at most 50 ms cpu. Are you using yolov8-nano? That helps a lot with speed. Idk why you would have such high inference otherwise.

  • @robert_trirop
    @robert_trirop ปีที่แล้ว

    Good job :)!
    I tried to do the exact same thing but on my iPhone. My plan was to film the iPhone with a webcam and slice the fruit using an ESP 32 board emulating a Bluetooth mouse. The mouse emulation works but I did not test the performance (speed/latency) yet. I want to use a GStreamer pipeline to capture the camera stream, process it and do inference. GStreamer can also be used to capture the computer screen and there are pipeline elements (plugins) from NVIDIA to do TensorRT inference right in the speed & latency optimized pipeline (DeepStream SDK). The inference results can be fed into a python script. I already trained the model with about 1500 images. Took quite some time to label them manually. I got surprisingly good results using the smallest sized model (nano). Which size did you use? Mine ran with 5ms inference time on my 1080Ti. Maybe one could predict the future position of the fruits to compensate for the latency. Have you analyzed why your code fails eventually? Is it because the model did not detect a fruit in the few frames where it is visible?

    • @TwoPointCode
      @TwoPointCode  ปีที่แล้ว +1

      Thank you! Oh, wow that sounds interesting. I also used the Nano version of YoloV8 for this project. I wonder if predicting the future position of the fruit is viable, and if so, how much it improves the score. I almost feel like the time it would take to predict the path could end up harming the performance. Based on my testing, the code eventually fails due to either randomly long inference times, fruits not being detected correctly, or fruits being too close to a bomb when above the screen threshold I created.

  • @LUVVEOUS
    @LUVVEOUS ปีที่แล้ว +1

    Could there be used a tragectory prediction for fruits to optimize the cutting path?

    • @TwoPointCode
      @TwoPointCode  ปีที่แล้ว

      Yes, this could be done to improve the cutting accuracy! I decided against doing this due to the complications in finding the same fruit between frames and predicting the downward acceleration as it would need to be done very quickly to see the benefits.

  • @the7ayreal
    @the7ayreal 4 หลายเดือนก่อน

    great video!pls tell me which version of YoloV8 you use

    • @the7ayreal
      @the7ayreal 4 หลายเดือนก่อน

      pls❤

    • @TwoPointCode
      @TwoPointCode  4 หลายเดือนก่อน

      I used the YOLO-V8 Nano object detection model since the inference speed is important.

    • @the7ayreal
      @the7ayreal 4 หลายเดือนก่อน

      @@TwoPointCode thanks I know it's nano, but I want to ask the ultralytics version

  • @the7ayreal
    @the7ayreal 4 หลายเดือนก่อน

    I also have a question. How is pomegranate, a special fruit, distinguished from other fruits? Because pomegranates need to be cut continuously, and other fruits don't

    • @the7ayreal
      @the7ayreal 4 หลายเดือนก่อน

      do have a look at it pls

    • @TwoPointCode
      @TwoPointCode  4 หลายเดือนก่อน

      I actually didn't label the pomegranate differently. Instead, I trained the model to detect the pomegranate as a fruit, even when the game zooms into it. This allows the code to detect and cut it as a fruit, but because the pomegranate doesn't split in half or change after being cut, it continues to be detected as a fruit and cut the pomegranate until it explodes.

  • @RemoteAccessGG
    @RemoteAccessGG 11 หลายเดือนก่อน +1

    Hey! What GPU do you have that it runs in only 18 milliseconds)

    • @TwoPointCode
      @TwoPointCode  11 หลายเดือนก่อน

      I have a 2060 Super

  • @fortun8diamond
    @fortun8diamond 4 หลายเดือนก่อน +1

    Now make it play arcade and zen

  • @cklangat7118
    @cklangat7118 ปีที่แล้ว +1

    Where can I find the code?

    • @TwoPointCode
      @TwoPointCode  ปีที่แล้ว

      Originally wasn't planning to release the code, but because you and another person are interested in it, I have added a link to it in the description. If you decide to use it, the screen coordinates will most likely need to be changed to work on your monitor!

  • @LOKI101-zt2dy
    @LOKI101-zt2dy ปีที่แล้ว +1

    Bro give the code for it.
    I want to try it out.😁😁

    • @TwoPointCode
      @TwoPointCode  ปีที่แล้ว

      Wasn't going to release the code due to the dependency on capturing specific parts of the screen and the problems that can arise with different Cuda, Python, and Pytorch versions, but I have just updated the description to include a link to the code. If you decide to use it, the screen coordinates will most likely need to be changed to work on your monitor!

    • @LOKI101-zt2dy
      @LOKI101-zt2dy ปีที่แล้ว +1

      @@TwoPointCode You are the man 😍😍
      But I am not into any coding.I thought I can execute your given code directly on my pc.But you said I have to tweak some of it which I don't think I am capable of.
      But anyways you shared your code without any hesitation made my day...THANKS👌👌👍👍

  • @reallight_
    @reallight_ ปีที่แล้ว

    Seems like you stole this from CodeBullet

    • @TwoPointCode
      @TwoPointCode  ปีที่แล้ว +2

      I’m actually not aware of a CodeBullet video about fruit ninja. What is the title of it?