AbdelrahmanRadwan / object-detection

A tool used for recognizing and counting the number of occurrences of four different shapes (i.e. square, rectangle, circle, triangle) in a given image.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Shape Recognition

Example 1

Example 2

Example 3

Example 4

Example 5

A tool used for recognizing and counting the number of occurrences of four different shapes (i.e. square, rectangle, circle, triangle) in a given image. [Easily scalable in terms of new shapes]

The tool takes an image path as input and returns three comma separated integers as output:

<int>,<int>,<int>

Where the first integer corresponds to the number of squares in the image, the second integer corresponds to the number of circles, and the third integer corresponds to the number of triangles.

💫 Version 0.1 out now!

📖 Documentation

How to Run

Install the requirements:

pip3 install -r requirements.txt 

To run the main tool:

python3 shapes_counter.py -i <image_path>
example:
python3 shapes_counter.py -i training_data/001979900122A9-0017558-00101E4-001F3DE-00118C8001FC1B0016C37.jpg

and you can also run the visualization tool with any image to see the visual edits and effects happened in the picture:

python3 evaluation/visualization.py  -i <image_path>

example:

python3 evaluation/visualization.py  -i training_data/00170820010925-001146F-0011FF9-001E987-0019C5D001A322001AEF8.jpg

Algorithm

I'm using simple image processing techniques using OpenCV-Python, the algorithm steps:

  1. Do image pre-processing and reduce the noise.
  2. Select the contours from the image.
  3. Select the core approximate corners from each contour.
  4. Determine the shape of the main contour; based on its number of vertices.
  5. Draw the picture with its shapes tagged by their types.
  6. Count the shapes occupancies and print the numbers, comma separated.

I tried to use a training dataset of a lot of labeled pictures, but I had two issues:

  1. The labeling technique seems to be not so useful for me; as if I know exactly the shapes in the picture but I don't know their places, then I can not use this info.
  2. I didn't find a lot of free papers discussing 2D geometric shapes recognition using machine learning and advanced models, most of them are using contours detection and edge detection.

Results

The results are not bad at all! a lot of test cases gonna be true, the algorithm can pass a lot of corner cases.

Example 1 Example 2
Example 1 Example 2
Example 3 Example 4
Example 3 Example 4
Example 5 Example 5
Example 5 Example 6
Example 7 Example 8
Example 7 Example 8

Comparison

Original Image Resultant Image
Example 1-1 Example 1-2
Example 2-1 Example 2-2
Example 3-1 Example 3-2
Example 4-1 Example 4-2

On the other hand, the algorithm fails in some cases:

  1. With small shapes (very small).
  2. With the unionized shapes.
  3. With objects on the image borders.

I run with more than one experiment before reaching this accuracy:

Further work

  1. As a post-processing step, we need to sharpen the shapes after the pre-processing phase, to avoid minimizing the shape which leads to unrecognizable shape with hard to detect edges.
  2. As a post-processing step, we need to separate the shapes from each other and from the borders if they are hardly connected together.
  3. Try using Lattice-Computing (LC) Techniques to enhance the results.
  4. Try Neural Network to detect 2D shapes in images using a GANN approach, This combines the heuristic approach of a Genetic Algorithm, and the precision of gradient descent, to reach optimum convergence.
  5. Use PIL for preprocessing.

About

A tool used for recognizing and counting the number of occurrences of four different shapes (i.e. square, rectangle, circle, triangle) in a given image.

License:MIT License


Languages

Language:Python 100.0%