You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: Add detailed comments to prediction notebook
Added comprehensive comments explaining the image preprocessing pipeline, model loading, and prediction logic to improve code documentation and understanding.
Copy file name to clipboardExpand all lines: Model/prediction.ipynb
+10-44Lines changed: 10 additions & 44 deletions
Original file line number
Diff line number
Diff line change
@@ -10,71 +10,37 @@
10
10
},
11
11
{
12
12
"cell_type": "code",
13
-
"execution_count": 1,
13
+
"execution_count": null,
14
14
"id": "c6bc823e",
15
15
"metadata": {},
16
16
"outputs": [],
17
-
"source": [
18
-
"import numpy as np\n",
19
-
"import cv2\n",
20
-
"import keras\n",
21
-
"import os"
22
-
]
17
+
"source": "# Import necessary libraries\nimport numpy as np # For numerical operations and array handling\nimport cv2 # OpenCV for image processing\nimport keras # For loading the trained model\nimport os # For file path operations"
"source": "# Preprocessing function for user input images\ndef image(path):\n # Read the image in grayscale mode\n img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n\n # Resize the image to 224x224 pixels to match model input requirements\n new_arr = cv2.resize(img, (224, 224))\n\n # Normalize pixel values to range [0, 1] by dividing by 255\n new_arr = np.array(new_arr)/255\n\n # Reshape to match model input shape: (batch_size, height, width, channels)\n # -1 means infer batch size, 1 channel for grayscale\n new_arr = new_arr.reshape(-1, 224, 224, 1)\n\n return new_arr"
"if result[0][0] > result[0][1] and result[0][0] > result[0][2]:\n",
70
-
" prediction = 'Mammootty'\n",
71
-
"elif result[0][1] > result[0][0] and result[0][1] > result[0][2]:\n",
72
-
" prediction = 'Mohanlal'\n",
73
-
"else:\n",
74
-
" prediction = 'I dont know this guy!'\n",
75
-
"\n",
76
-
"print(prediction)"
77
-
]
42
+
"outputs": [],
43
+
"source": "# Get prediction probabilities for the test image\nresult = model.predict([image('test_images/mohanlal.png')])\n\n# Interpret the prediction results\n# Model outputs 3 probabilities: [Mammootty, Mohanlal, Unknown]\n# Compare probabilities to determine which class has the highest confidence\nif result[0][0] > result[0][1] and result[0][0] > result[0][2]:\n prediction = 'Mammootty'\nelif result[0][1] > result[0][0] and result[0][1] > result[0][2]:\n prediction = 'Mohanlal'\nelse:\n # If the third class has highest probability, the person is unrecognized\n prediction = 'I dont know this guy!'\n\nprint(prediction)"
0 commit comments