File size: 1,912 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
# %%
import cv2
import numpy as np 

# %%
glasses=cv2.imread('Train/glasses.png',cv2.IMREAD_UNCHANGED)
mustache=cv2.imread('Train/mustache.png',cv2.IMREAD_UNCHANGED)

# %%
glassesCasc=cv2.CascadeClassifier('Train/third-party/frontalEyes35x16.xml')
noseCasc=cv2.CascadeClassifier('Train/third-party/Nose18x15.xml')

def camera():
    cap=cv2.VideoCapture(0)
    while True:
        ret,frame=cap.read()
        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
            
            # Create a mask for the glasses
            glasses_mask = np.zeros_like(glasses_resized[:, :, 3])
            
            # Copy alpha channel to mask and apply threshold
            glasses_mask[glasses_resized[:, :, 3] > 0] = 255
            
            # Overlay the glasses using the mask
            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.1,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])
            
            # Copy alpha channel to mask and apply threshold
            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]

        cv2.imshow('Webcam Feed', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

camera()