File size: 3,162 Bytes
b083177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "glasses=cv2.imread('Train/glasses.png',cv2.IMREAD_UNCHANGED)\n",
    "mustache=cv2.imread('Train/mustache.png',cv2.IMREAD_UNCHANGED)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "glassesCasc=cv2.CascadeClassifier('Train/third-party/frontalEyes35x16.xml')\n",
    "noseCasc=cv2.CascadeClassifier('Train/third-party/Nose18x15.xml')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "cap=cv2.VideoCapture(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "while True:\n",
    "    ret,frame=cap.read()\n",
    "    gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)\n",
    "    eyes = glassesCasc.detectMultiScale(gray, 1.5, 5, 0)\n",
    "    for (x, y, w, h) in eyes:\n",
    "        glasses_resized = cv2.resize(glasses, (w, h))\n",
    "        alpha_channel = glasses_resized[:, :, 3] / 255.0\n",
    "        \n",
    "        # Create a mask for the glasses\n",
    "        glasses_mask = np.zeros_like(glasses_resized[:, :, 3])\n",
    "        \n",
    "        # Copy alpha channel to mask and apply threshold\n",
    "        glasses_mask[glasses_resized[:, :, 3] > 0] = 255\n",
    "        \n",
    "        # Overlay the glasses using the mask\n",
    "        for c in range(0, 3):\n",
    "            frame[y:y+h, x:x+w, c] = (1 - alpha_channel) * frame[y:y+h, x:x+w, c] + alpha_channel * glasses_resized[:, :, c]\n",
    "\n",
    "    nose=noseCasc.detectMultiScale(gray,1.3,5,0)\n",
    "    for (x, y, w, h) in nose:\n",
    "        mustache_resized = cv2.resize(mustache, (w, h))\n",
    "        alpha_channel = mustache_resized[:, :, 3] / 255.0\n",
    "        \n",
    "        mustache_mask = np.zeros_like(mustache_resized[:, :, 3])\n",
    "        \n",
    "        # Copy alpha channel to mask and apply threshold\n",
    "        mustache_mask[mustache_resized[:, :, 3] > 0] = 255\n",
    "\n",
    "        for c in range(0, 3):\n",
    "            frame[y:y+h, x:x+w, c] = (1 - alpha_channel) * frame[y:y+h, x:x+w, c] + alpha_channel * mustache_resized[:, :, c]\n",
    "\n",
    "    cv2.imshow('Webcam Feed', frame)\n",
    "\n",
    "    if cv2.waitKey(1) & 0xFF == ord('q'):\n",
    "        break\n",
    "\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.10.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}