Skip to content

Commit e79a18e

Browse files
committed
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.
1 parent 1be9f02 commit e79a18e

File tree

1 file changed

+10
-44
lines changed

1 file changed

+10
-44
lines changed

Model/prediction.ipynb

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,71 +10,37 @@
1010
},
1111
{
1212
"cell_type": "code",
13-
"execution_count": 1,
13+
"execution_count": null,
1414
"id": "c6bc823e",
1515
"metadata": {},
1616
"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"
2318
},
2419
{
2520
"cell_type": "code",
26-
"execution_count": 2,
21+
"execution_count": null,
2722
"id": "78b26d23",
2823
"metadata": {},
2924
"outputs": [],
30-
"source": [
31-
"#preprocessing the user input\n",
32-
"def image(path):\n",
33-
" img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n",
34-
" new_arr = cv2.resize(img, (224, 224))\n",
35-
" new_arr = np.array(new_arr)/255\n",
36-
" new_arr = new_arr.reshape(-1, 224,224, 1)\n",
37-
" return new_arr"
38-
]
25+
"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"
3926
},
4027
{
4128
"cell_type": "code",
42-
"execution_count": 3,
29+
"execution_count": null,
4330
"id": "dc422667",
4431
"metadata": {},
4532
"outputs": [],
46-
"source": [
47-
"model = keras.models.load_model('../Server/3-class-improved.h5')"
48-
]
33+
"source": "# Load the pre-trained model from the Server directory\nmodel = keras.models.load_model('../Server/3-class-improved.h5')"
4934
},
5035
{
5136
"cell_type": "code",
52-
"execution_count": 23,
37+
"execution_count": null,
5338
"id": "f0421a50",
5439
"metadata": {
5540
"scrolled": true
5641
},
57-
"outputs": [
58-
{
59-
"name": "stdout",
60-
"output_type": "stream",
61-
"text": [
62-
"mohanlal\n"
63-
]
64-
}
65-
],
66-
"source": [
67-
"result = model.predict([image('test_images/mohanlal.png')])\n",
68-
"\n",
69-
"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)"
7844
},
7945
{
8046
"cell_type": "code",
@@ -111,4 +77,4 @@
11177
},
11278
"nbformat": 4,
11379
"nbformat_minor": 5
114-
}
80+
}

0 commit comments

Comments
 (0)