Computer Vision Based Quality Control Using Python

Pooja Juyal is manager at Samtel Avionics Ltd

12700
Advertisement

You can run it directly from the installed applications by clicking its logo. Or, write IDLE in the search tab on the Windows start page to get the option to be clicked. Python is an interpreted language, so you can immediately start writing the commands and press ‘Enter.’ The commands get executed when you press ‘Enter.’ Test by typing 2+2 and then press ‘Enter.’ You should get four as the answer in the next line.

Create new program

Run Python IDLE->File->New File

Now you can start writing the program in the IDLE. The complete program is provided in Quality Control.py file as shown in Fig. 8. Copy the code from this file or use the file as it is, because Python is an indentation-sensitive language.

Python code
Fig. 8: Python code

Import modules

Modules required in the program are:
1. from SimpleCV import * # importing everything from SimpleCV
2. import winsound # importing winsound to play sound

The winsound module comes prepackaged with Python, while we have already installed SimpleCV using aforementioned steps.

Functions used

Initialise camera. You need to be sure that your camera is detected as 0 or 1 and update the program accordingly as given below:

Cam = Camera(0) # initialise the camera

Peak hue level.

Enter peak hue level of the colour to be detected. In this project, tablets used had the peak hue of 11.0. You need to find it out for your tablets and update the same in the code as given below:

[stextbox id=”info”]peakHue = 11.0[/stextbox]

The software counts the number of tablets by detecting the objects matching the predefined colour and separating them from the rest of the scene as shown in Fig. 11.

Here we have used an HSV system instead of RGB as it performs better in such applications.

To find out the hue value for the tablets you are using, run the following code:
from SimpleCV

[stextbox id=”info”]import *
cam = Camera(0)
while True:
img = cam.getImage()
peak = img.huePeaks()
print peak[/stextbox]

A separate hue.py Python file is also provided for hue. Now, put the tablet pack under the camera and run this code. Ensure optimal lighting as excessive light will reflect off the silver background of tablets to create false results. When you run the code, you will start to see many numbers. Just note the first one in the list. That is the hue value for your tablets.

Capture image.

The following function captures the image (Fig. 9) of the tablet pack and scales it down to 320×240 resolution:

img = cam.getImage().scale(320,240)

Image of tablets captured by the program
Fig. 9: Image of tablets captured by the program

Hue distance image.

The function below creates another image that indicates the hue distance for the value you selected:

[stextbox id=”info”]imgdist = img.hueDistance(peakHue)[/stextbox]

The tablets should look black (Fig. 10) because the difference is zero there. If not, you have not selected the right hue value.

Hue of the tablets
Fig.10: Hue of the tablets

Inverted image.

The tablets will become white (Fig. 11) after this command:

[stextbox id=”info”]imgdistbin = imgdist.invert().
threshold(200)[/stextbox]

Now you can detect them as blobs. Threshold of 200 is selected after hit-and-trial so that white region is clearly visible.

Inverted image of the tablets
Fig. 11: Inverted image of the tablets

Blobs detection.

Blobs are continuous regions of similar light-coloured pixels. Once blobs are detected, you can apply many operations on them, to find their area, length, angle, etc. You can even count them.

For example, in the following command, Minsize=1000 indicates that you want the system to detect only those blobs which are above size 1000:

[stextbox id=”info”]blobs = imgdistbin.findBlobs(minsize=1000)[/stextbox]

This is also selected via hit-and-trial method such that you only get tablets area as blobs. The function detects all blobs and their co-ordinates are stored in a matrix called blobs.

Display all images.

The following function shows all the processed images side by side (Fig. 12):

[stextbox id=”info”]result = img.sideBySide(imgdist,
side=’bottom’, scale=False)
result = result.sideBySide(imgdistbin,
side=’right’)
result.show()[/stextbox]
Display of all images
Fig. 12: Display of all images

Count blobs.

The number of blobs is equal to the number of tablets. Below function counts the number of blobs:

[stextbox id=”info”]if blobs is not None:
le = len(blobs)
print ‘Pills Detected =’, le[/stextbox]

The first statement helps the code to keep running even if no blob is detected. Otherwise, the program will terminate in the next step. The second statement counts the length of the matrix, which is equal to the number of blobs. The third statement prints the number of blobs detected.

Check and compare the blobs detected.

The code for this purpose follows:

[stextbox id=”info”]if le == 3:
winsound.Beep(1000, 100)
print ‘OK’
else:
print ‘FAIL’[/stextbox]

The first statement compares the number of detected blobs with the prefixed number of pills in the pack. Here, the fixed value is entered as 3 because three-tablet blister packs were used for testing purpose. You need to change the number as required. The second statement plays a beep (frequency, time) if the number of blobs detected is 3 and the third statement prints OK. Otherwise, correct blob detection fails.

Advertisement


2 COMMENTS

SHARE YOUR THOUGHTS & COMMENTS

Please enter your comment!
Please enter your name here