File size: 6,161 Bytes
76dfe10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f8d9c166-c713-4e3e-900b-df2d6c6df3b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:TensorFlow GPU support is not available on native Windows for TensorFlow >= 2.11. Even if CUDA/cuDNN are installed, GPU will not be used. Please use WSL2 or the TensorFlow-DirectML plugin.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:absl:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Sequential name=sequential, built=True>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tensorflow.keras.models import load_model\n",
    "\n",
    "model=load_model(\"Face_Detection_Model.h5\")\n",
    "model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "92f623a0-76d0-463e-a7d8-4783296ef2e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras.preprocessing import image\n",
    "import numpy as np\n",
    "\n",
    "test_image = image.load_img(\n",
    "    r\"C:\\Users\\abhis\\Desktop\\Deep Learning\\Deep Learning Project\\Face Detection Project\\images\\train\\Face\\00af34930b6f21da.jpg\",\n",
    "    target_size=(128,128))\n",
    "\n",
    "test_image = image.img_to_array(test_image)\n",
    "test_image = np.expand_dims(test_image, axis=0)\n",
    "test_image = test_image / 255.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6df15bba-8afd-479f-9e56-8abffffe46b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 4s/step\n",
      "[[0.12947011]]\n"
     ]
    }
   ],
   "source": [
    "pred = model.predict(test_image)\n",
    "print(pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "031710e6-077c-482d-aa39-9412ab838578",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Face Detected\n"
     ]
    }
   ],
   "source": [
    "if pred [0][0]>0.5:\n",
    "    print(\"Face Not Detected\")\n",
    "else:\n",
    "    print(\"Face Detected\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "de1d4df7-38de-4bfd-8d82-605a8a904cc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras.preprocessing import image\n",
    "import numpy as np\n",
    "\n",
    "test_image = image.load_img(\n",
    "    r\"C:\\Users\\abhis\\Desktop\\Deep Learning\\Deep Learning Project\\Face Detection Project\\images\\train\\No_Face\\001_0001.jpg\",\n",
    "    target_size=(128,128))\n",
    "\n",
    "test_image = image.img_to_array(test_image)\n",
    "test_image = np.expand_dims(test_image, axis=0)\n",
    "test_image = test_image / 255.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79dba6a6-479c-403a-bfa7-768e2be56564",
   "metadata": {},
   "outputs": [],
   "source": [
    "pred = model.predict(test_image)\n",
    "print(pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80198a51-50d1-4a47-9595-54f5330f728b",
   "metadata": {},
   "outputs": [],
   "source": [
    "if pred [0][0]>0.5:\n",
    "    print(\"Face Not Detected\")\n",
    "else:\n",
    "    print(\"Face Detected\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70d6e1d0-4cca-470e-9bc0-9e5099139aaa",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8aa5e9f5-2fe5-4d84-9f12-dca2f7834335",
   "metadata": {},
   "source": [
    "**WEB CAM OR LIVE FACE DETECTING**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "571476e8-d9ab-4f1a-bad6-43238be047f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "\n",
    "#Load Face Detector\n",
    "face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')\n",
    "\n",
    "#Open Webcam\n",
    "cam = cv2.VideoCapture(1) #1 Means Portable Webcam. In Default Is 0 Means Inbuilt Camera.\n",
    "\n",
    "while True:\n",
    "    ret, frame = cam.read()\n",
    "\n",
    "    if not ret:\n",
    "        break\n",
    "    \n",
    "    #Convert To Grayscale\n",
    "    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)\n",
    "\n",
    "    #Detect Faces\n",
    "    faces = face_cascade.detectMultiScale(gray, scaleFactor = 1.1, minNeighbors = 5)\n",
    "\n",
    "    #If Face Detected\n",
    "    if len(faces)>0:\n",
    "        text = \"Face Detected\"\n",
    "    else:\n",
    "        text = \"Face Not Detected\"\n",
    "\n",
    "    cv2.putText(frame, text, (20,40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)\n",
    "\n",
    "    #Draw Rectangle Around Faces\n",
    "    for (x,y,w,h) in faces:\n",
    "        cv2.rectangle(frame, (x,y), (x+w,y+h), (255,0,0), 2)\n",
    "\n",
    "    cv2.imshow(\"Live Face Detection\", frame)\n",
    "\n",
    "    if cv2.waitKey(1) == 27:\n",
    "        break\n",
    "\n",
    "cam.release()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a0764de-c404-4af1-86e1-1fbe751698db",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}