# Read the ImageNet class names.
with open('input/classification_classes_ILSVRC2012.txt', 'r') as f:
image_net_names = f.read().split('\n')
# Save the names of all possible classifications, removing empty final line.
class_names = image_net_names[:-1]
# Verify the size, and inspect one of the classes by name.
print(len(class_names), class_names[0])
# Loading the Classification model.
config_file = 'models/DenseNet_121.prototxt'
model_file = 'models/DenseNet_121.caffemodel'
model = cv2.dnn.readNet(model=model_file, config=config_file, framework='Caffe')
# Load and display the image from disk.
tiger_img = cv2.imread('input/image1.jpg')
plt.figure(figsize=[10, 10])
plt.imshow(tiger_img[:, :, ::-1]);
# Create blob from image.
blob = cv2.dnn.blobFromImage(
image=tiger_img, scalefactor=0.017, size=(224, 224), mean=(104, 117, 123), swapRB=False, crop=False)
# Set the input blob for the neural network.
model.setInput(blob)
# Pass the blob forward through the network.
outputs = model.forward()
final_outputs = outputs[0]
# Make all the outputs 1D, where each represents likihood of matching one of the 1K classification groups.
final_outputs = final_outputs.reshape(1000, 1)
# Get the class label index with the max confidence.
label_id = np.argmax(final_outputs)
# Convert score to probabilities for all matches.
probs = np.exp(final_outputs) / np.sum(np.exp(final_outputs))
print(probs[:10])
print("Max probability:", np.max(probs))
# Get the final highest probability
final_prob = np.max(probs) * 100.0
# Map the max confidence to the class label names.
out_name = class_names[label_id]
out_text = f"{out_name}, {final_prob:.3f}%"
# Display the image, best matched classification, and confidence.
plt.imshow(tiger_img[:, :, ::-1])
plt.title(out_text)
plt.xticks([]), plt.yticks([])
plt.show()