forked from HitanshuSoni/computer_vision_YT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.py
More file actions
34 lines (23 loc) · 1.01 KB
/
filter.py
File metadata and controls
34 lines (23 loc) · 1.01 KB
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
import cv2
import numpy as np
from PIL import Image
from streamlit_webrtc import VideoTransformerBase, webrtc_streamer
import streamlit as st
st.title('Thug Life Filter')
class VideoTransformer(VideoTransformerBase):
def transform(self, image):
image = image.to_ndarray(format="bgr24")
maskPath = 'mask.png'
harcasPath = 'haarcascade_frontalface_default.xml'
faceCascade = cv2.CascadeClassifier(harcasPath)
mask = Image.open(maskPath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, 2.1)
background = Image.fromarray(image)
for (x, y, w, h) in faces:
resized_mask = mask.resize((w, h), Image.ANTIALIAS)
offset = (x, y)
background.paste(resized_mask, offset, mask=resized_mask)
thug_filter = np.asarray(background)
return thug_filter
webrtc_streamer(key="example", video_transformer_factory=VideoTransformer)