mouth_train.ipynb 1.86 KB
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import os \n",
    "import random\n",
    "import string\n",
    "\n",
    "face_class = cv2.CascadeClassifier('../pre_trained/haarcascade_frontalface_default.xml')\n",
    "mouth_class = cv2.CascadeClassifier('../pre_trained/haarcascade_mcs_mouth.xml')\n",
    "\n",
    "for file in os.listdir('../data/mouth/raw_no_yawn'):\n",
    "  img = cv2.imread(f\"../data/mouth/raw_no_yawn/{file}\")\n",
    "  face = face_class.detectMultiScale(img)\n",
    "\n",
    "  for (fx,fy,fw,fh) in face:\n",
    "    face_img = img[fy:fy+fh, fx:fx+fw]\n",
    "    mouth = mouth_class.detectMultiScale(img)\n",
    "\n",
    "    for (x,y,w,h) in mouth:\n",
    "      cropped_img = img[y:y+h, x:x+w]\n",
    "      random_name = \"\".join(random.choice(string.ascii_letters) for _ in range(5))\n",
    "      # cv2.imwrite(\"../data/mouth/no_yawn/\" + \"\".join(random.choice(string.ascii_letters) for _ in range(5)) + \"_\" + file, cropped_img)\n",
    "      cv2.imwrite(\"../data/mouth/no_yawn/\" + f\"{file.split('.')[0]}_{random_name}.jpg\", cropped_img)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "d9dade41cb61ad4dc71015ff9c058e4e51f62a5c87dfb0aca90e14591e928332"
  },
  "kernelspec": {
   "display_name": "Python 3.9.12 ('drowsiness_detector')",
   "language": "python",
   "name": "python3"
  },
  "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.9.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}