Hi, this is Nayon.
ShapeAnnotator (
shapes-v1.pkl) is my first machine learning–based AI model that can identify basic geometrical shapes such as triangle, circle, and rectangle.
For best accuracy, you should use pure black-and-white images as shown in the example.
Here's the python code and example.
1import matplotlib.pyplot as plt
2import joblib
3import cv2
4
5model=joblib.load('give the model (shapes-v1.pkl) path here')
6shape = {1:'circle',
7 2:'rectangle',
8 3:'triangle'}
9image = cv2.imread('give the image path here',cv2.IMREAD_GRAYSCALE)
10edges = cv2.Canny(image, 0,255)
11contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
12pic=image.copy()
13for i in range(0,len(contours),1):
14 x, y, w, h = cv2.boundingRect(contours[i])
15 cropped=image[y:y+h, x:x+w]
16 cropped=cv2.resize(cropped,(32,32))
17 cropped=cropped/255.0
18 cropped=cropped.reshape(1,-1)
19 result=model.predict(cropped)
20 cv2.putText(pic,
21 shape[result[0]],
22 (x,y),
23 cv2.FONT_HERSHEY_SIMPLEX,
24 w/100,
25 (100, 100, 100),
26 2)
27plt.imshow(pic)