Finding average of features generated to create template for prediction of class of object

I extracted features from 200 images of 20 classes. The code return feature extracted (data) and class labels (label) as shown below. I want to find the average of each class of features and use it to create a template for each class of image to predict new input image using distance metrics or KNN.

data = [ ]
label = [ ]

df = pd.DataFrame()
df2 = pd.DataFrame()
df3 = pd.DataFrame()
for dir1 in os.listdir(img_folder):
for file in os.listdir(os.path.join(img_folder, dir1)):
filename = os.path.join(img_folder, dir1, file)
# read image
img = cv2.imread(filename)
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# = FeatureExtractor()
edges=cv2.Canny(image_result, 200, 300)
hog_feature, hog_image = hog(edges, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), block_norm= ‘L2’,visualize=True)

    image_label = os.path.splitext(os.path.basename(dir1))[0]

    # append descriptor and label to train/test data, labels
    data.append(hog_feature)
    
    label.append(image_label)

return data and label

return data, label

© Copyright 2013-2020 Analytics Vidhya