Text this: Machine-learning based photon counting for PMT waveforms and its application to the improvement of the energy resolution in large liquid scintillator detectors