Real time Hand digit recognition with OpenCV

Hi,
I’ve trained a CNN with two layers and got 98% test accuracy. However, I wanted to try to do real time digit recognition and tried to incorporate OpenCV.

This implementation produces terrible accuracy and usually predicts the number incorrectly. Can somebody point out what it is that I am doing incorrectly?
This is the code I’ve written so far. https://pastebin.com/tt8qStjj

I know this is not a question directly related to pytorch but I thought I might find some help on these forums.

If you train a network in some dataset does not mean at all that will have good performance in real world. Did you trained with mnist?

Yes, that’s right I was using mnist. Do you suggest I use another dataset?

Or could it be my implementation of OpenCV is incorrect?

make sure you are doing the same image transformation in training and test. The opencv implementation seems ok