metadata
license: mit
It's the base code of my ShapeAnnotator's shapes-v1.pkl model. You can download it.
Download python file (shape.py)
you may also copy this code.
import joblib
import numpy as np
import os
import cv2
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
classes = {"circle":1,
"rectangle":2,
"triangle":3 }
X=[]
Y=[]
path1="trainer dataset's folder"
main_folder=os.listdir(path1)
print(main_folder)
for folder in main_folder:
path2=os.path.join(path1,folder)
files=os.listdir(path2)
for file in files:
path3=os.path.join(path2,file)
img=cv2.imread(path3,cv2.IMREAD_GRAYSCALE)
img=cv2.resize(img,(32,32))
edges = cv2.Canny(img, 100, 200)
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(contours[0])
img= img[y:y+h, x:x+w]
img=cv2.resize(img,(32,32))
X.append(img)
Y.append(classes[folder])
X=np.array(X)
Y=np.array(Y)
X=X/255.0
X=X.reshape(300,-1)
model=LogisticRegression(max_iter=1000)
model=model.fit(X,Y)
joblib.dump(model,'model_name')