Implementing a module to train the ANN

Training an ANN based on MNIST is something you might want to do in future projects as well. To make our code more reusable, we can write a Python module that is solely dedicated to this training process. Then (in the next section, Implementing the main module), we will import this training module into a main module, where we will implement our demonstration of digit detection and classification.

Let's implement the training module in a file called digits_ann.py:

  1. To begin, we will import the gzip and pickle modules from the Python standard library. As usual, we will also import OpenCV and NumPy:
import gzipimport pickleimport cv2import numpy as np

We will use the gzip and pickle modules to decompress ...

Get Learning OpenCV 4 Computer Vision with Python 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.