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.
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 |
The number of corners tells us the shape:
| Corners | Shape |
|---|---|
| 3 | Triangle |
| 4 | Rectangle |
| 5 | Pentagon |
| 6 | Hexagon |
| many | Circle |
- 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
Google Colab (easiest — nothing to install):
- Open the notebook in Google Colab.
- Upload
shapes.pngusing the folder icon on the left. - Run the cells from top to bottom.
On your own computer:
pip install opencv-python numpy matplotlib pillowThen run the notebook in Jupyter.
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")- Shapes not detected? Change the threshold number
110(try90or150). - Outlines too thin? Increase the dilate
iterations. - A shape mis-named? Its outline is probably wobbly — dilate a bit more.
Python · OpenCV · NumPy · Matplotlib · Pillow