Spaces:
Runtime error
Runtime error
import cv2 | |
import numpy as np | |
import gradio as gr | |
# Load the cascade classifiers and images | |
glassesCasc = cv2.CascadeClassifier('Train/third-party/frontalEyes35x16.xml') | |
noseCasc = cv2.CascadeClassifier('Train/third-party/Nose18x15.xml') | |
glasses = cv2.imread('Train/glasses.png', cv2.IMREAD_UNCHANGED) | |
mustache = cv2.imread('Train/mustache.png', cv2.IMREAD_UNCHANGED) | |
def apply_effects(frame): | |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
eyes = glassesCasc.detectMultiScale(gray, 1.5, 5, 0) | |
for (x, y, w, h) in eyes: | |
glasses_resized = cv2.resize(glasses, (w, h)) | |
alpha_channel = glasses_resized[:, :, 3] / 255.0 | |
glasses_mask = np.zeros_like(glasses_resized[:, :, 3]) | |
glasses_mask[glasses_resized[:, :, 3] > 0] = 255 | |
for c in range(0, 3): | |
frame[y:y+h, x:x+w, c] = (1 - alpha_channel) * frame[y:y+h, x:x+w, c] + alpha_channel * glasses_resized[:, :, c] | |
nose = noseCasc.detectMultiScale(gray, 1.3, 5, 0) | |
for (x, y, w, h) in nose: | |
mustache_resized = cv2.resize(mustache, (w, h)) | |
alpha_channel = mustache_resized[:, :, 3] / 255.0 | |
mustache_mask = np.zeros_like(mustache_resized[:, :, 3]) | |
mustache_mask[mustache_resized[:, :, 3] > 0] = 255 | |
for c in range(0, 3): | |
frame[y:y+h, x:x+w, c] = (1 - alpha_channel) * frame[y:y+h, x:x+w, c] + alpha_channel * mustache_resized[:, :, c] | |
return frame | |
iface = gr.Interface(fn=apply_effects, inputs="webcam", outputs="image") | |
iface.launch() | |