The solution is to get a better power supply than 4 AA batteries.
Or a better driver chip than that bipolar L298.
Those yellow motors are low voltage, high current.
It is one of my personal rants that they still put these toy motors in robot gearboxes.
You will get a lot of voltage drop in the driver chip.
By moving to a mosfet driver there is less voltage drop.
But high current motors can draw more power than AA batteries can provide unless they are NiMH or LiPo.
You are basically using crappy toy motors with a crappy driver.
I usually start with 6 or even 8 AA cells and a separate USB power bank for the Pi.
You can get higher voltage, lower current RS130 motors for about $1 each and just swap them for the toy motor in the gearbox.
You need more batteries but they will last longer and the voltage drop means less power wasted in the L298.
If you want to have fun you can pull the motors apart and rewind them with more turns of smaller wire.
That is the main difference between a 3V toy motor and a 12V robot motor.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges