Sebahadin1234/Facial_Expression_Recognition_Deep_Learning
0
1import streamlit as st2import torch3import torch.nn as nn4import torch.nn.functional as F5 6from PIL import Image7import numpy as np8 9from pytorch_grad_cam.utils.image import show_cam_on_image10 11import random12import matplotlib.pyplot as plt13import torchvision.transforms as transforms14import cv215 16 17 18import models19 20 21 22def Display_prediction(model_choice, label_dict,):23 # Camera input24 img_file = st.camera_input("๐ธ Take a photo to classify")25 # Load the selected model26 if model_choice == "CNN":27 model = models.load_cnn_model()28 elif model_choice == "VGG16":29 model = models.load_vgg_model()30 else:31 model = models.load_vit_model()32 33 34 35 36 if img_file is not None:37 image = Image.open(img_file)38 39 # ๐ Tightly crop the center to focus on the face40 cropped_image = models.tight_center_crop(image, crop_ratio=0.7)41 42 # Show cropped image to user43 st.image(cropped_image, caption="๐ง Tightly Center-Cropped Image")44 45 46 predict = False47 if st.button("๐ง Predict Emotion"):48 predict = True49 if predict:50 st.write("๐ง Predicting...")51 52 input_tensor = models.preprocess_image(cropped_image, model_type=model_choice)53 54 55 # Inference56 with torch.no_grad():57 outputs = model(input_tensor)58 _, predicted = torch.max(outputs, 1)59 predicted_label = predicted.item() + 1 60 61 st.success(f"๐ง Predicted Emotion: **{label_dict[predicted_label]}**")62 63 64 if model_choice == "CNN":65 target_layer = model.conv2 # Adjust to your CNN66 67 # Grad-CAM68 orig, gradcam_img, pred_label = models.apply_gradcam_streamlit(69 model=model,70 input_tensor=input_tensor,71 target_layer=target_layer,72 class_names=label_dict,73 true_label=None74 )75 76 st.subheader("๐ง Grad-CAM Visualization")77 78 79 80 # Convert both images to PIL81 orig_img_pil = Image.fromarray((orig * 255).astype(np.uint8))82 heatmap_img_pil = Image.fromarray(gradcam_img)83 84 # Side-by-side view85 st.image([orig_img_pil, heatmap_img_pil], caption=["Original", "Grad-CAM"], width=300)86 87 88 89 90 if st.button("๐ฒ Show Random Prediction From Test Dataset"):91 model.eval()92 93 test_dataset = models.test_dataset_cnn94 95 96 97 98 99 if model_choice != "CNN":100 test_dataset = models.test_dataset_v101 102 # Pick a truly random image from the whole dataset103 104 105 index_to_label = {i: int(cls) for i, cls in enumerate(test_dataset.classes)} # test_dataset.classes should be strings like ['1', '2', ..., '6']106 107 108 total_samples = len(test_dataset)109 rand_index = random.randint(0, total_samples - 1)110 111 # Load image and label directly112 image, label = test_dataset[rand_index]113 input_tensor = image.unsqueeze(0) # Add batch dimension114 115 # Run prediction116 model.eval()117 with torch.no_grad():118 output = model(input_tensor)119 _, predicted = torch.max(output, 1)120 121 # Convert class index (0-based) to folder label (1-based)122 true_label = int(test_dataset.classes[label])123 predicted_label = int(test_dataset.classes[predicted.item()])124 125 126 127 128 # Convert image for display129 image_disp = image.permute(1, 2, 0).cpu().numpy()130 image_disp = image_disp * 0.5 + 0.5 # unnormalize131 image_disp = np.clip(image_disp, 0, 1)132 133 # Display image using Matplotlib134 fig, ax = plt.subplots(figsize=(6, 6))135 ax.imshow(image_disp)136 ax.set_title(f"โ
True: {label_dict[true_label]}\n๐ค Predicted: {label_dict[predicted_label]}")137 ax.axis("off")138 st.pyplot(fig)139 140 141 142 143 144 if model_choice == "CNN":145 # ----------------------------146 # ๐ง Apply Grad-CAM on Selected Random Image147 # ----------------------------148 149 150 target_layer = model.conv2151 152 # Prepare the single image tensor for Grad-CAM153 input_tensor = image.unsqueeze(0)154 155 # Grad-CAM156 img_disp, gradcam_overlay, _ = models.apply_gradcam_streamlit(157 model=model,158 input_tensor=input_tensor,159 target_layer=target_layer,160 class_names=label_dict,161 true_label=true_label162 )163 164 st.subheader("๐ฅ Grad-CAM on Random Test Image")165 166 # Convert both to displayable format167 orig_pil = Image.fromarray((img_disp * 255).astype(np.uint8))168 heatmap_pil = Image.fromarray(gradcam_overlay)169 170 # Side-by-side in Streamlit171 st.image([orig_pil, heatmap_pil], caption=["Original", "Grad-CAM"], width=300)172 