CoolFace
Datasetpublic

alphuuuu02/Deep-Learning-Face-Detection-Project

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