{ "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 }