Home » Python » Python: Detect Numbers in Image Using OpenCV

Python: Detect Numbers in Image Using OpenCV

In Python, you can use the SVM or KNearest features of OpenCV library to detect numbers in an image. Below is an example:

Detect Numbers in an Image Using OpenCV

import sys

import numpy as np
import cv2

im = cv2.imread('numberImage.png')
im3 = im.copy()

gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)

contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

samples =  np.empty((0,100))
responses = []
keys = [i for i in range(48,58)]

for cnt in contours:
    if cv2.contourArea(cnt)>50:
        [x,y,w,h] = cv2.boundingRect(cnt)
        if  h>28:
            roi = thresh[y:y+h,x:x+w]
            roismall = cv2.resize(roi,(10,10))
            key = cv2.waitKey(0)

            if key == 27:  # (escape to quit)
            elif key in keys:
                sample = roismall.reshape((1,100))
                samples = np.append(samples,sample,0)

responses = np.array(responses,np.float32)
responses = responses.reshape((responses.size,1))
print "training complete"


Code Explanation

  •  The code starts by importing the necessary libraries.
  •  It then loads a picture of a number image from disk, converts it to grayscale, and blurs it with Gaussian blur.
  •  The code then finds contours in the image using cv2.findContours().
  •  After finding all the contours, they are sorted into order according to their size (largest first).
  •  The next step is to find out which keys on the keyboard correspond with each number on the range from 48-58.
  •  This is done by looping through every contour and checking if its area exceeds 50 pixels.
  •  If so, it creates an empty list called responses that will contain one value for each key pressed on your keyboard when you press enter at any time during this program's execution.
  •  Then for each contour found in step 1, we check if its height exceeds 28 pixels; if so, we create a rectangle around that point using cv2.rectangle() and show it as white against black background using cv2.imshow('norm',im) .
  •  We also store this information about what was clicked in our variable key , which corresponds with 27 on your keyboard because pressing escape terminates this program prematurely (27 = Esc).
  •  For every other key pressed
  •  The code is a Python script that reads in the "numberImage.png" file, then applies a Gaussian blur to it and finds contours.
  •  It then uses those contours to create an array of points called "samples".
  •  The next step is to find the responses for each point in the array of samples by using a list comprehension.
  •  This will generate an output that looks like this: [0, 0, 1, 1].

I hope the above example with explanation helped you understand the code as well as detect the numbers in an image using the Python OpenCV library.