# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import cv2
import numpy as np

image = cv2.imread('Untitled.jpg')
cv2.imshow('shapes', image)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 

ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

contours, heirarchy = cv2.findContours(thresh,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
len(contours)

cnt_list = []
len(cnt_list)
i = 0

for cnt in contours:
    if i == 0:
        i = 1
        continue
    approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
    i = len(approx)
    cnt_list.append(i)
    if len(approx) == 3:
        shape_name = "Triangle 3"
        cv2.drawContours(image, [cnt], 0, (0, 255, 0), 5)

        #placing text at centre
        M = cv2.moments(cnt)
        cx = int(M['m10'] / M['m00'])
        cy = int(M['m01'] / M['m00'])
        cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1)
    
    elif len(approx) == 5:
        shape_name = "Pentagon 5"
        cv2.drawContours(image, [cnt], -1, (0,255,0), 5)
        #placing text at centre
        M = cv2.moments(cnt)
        cx = int(M['m10'] / M['m00'])
        cy = int(M['m01'] / M['m00'])
        cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1)
        
    elif len(approx) == 6:
        shape_name = "Hexagon 6"
        cv2.drawContours(image, [cnt], -1, (0,255,0), 5)
        #placing text at centre
        M = cv2.moments(cnt)
        cx = int(M['m10'] / M['m00'])
        cy = int(M['m01'] / M['m00'])
        cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1)
        
    elif len(approx) == 8:
        shape_name = "Octagon 8"
        cv2.drawContours(image, [cnt], -1, (0,255,0), 5)
        #placing text at centre
        M = cv2.moments(cnt)
        cx = int(M['m10'] / M['m00'])
        cy = int(M['m01'] / M['m00'])
        cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1)    
    
    elif len(approx) == 12:
        shape_name = "Star 12"
        cv2.drawContours(image, [cnt], -1, (0,255,0), 5)
        #placing text at centre
        M = cv2.moments(cnt)
        cx = int(M['m10'] / M['m00'])
        cy = int(M['m01'] / M['m00'])
        cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1)
        
    elif len(approx) == 4:
        x, y, w, h = cv2.boundingRect(approx)
        aspectRatio = float(w)/h
        if aspectRatio >= 0.5 and aspectRatio <= 1.5:
            shape_name = "Square 4"
            cv2.drawContours(image, [cnt], -1, (0,255,0), 5)
            #placing text at centre
            M = cv2.moments(cnt)
            cx = int(M['m10'] / M['m00'])
            cy = int(M['m01'] / M['m00'])
            cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1)    
        else:
            shape_name = "Rectangle 4"
            cv2.drawContours(image, [cnt], -1, (0,255,0), 5)
            #placing text at centre
            M = cv2.moments(cnt)
            cx = int(M['m10'] / M['m00'])
            cy = int(M['m01'] / M['m00'])
            cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1)
            
    elif len(approx) > 15:
        shape_name = "Circle"
        cv2.drawContours(image, [cnt], -1, (0,255,0), 5)
        #placing text at centre
        M = cv2.moments(cnt)
        cx = int(M['m10'] / M['m00'])
        cy = int(M['m01'] / M['m00'])
        cv2.putText(image, shape_name, (cx-50, cy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1)
     
 
print(len(cnt_list))

cnt_list.sort()
print(len(cnt_list))

for i in cnt_list:
    if i == 3:
        print("triangle")
    elif i == 4:
        print("quadrilateral")
    elif i == 5:
        print("Pentagon")
    elif i == 6:
        print("Hexagon")
    elif i == 8:
        print("Octagon")
    elif i == 12:
        print("Star")
    elif i > 15:
        print("Circle")
        
cv2.imshow("image1", image)
cv2.waitKey(0)
cv2.clearAllWindows()        
        
# -





StarcoderdataJupyterScriptsDedupFiltered