{ "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": [ "" ] }, "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 }