Skip to content

Tanyaagarg/Python_shape_detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Python Shape Detection ⬛⚫

A beginner-friendly Python project that looks at an image of shapes and automatically outlines and names each one — Triangle, Rectangle, Pentagon, Hexagon, or Circle — using OpenCV.

What it does

The program takes a picture of shapes, cleans it up step by step, finds every shape, counts its corners, and writes the shape's name right in the middle of it.

Step What happens
1. Open image Load the picture
2. Greyscale Remove colour, keep only light and dark
3. Threshold Make it pure black & white (shapes become white)
4. Morphology Thicken outlines, close gaps, remove noise
5. Find contours Trace every shape's outline
6. Name shapes Count corners → pick the name → label it

How the naming works

The number of corners tells us the shape:

Corners Shape
3 Triangle
4 Rectangle
5 Pentagon
6 Hexagon
many Circle

Files in this repo

  • code_shape_detection.ipynb — the full commented code (open it in Google Colab)
  • shapes.png — a sample input image
  • Shape Detection Beginners Guide.pdf — a step-by-step guide explaining every line

How to run

Google Colab (easiest — nothing to install):

  1. Open the notebook in Google Colab.
  2. Upload shapes.png using the folder icon on the left.
  3. Run the cells from top to bottom.

On your own computer:

pip install opencv-python numpy matplotlib pillow

Then run the notebook in Jupyter.

Use your own image

Draw a few shapes on white paper with a thick black marker, take a photo, upload it, and change the file name in the first step:

img = Image.open("your_photo.jpg")

Tuning tips

  • Shapes not detected? Change the threshold number 110 (try 90 or 150).
  • Outlines too thin? Increase the dilate iterations.
  • A shape mis-named? Its outline is probably wobbly — dilate a bit more.

Built with

Python · OpenCV · NumPy · Matplotlib · Pillow

About

Detects and labels geometric shapes in an image using Python and OpenCV.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors