i was trying to make as an exercise a model with scikit-learn to recognize images and return a box that surrounds the face. I got an SVC to work, but when trying to make a sliding window (my first time doing it) as result i got the image with multiple boxes that cover pretty much anything except the face, and in non-face pictures they get labeled as “face” inside the sliding window because like 3-4 window think they have a face in it, but when i test the model with the same pictures all works good.
I’m not sure if it’s a problem with the sliding window since i never used one and couldn’t find a source that explains how to do it properly, but i tried:
- Doing a loop for the lenghts of the images (x and y)
- Getting the window with custom size and custom step and resizing it as the model wanted, flatten and reshapen them.
- Then use the model to predict if the window is a face and get the rectangle.
def features_extr(window):
window = resize(window, (50,50))
window = window.flatten()
window = window.reshape(1,-1)
return window
def sliding_window(img,step,window_size,model,label):
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gray = cv2.equalizeHist(img_gray)
for y in tqdm(range(0, img_gray.shape[0] - window_size[1] + 1, step)):
for x in range(0, img_gray.shape[1] - window_size[0] + 1, step):
window = img_gray[y:y + window_size[1], x:x + window_size[0]]
features = features_extr(window)
predict = model.predict(features)[0]
if predict == label:
cv2.rectangle(img, (x, y), (x + window_size[0], y + window_size[1]), (0, 255, 0), 2)
plt.imshow(img)
sliding_window(img, 100, (100,100), gs, "Faces")