<jupyter_start><jupyter_text>Use generated model for prediction: Randomly select images from data_test directory<jupyter_code>import os
import gc
import cv2
import csv
import dlib
import glob
import random
import numpy as np
import tensorflow as tf
detector = dlib.get_frontal_face_detector()
from keras.models import load_model
def get_predictions(img_path, lookup_table, model):
    '''
    Function to get model predictions.
    img_path: image path for prediction
    lookup_table: class labels for inverse encoding
    model: generated model(VGGNet)
    '''
    org = cv2.imread(img_path)
    actual_name = os.path.basename(os.path.dirname(img_path))
    image = cv2.resize(cv2.imread(img_path), (64, 64))
    image = np.expand_dims(image, axis=0)
    pred = np.argmax(model.predict(image), axis=1)[0]
    result = {'Actual_Name': actual_name, 'Prediction': labels[pred]}
    font = cv2.FONT_HERSHEY_PLAIN
    
    window = cv2.namedWindow('Image', cv2.WINDOW_NORMAL)
    cv2.resizeWindow('Image', 900, 900)
    # dlib detector, returns the default face detector
    dets = detector(org, 1)
    for i, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            i, d.left(), d.top(), d.right(), d.bottom()))
    
        cv2.rectangle(org, (d.left(), d.top()), (d.right(), d.bottom()), (255,255,0), 5)
    cv2.putText(org, str(labels[pred]), (d.left(), d.top()-5),
                font, 2, (0, 0, 255), 2, cv2.LINE_AA)
    cv2.imshow('Image', org)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    return result
# directory path 
test_dir = glob.glob(
    './Dataset/**/*.jpg', recursive=True)
# load saved model
model = load_model('model.h5')
with open('class_labels.csv', 'r') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
    labels = [row for row in spamreader]
    labels = labels[0]
# Randomly choose images from test_data directory
idx = random.randint(1, len(test_dir))
# get prediction
get_predictions(test_dir[idx], labels, model)
gc.collect()
tf.keras.backend.clear_session()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup