Spaces:
Sleeping
Sleeping
Commit ·
4c5889f
1
Parent(s): 51f6cdc
adding the files
Browse files- app.py +140 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
from cvzone.SelfiSegmentationModule import SelfiSegmentation
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
segmen = SelfiSegmentation()
|
| 9 |
+
|
| 10 |
+
def save_image_to_desktop(image):
|
| 11 |
+
desktop_path = os.path.expanduser("~/Desktop")
|
| 12 |
+
project_folder = os.path.join(desktop_path, "project")
|
| 13 |
+
os.makedirs(project_folder, exist_ok=True)
|
| 14 |
+
timestamp = int(time.time())
|
| 15 |
+
file_name = f"image_{timestamp}.jpg"
|
| 16 |
+
file_path = os.path.join(project_folder, file_name)
|
| 17 |
+
cv2.imwrite(file_path, image)
|
| 18 |
+
return file_path
|
| 19 |
+
|
| 20 |
+
def apply_sepia_filter(frame):
|
| 21 |
+
sepia_kernel = np.array([[0.131, 0.534, 0.272],
|
| 22 |
+
[0.168, 0.686, 0.349],
|
| 23 |
+
[0.189, 0.769, 0.393]])
|
| 24 |
+
sepia_image = cv2.transform(frame, sepia_kernel)
|
| 25 |
+
return np.clip(sepia_image, 0, 255).astype(np.uint8)
|
| 26 |
+
|
| 27 |
+
def apply_emboss_filter(image):
|
| 28 |
+
emboss_kernel = np.array([[-2, -1, 0], [-1, 1, 1], [0, 1, 2]])
|
| 29 |
+
emboss_image = cv2.filter2D(image, -1, emboss_kernel)
|
| 30 |
+
return np.clip(emboss_image, 0, 255).astype(np.uint8)
|
| 31 |
+
|
| 32 |
+
def pixelate(image):
|
| 33 |
+
pixel_size = 10
|
| 34 |
+
height, width = image.shape[:2]
|
| 35 |
+
temp_image = cv2.resize(image, (width // pixel_size, height // pixel_size), interpolation=cv2.INTER_NEAREST)
|
| 36 |
+
return cv2.resize(temp_image, (width, height), interpolation=cv2.INTER_NEAREST)
|
| 37 |
+
|
| 38 |
+
def apply_edge_enhance(image):
|
| 39 |
+
enhanced_edge_kernel = np.array([[-1, -1, -1], [-1, 10, -1], [-1, -1, -1]])
|
| 40 |
+
enhanced_edge_image = cv2.filter2D(image, -1, enhanced_edge_kernel)
|
| 41 |
+
return np.clip(enhanced_edge_image, 0, 255).astype(np.uint8)
|
| 42 |
+
|
| 43 |
+
def brightness_control(image, value):
|
| 44 |
+
return cv2.convertScaleAbs(image, alpha=1, beta=value)
|
| 45 |
+
|
| 46 |
+
def final(option1,option2, frame):
|
| 47 |
+
option1_map = {
|
| 48 |
+
"Person": 1,
|
| 49 |
+
"Background": 2,
|
| 50 |
+
"Whole Frame": 3
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
option2_map = {
|
| 54 |
+
"Blur": 1,
|
| 55 |
+
"Sepia": 2,
|
| 56 |
+
"Pixelate": 3,
|
| 57 |
+
"Emboss": 4,
|
| 58 |
+
"Edge Enhance": 5,
|
| 59 |
+
"Increase Brightness": 'i',
|
| 60 |
+
"Decrease Brightness": 'd'
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
option1 = option1_map.get(option1, 1)
|
| 64 |
+
option2 = option2_map.get(option2, 1)
|
| 65 |
+
|
| 66 |
+
if option1 == 1:
|
| 67 |
+
person = segmen.removeBG(frame, (0, 0, 0), cutThreshold=0.8)
|
| 68 |
+
background = cv2.subtract(frame, person)
|
| 69 |
+
if option2 == 1:
|
| 70 |
+
person = cv2.GaussianBlur(person, (15, 15), 0)
|
| 71 |
+
elif option2 == 2:
|
| 72 |
+
person = apply_sepia_filter(person)
|
| 73 |
+
elif option2 == 3:
|
| 74 |
+
person = pixelate(person)
|
| 75 |
+
elif option2 == 4:
|
| 76 |
+
person = apply_emboss_filter(person)
|
| 77 |
+
elif option2 == 5:
|
| 78 |
+
person = apply_edge_enhance(person)
|
| 79 |
+
elif option2 == 'i':
|
| 80 |
+
person = brightness_control(person,10)
|
| 81 |
+
background = brightness_control(background,-10)
|
| 82 |
+
elif option2 == 'd':
|
| 83 |
+
background = brightness_control(background, -10)
|
| 84 |
+
frame = cv2.add(person, background)
|
| 85 |
+
elif option1 == 2:
|
| 86 |
+
person = segmen.removeBG(frame, (0, 0, 0), cutThreshold=0.8)
|
| 87 |
+
background = cv2.subtract(frame, person)
|
| 88 |
+
if option2 == 1:
|
| 89 |
+
background = cv2.GaussianBlur(background, (15, 15), 0)
|
| 90 |
+
elif option2 == 2:
|
| 91 |
+
background = apply_sepia_filter(background)
|
| 92 |
+
elif option2 == 3:
|
| 93 |
+
background = pixelate(background)
|
| 94 |
+
elif option2 == 4:
|
| 95 |
+
background = apply_emboss_filter(background)
|
| 96 |
+
elif option2 == 5:
|
| 97 |
+
background = apply_edge_enhance(background)
|
| 98 |
+
elif option2 == 'i':
|
| 99 |
+
person = brightness_control(person,-30)
|
| 100 |
+
background = brightness_control(background, 30)
|
| 101 |
+
elif option2 == 'd':
|
| 102 |
+
background = brightness_control(background, -10)
|
| 103 |
+
frame = cv2.add(person, background)
|
| 104 |
+
elif option1 == 3:
|
| 105 |
+
if option2 == 1:
|
| 106 |
+
frame = cv2.GaussianBlur(frame, (15, 15), 0)
|
| 107 |
+
elif option2 == 2:
|
| 108 |
+
frame = apply_sepia_filter(frame)
|
| 109 |
+
elif option2 == 3:
|
| 110 |
+
frame = pixelate(frame)
|
| 111 |
+
elif option2 == 4:
|
| 112 |
+
frame = apply_emboss_filter(frame)
|
| 113 |
+
elif option2 == 5:
|
| 114 |
+
frame = apply_edge_enhance(frame)
|
| 115 |
+
elif option2 == 'i':
|
| 116 |
+
frame = brightness_control(frame, 30)
|
| 117 |
+
elif option2 == 'd':
|
| 118 |
+
frame = brightness_control(frame, -30)
|
| 119 |
+
return frame
|
| 120 |
+
|
| 121 |
+
def process_image(img, option1, option2):
|
| 122 |
+
frame = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 123 |
+
output_frame = final(option1, option2, frame)
|
| 124 |
+
return cv2.cvtColor(output_frame, cv2.COLOR_BGR2RGB)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
# with gr.Blocks() as demo:
|
| 128 |
+
|
| 129 |
+
iface = gr.Interface(
|
| 130 |
+
fn=process_image,
|
| 131 |
+
inputs=[
|
| 132 |
+
gr.Image(source="webcam", streaming=True),
|
| 133 |
+
gr.Radio(["Person", "Background", "Whole Frame"]),
|
| 134 |
+
gr.Radio(["Blur", "Sepia", "Pixelate", "Emboss", "Edge Enhance", "Increase Brightness", "Decrease Brightness"])
|
| 135 |
+
],
|
| 136 |
+
outputs=gr.Image(),
|
| 137 |
+
live=True
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
opencv-python
|
| 2 |
+
cvzone
|
| 3 |
+
gradio
|