Multi-face Recognition with loaded keras model

Viewed 49

When I load the trained Keras model (VGG16 which has 6 classes), it works as binary classification problem rather than multi-classification. I have tried with several techniques, still it works as binary classification. I need this problem as multi-classification problems with 6 classes. The code is for the problem is below,

import tensorflow as tf
from tensorflow import keras
import numpy as np
import cv2
from keras.models import load_model
import numpy as np

facedetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

cap=cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
font=cv2.FONT_HERSHEY_COMPLEX

model = load_model('model/FAceRec.h5', compile=False)

def get_className(classNo):
    if classNo==0:
        return "Karuna"
    elif classNo==1:
        return "Suhail"
    elif classNo==2:
        return "Uyaam"
    elif classNo==3:
        return "Saftan"
    elif classNo==4:
        return "Ahmad"
    elif classNo==5:
        return "Nikzaad"


while True:
    sucess, imgOrignal=cap.read()
    faces = facedetect.detectMultiScale(imgOrignal,1.3,5)
    for x,y,w,h in faces:
        crop_img=imgOrignal[y:y+h,x:x+h]
        img=cv2.resize(crop_img, (224,224))
        img=img.reshape(1, 224, 224, 3)
        classIndex=model.predict_classes(img)
        probabilityValue = np.argmax(model.predict(img), axis=-1)
        
        if classIndex==0:
            cv2.rectangle(imgOrignal,(x,y),(x+w,y+h),(0,255,0),2)
            cv2.rectangle(imgOrignal, (x,y-40),(x+w, y), (0,255,0),-2)
            cv2.putText(imgOrignal, str(get_className(classIndex)),(x,y-10), font, 0.75, (255,255,255),1, cv2.LINE_AA)
            
        elif classIndex==1:
            cv2.rectangle(imgOrignal,(x,y),(x+w,y+h),(0,255,0),2)
            cv2.rectangle(imgOrignal, (x,y-40),(x+w, y), (0,255,0),-2)
            cv2.putText(imgOrignal, str(get_className(classIndex)),(x,y-10), font, 0.75, (255,255,255),1, cv2.LINE_AA)
        
        elif classIndex==2:
            cv2.rectangle(imgOrignal,(x,y),(x+w,y+h),(0,255,0),2)
            cv2.rectangle(imgOrignal, (x,y-40),(x+w, y), (0,255,0),-2)
            cv2.putText(imgOrignal, str(get_className(classIndex)),(x,y-10), font, 0.75, (255,255,255),1, cv2.LINE_AA)
            
        elif classIndex==3:
            cv2.rectangle(imgOrignal,(x,y),(x+w,y+h),(0,255,0),2)
            cv2.rectangle(imgOrignal, (x,y-40),(x+w, y), (0,255,0),-2)
            cv2.putText(imgOrignal, str(get_className(classIndex)),(x,y-10), font, 0.75, (255,255,255),1, cv2.LINE_AA)
            
        elif classIndex==4:
            cv2.rectangle(imgOrignal,(x,y),(x+w,y+h),(0,255,0),2)
            cv2.rectangle(imgOrignal, (x,y-40),(x+w, y), (0,255,0),-2)
            cv2.putText(imgOrignal, str(get_className(classIndex)),(x,y-10), font, 0.75, (255,255,255),1, cv2.LINE_AA)
            
        elif classIndex==5:
            cv2.rectangle(imgOrignal,(x,y),(x+w,y+h),(0,255,0),2)
            cv2.rectangle(imgOrignal, (x,y-40),(x+w, y), (0,255,0),-2)
            cv2.putText(imgOrignal, str(get_className(classIndex)),(x,y-10), font, 0.75, (255,255,255),1, cv2.LINE_AA)

        cv2.putText(imgOrignal,str(np.round(probabilityValue*100, 2))+"%" ,(180, 75), font, 0.75, (255,0,0),2, cv2.LINE_AA)
        
    cv2.imshow("Face Detection",imgOrignal)
    k=cv2.waitKey(1)
    if k==ord('q'):
        break


cap.release()
cv2.destroyAllWindows()

The code is classifying only two classes and not more than that, the model is trained with 6 classes. How can I refine this code classifying multi faces. What is the problem with this code? can someone try to explain me to sort this out?

0 Answers
Related