import cv2
import numpy as np
import os
import math
def distance(v1,v2):
return math.sqrt(((v1-v2)**2).sum())
def KNN(train,test,k=5):
dist=[]
for i in range(train.shape[0]):
#get vector and label
ix = train[i,:-1]
iy = train[i,-1]
d = distance(test,ix)
dist.append([d,iy])
# Sort based on distance and get top k
dk = sorted(dist, key = lambda x:x[0])[:k]
labels = np.array(dk)[:-1]
#get frequency of each element
output = np.unique(labels,return_counts=True)
#Getting max frequency and corresponding label
index = np.argmax(output[1])
return output[0][index]
#Initialise Camera
cap = cv2.VideoCapture
#Face Detection
face = cv2.CascadeClassifier(‘haarcascade_frontalface_alt.xml’)
skip = 0
dataset_path = ‘D:/Java_Projects/Machine Learning/’
face_data = []
labels = []
class_id = [] #label for given file
names = {} # mapping between id and name
for fx in os.listdir(dataset_path):
if fx.endswith(’.npy’):
print(“loaded” + fx)
data_item = np.load(dataset_path+fx)
face_data.append(data_item)
target = class_id*np.ones((data_item.shape[0],))
class_id +=1
labels.append(target)
face_dataset = np.concatenate(face_data,axis = 0)
face_labels = np.concatenate(labels,axis = 0)
print(face_dataset.shape)
print(face_labels.shape)
While using the target = class_id*np.ones((data_item.shape[0],))
line , the following error occurs: ValueError: operands could not be broadcast together with shapes (0,) (15,)