File size: 1,684 Bytes
b083177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88e24fb
 
 
 
 
 
 
4ac9c22
88e24fb
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
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",
    live=True,  # This enables the live webcam feed
    capture_session=True  # This ensures the webcam feed is properly closed when the interface is closed
)

iface.launch()