|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "id": "O9kr2EPop_lf" |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "# deep face" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": null, |
| 15 | + "metadata": { |
| 16 | + "id": "UHTVi3xHrLCb" |
| 17 | + }, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "!pip install deepface" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": null, |
| 26 | + "metadata": { |
| 27 | + "id": "xQEBd7Mnp9c8" |
| 28 | + }, |
| 29 | + "outputs": [], |
| 30 | + "source": [ |
| 31 | + "from deepface import DeepFace\n", |
| 32 | + "import cv2\n", |
| 33 | + "from google.colab.patches import cv2_imshow\n", |
| 34 | + "\n", |
| 35 | + "\n", |
| 36 | + "img = cv2.imread(\"/content/input3.png\")\n", |
| 37 | + "analysis = DeepFace.analyze(img, actions=['age'], enforce_detection=False)\n", |
| 38 | + "age = analysis[0]['age']\n", |
| 39 | + "cv2.putText(img, f\"Age: {age}\", (50, 50), cv2.FONT_HERSHEY_SIMPLEX,\n", |
| 40 | + " 1, (0, 255, 0), 2)\n", |
| 41 | + "cv2_imshow(img)\n" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "markdown", |
| 46 | + "metadata": { |
| 47 | + "id": "zo1QbV1YqxFv" |
| 48 | + }, |
| 49 | + "source": [ |
| 50 | + "# opencv" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": null, |
| 56 | + "metadata": { |
| 57 | + "id": "vow9EhJKuhPh" |
| 58 | + }, |
| 59 | + "outputs": [], |
| 60 | + "source": [ |
| 61 | + "!wget -O opencv_face_detector_uint8.pb https://github.com/spmallick/learnopencv/raw/master/AgeGender/opencv_face_detector_uint8.pb\n", |
| 62 | + "!wget -O opencv_face_detector.pbtxt https://github.com/spmallick/learnopencv/raw/master/AgeGender/opencv_face_detector.pbtxt\n", |
| 63 | + "!wget -O age_deploy.prototxt https://github.com/spmallick/learnopencv/raw/master/AgeGender/age_deploy.prototxt\n", |
| 64 | + "!wget -O age_net.caffemodel https://github.com/spmallick/learnopencv/raw/master/AgeGender/age_net.caffemodel\n", |
| 65 | + "!wget -O age_net.caffemodel \"https://huggingface.co/AjaySharma/genderDetection/resolve/5cde30ccaa3ebff2e3d06876bf6412f19be183c3/age_net.caffemodel\"" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": null, |
| 71 | + "metadata": { |
| 72 | + "id": "5Afwlsj5Cp7V" |
| 73 | + }, |
| 74 | + "outputs": [], |
| 75 | + "source": [ |
| 76 | + "import cv2\n", |
| 77 | + "import numpy as np\n", |
| 78 | + "from google.colab.patches import cv2_imshow\n", |
| 79 | + "\n", |
| 80 | + "face_proto = \"/content/Gender-and-Age-Detection/opencv_face_detector.pbtxt\"\n", |
| 81 | + "face_model = \"/content/Gender-and-Age-Detection/opencv_face_detector_uint8.pb\"\n", |
| 82 | + "age_proto = \"/content/Gender-and-Age-Detection/age_deploy.prototxt\"\n", |
| 83 | + "age_model = \"/content/Gender-and-Age-Detection/age_net.caffemodel\"\n", |
| 84 | + "\n", |
| 85 | + "face_net = cv2.dnn.readNetFromTensorflow(face_model, face_proto)\n", |
| 86 | + "age_net = cv2.dnn.readNetFromCaffe(age_proto, age_model)\n", |
| 87 | + "\n", |
| 88 | + "AGE_BUCKETS = [\"(0-2)\", \"(4-6)\", \"(8-12)\", \"(15-20)\",\n", |
| 89 | + " \"(25-32)\", \"(38-43)\", \"(48-53)\", \"(60-100)\"]\n", |
| 90 | + "MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746)\n", |
| 91 | + "\n", |
| 92 | + "img = cv2.imread(\"/content/input.png\")\n", |
| 93 | + "h, w = img.shape[:2]\n", |
| 94 | + "\n", |
| 95 | + "blob = cv2.dnn.blobFromImage(img, 1.0, (300, 300), [104, 117, 123])\n", |
| 96 | + "face_net.setInput(blob)\n", |
| 97 | + "detections = face_net.forward()\n", |
| 98 | + "\n", |
| 99 | + "for i in range(detections.shape[2]):\n", |
| 100 | + " confidence = detections[0, 0, i, 2]\n", |
| 101 | + " if confidence > 0.5:\n", |
| 102 | + " x1, y1, x2, y2 = (detections[0, 0, i, 3:7] * np.array([w, h, w, h])).astype(int)\n", |
| 103 | + "\n", |
| 104 | + "\n", |
| 105 | + " x1, y1, x2, y2 = max(0, x1), max(0, y1), min(w, x2), min(h, y2)\n", |
| 106 | + " face = img[y1:y2, x1:x2]\n", |
| 107 | + "\n", |
| 108 | + " if face.size > 0:\n", |
| 109 | + " blob = cv2.dnn.blobFromImage(face, 1.0, (227, 227), MODEL_MEAN_VALUES, swapRB=False)\n", |
| 110 | + " age_net.setInput(blob)\n", |
| 111 | + " age_preds = age_net.forward()\n", |
| 112 | + " age = AGE_BUCKETS[age_preds[0].argmax()]\n", |
| 113 | + "\n", |
| 114 | + " cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)\n", |
| 115 | + " cv2.putText(img, f'Age: {age}', (x1, y1 - 10),\n", |
| 116 | + " cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)\n", |
| 117 | + "\n", |
| 118 | + "cv2_imshow(img)" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "markdown", |
| 123 | + "metadata": { |
| 124 | + "id": "cUkB678gAfWi" |
| 125 | + }, |
| 126 | + "source": [ |
| 127 | + "# smahesh29/Gender-and-Age-Detection" |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "code", |
| 132 | + "execution_count": null, |
| 133 | + "metadata": { |
| 134 | + "id": "OYY5XRC6vy4Q" |
| 135 | + }, |
| 136 | + "outputs": [], |
| 137 | + "source": [ |
| 138 | + "!git clone https://github.com/smahesh29/Gender-and-Age-Detection.git\n", |
| 139 | + "%cd Gender-and-Age-Detection\n", |
| 140 | + "!pip install opencv-python argparse\n" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": null, |
| 146 | + "metadata": { |
| 147 | + "colab": { |
| 148 | + "base_uri": "https://localhost:8080/" |
| 149 | + }, |
| 150 | + "id": "lieISP3pBD4l", |
| 151 | + "outputId": "bb0d3c6b-b3c0-4117-aa69-f9c631f23266" |
| 152 | + }, |
| 153 | + "outputs": [], |
| 154 | + "source": [ |
| 155 | + "%cd Gender-and-Age-Detection" |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "code", |
| 160 | + "execution_count": null, |
| 161 | + "metadata": { |
| 162 | + "colab": { |
| 163 | + "base_uri": "https://localhost:8080/" |
| 164 | + }, |
| 165 | + "id": "uai0V9Q4Ae69", |
| 166 | + "outputId": "62ac4df6-a5aa-43c4-e0e0-106598f33c47" |
| 167 | + }, |
| 168 | + "outputs": [], |
| 169 | + "source": [ |
| 170 | + "!python detect.py --image /content/input3.png" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "markdown", |
| 175 | + "metadata": { |
| 176 | + "id": "BRzSgcZgDT3w" |
| 177 | + }, |
| 178 | + "source": [ |
| 179 | + "# yu4u/age-gender-estimation" |
| 180 | + ] |
| 181 | + }, |
| 182 | + { |
| 183 | + "cell_type": "code", |
| 184 | + "execution_count": null, |
| 185 | + "metadata": { |
| 186 | + "id": "bdGjpEUYAe2S" |
| 187 | + }, |
| 188 | + "outputs": [], |
| 189 | + "source": [ |
| 190 | + "!git clone https://github.com/yu4u/age-gender-estimation.git\n", |
| 191 | + "%cd age-gender-estimation\n", |
| 192 | + "!pip install tensorflow keras opencv-python numpy\n" |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "code", |
| 197 | + "execution_count": null, |
| 198 | + "metadata": { |
| 199 | + "colab": { |
| 200 | + "base_uri": "https://localhost:8080/" |
| 201 | + }, |
| 202 | + "id": "bfFpsB_mDdZ2", |
| 203 | + "outputId": "9aaea709-6014-4f85-98cc-e01e80aa0455" |
| 204 | + }, |
| 205 | + "outputs": [], |
| 206 | + "source": [ |
| 207 | + "!python demo.py --image_dir /content/input.png" |
| 208 | + ] |
| 209 | + }, |
| 210 | + { |
| 211 | + "cell_type": "markdown", |
| 212 | + "metadata": { |
| 213 | + "id": "yOBonjHoGloR" |
| 214 | + }, |
| 215 | + "source": [ |
| 216 | + "# MiVOLO" |
| 217 | + ] |
| 218 | + }, |
| 219 | + { |
| 220 | + "cell_type": "code", |
| 221 | + "execution_count": null, |
| 222 | + "metadata": { |
| 223 | + "colab": { |
| 224 | + "base_uri": "https://localhost:8080/" |
| 225 | + }, |
| 226 | + "id": "GbKTjF6fFFIC", |
| 227 | + "outputId": "765930ec-61d2-4da6-a50a-563636c28b03" |
| 228 | + }, |
| 229 | + "outputs": [], |
| 230 | + "source": [ |
| 231 | + "!git clone https://github.com/WildChlamydia/MiVOLO.git\n", |
| 232 | + "%cd MiVOLO\n", |
| 233 | + "!pip install -r requirements.txt\n", |
| 234 | + "!pip install .\n" |
| 235 | + ] |
| 236 | + }, |
| 237 | + { |
| 238 | + "cell_type": "code", |
| 239 | + "execution_count": 5, |
| 240 | + "metadata": { |
| 241 | + "id": "xh1a6IO1QkNx" |
| 242 | + }, |
| 243 | + "outputs": [], |
| 244 | + "source": [ |
| 245 | + "!mkdir /content/MiVOLO/models" |
| 246 | + ] |
| 247 | + }, |
| 248 | + { |
| 249 | + "cell_type": "code", |
| 250 | + "execution_count": null, |
| 251 | + "metadata": { |
| 252 | + "colab": { |
| 253 | + "base_uri": "https://localhost:8080/" |
| 254 | + }, |
| 255 | + "id": "dYrglLLKILC2", |
| 256 | + "outputId": "f8124239-624b-423b-ef94-affd331c4bea" |
| 257 | + }, |
| 258 | + "outputs": [], |
| 259 | + "source": [ |
| 260 | + "\n", |
| 261 | + "!gdown https://drive.google.com/uc?id=1CGNCkZQNj5WkP3rLpENWAOgrBQkUWRdw -O models/yolov8x_person_face.pt\n", |
| 262 | + "!gdown https://drive.google.com/uc?id=11i8pKctxz3wVkDBlWKvhYIh7kpVFXSZ4 -O models/mivolo_imbd.pth.tar\n", |
| 263 | + "\n" |
| 264 | + ] |
| 265 | + }, |
| 266 | + { |
| 267 | + "cell_type": "code", |
| 268 | + "execution_count": null, |
| 269 | + "metadata": { |
| 270 | + "colab": { |
| 271 | + "base_uri": "https://localhost:8080/" |
| 272 | + }, |
| 273 | + "id": "gC_x5Us_PCax", |
| 274 | + "outputId": "01ef1b51-f4bb-451a-a5fe-81b9cc430d28" |
| 275 | + }, |
| 276 | + "outputs": [], |
| 277 | + "source": [ |
| 278 | + "%cd MiVOLO" |
| 279 | + ] |
| 280 | + }, |
| 281 | + { |
| 282 | + "cell_type": "code", |
| 283 | + "execution_count": null, |
| 284 | + "metadata": { |
| 285 | + "id": "G2YDJD_2RPtJ" |
| 286 | + }, |
| 287 | + "outputs": [], |
| 288 | + "source": [ |
| 289 | + "#add in demo file\n", |
| 290 | + "import torch\n", |
| 291 | + "\n", |
| 292 | + "_old_torch_load = torch.load\n", |
| 293 | + "\n", |
| 294 | + "def torch_load_force_weights_only_false(*args, **kwargs):\n", |
| 295 | + " if 'weights_only' not in kwargs:\n", |
| 296 | + " kwargs['weights_only'] = False\n", |
| 297 | + " return _old_torch_load(*args, **kwargs)\n", |
| 298 | + "\n", |
| 299 | + "torch.load = torch_load_force_weights_only_false\n" |
| 300 | + ] |
| 301 | + }, |
| 302 | + { |
| 303 | + "cell_type": "code", |
| 304 | + "execution_count": null, |
| 305 | + "metadata": { |
| 306 | + "colab": { |
| 307 | + "base_uri": "https://localhost:8080/" |
| 308 | + }, |
| 309 | + "id": "m5nElLsnJI92", |
| 310 | + "outputId": "bf3a8bed-6198-428c-e31e-f461b0fb9f4d" |
| 311 | + }, |
| 312 | + "outputs": [], |
| 313 | + "source": [ |
| 314 | + "!python3 demo.py \\\n", |
| 315 | + "--input \"/content/input.png\" \\\n", |
| 316 | + "--output \"/content/output\" \\\n", |
| 317 | + "--detector-weights \"models/yolov8x_person_face.pt \" \\\n", |
| 318 | + "--checkpoint \"models/mivolo_imbd.pth.tar\" \\\n", |
| 319 | + "--device \"cuda:0\" \\\n", |
| 320 | + "--with-persons \\\n", |
| 321 | + "--draw" |
| 322 | + ] |
| 323 | + }, |
| 324 | + { |
| 325 | + "cell_type": "markdown", |
| 326 | + "metadata": {}, |
| 327 | + "source": [ |
| 328 | + "# icatcher" |
| 329 | + ] |
| 330 | + }, |
| 331 | + { |
| 332 | + "cell_type": "code", |
| 333 | + "execution_count": null, |
| 334 | + "metadata": { |
| 335 | + "id": "PE9fcHzoqFRM" |
| 336 | + }, |
| 337 | + "outputs": [], |
| 338 | + "source": [ |
| 339 | + "!pip install icatcher\n", |
| 340 | + "video_path = '/content/video.mp4'\n", |
| 341 | + "!icatcher \"{video_path}\" \\\n", |
| 342 | + " --output_annotation /content/annotations \\\n", |
| 343 | + " --output_video_path /content/output_videos\n" |
| 344 | + ] |
| 345 | + } |
| 346 | + ], |
| 347 | + "metadata": { |
| 348 | + "accelerator": "GPU", |
| 349 | + "colab": { |
| 350 | + "gpuType": "T4", |
| 351 | + "provenance": [] |
| 352 | + }, |
| 353 | + "kernelspec": { |
| 354 | + "display_name": "Python 3", |
| 355 | + "name": "python3" |
| 356 | + }, |
| 357 | + "language_info": { |
| 358 | + "name": "python" |
| 359 | + } |
| 360 | + }, |
| 361 | + "nbformat": 4, |
| 362 | + "nbformat_minor": 0 |
| 363 | +} |
0 commit comments