mouth_train.ipynb
1.86 KB
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
{
"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
}