Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,53 @@
|
|
1 |
import numpy as np
|
2 |
import gradio as gr
|
3 |
-
|
4 |
-
from
|
|
|
|
|
5 |
from PIL import Image
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
return compressed_image
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def process_image(image, k):
|
14 |
-
"""Process the uploaded image, compress it using SVD
|
15 |
# Convert PIL Image to NumPy array
|
16 |
image_np = np.array(image)
|
17 |
|
18 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
gray_image = color.rgb2gray(image_np)
|
20 |
|
21 |
# Compress the image
|
22 |
-
compressed_image =
|
23 |
|
24 |
# Convert compressed image back to PIL Image for Gradio output
|
25 |
compressed_image_pil = Image.fromarray((compressed_image * 255).astype(np.uint8))
|
@@ -27,10 +55,16 @@ def process_image(image, k):
|
|
27 |
return compressed_image_pil
|
28 |
|
29 |
# Gradio interface
|
30 |
-
gr_interface = gr.Interface(
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
35 |
|
|
|
36 |
gr_interface.launch()
|
|
|
1 |
import numpy as np
|
2 |
import gradio as gr
|
3 |
+
import cv2 # OpenCV for face detection
|
4 |
+
from skimage import color
|
5 |
+
from sklearn.decomposition import TruncatedSVD
|
6 |
+
from concurrent.futures import ThreadPoolExecutor
|
7 |
from PIL import Image
|
8 |
|
9 |
+
# Detect faces using OpenCV
|
10 |
+
def detect_faces(image_np):
|
11 |
+
"""Detect faces in the image and return their bounding boxes."""
|
12 |
+
gray_image = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
|
13 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
14 |
+
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)
|
15 |
+
return faces
|
16 |
+
|
17 |
+
# Compress the image using Truncated SVD for faster performance
|
18 |
+
def truncated_svd_compress(image, k):
|
19 |
+
"""Compress the image using Truncated SVD by keeping only the top k singular values."""
|
20 |
+
svd = TruncatedSVD(n_components=k)
|
21 |
+
U = svd.fit_transform(image)
|
22 |
+
S = svd.singular_values_
|
23 |
+
Vt = svd.components_
|
24 |
+
compressed_image = np.dot(U, np.dot(np.diag(S), Vt))
|
25 |
return compressed_image
|
26 |
|
27 |
+
# Process the image asynchronously for smoother user experience
|
28 |
+
async def process_image_async(image, k):
|
29 |
+
"""Asynchronous image processing to avoid blocking."""
|
30 |
+
return await asyncio.to_thread(process_image, image, k)
|
31 |
+
|
32 |
+
# Main image processing function
|
33 |
def process_image(image, k):
|
34 |
+
"""Process the uploaded image, detect faces, and compress it using Truncated SVD."""
|
35 |
# Convert PIL Image to NumPy array
|
36 |
image_np = np.array(image)
|
37 |
|
38 |
+
# Detect faces in the image
|
39 |
+
faces = detect_faces(image_np)
|
40 |
+
|
41 |
+
# If faces are detected, highlight the regions (optional)
|
42 |
+
if len(faces) > 0:
|
43 |
+
for (x, y, w, h) in faces:
|
44 |
+
cv2.rectangle(image_np, (x, y), (x+w, y+h), (255, 0, 0), 2)
|
45 |
+
|
46 |
+
# Convert the image to grayscale
|
47 |
gray_image = color.rgb2gray(image_np)
|
48 |
|
49 |
# Compress the image
|
50 |
+
compressed_image = truncated_svd_compress(gray_image, k)
|
51 |
|
52 |
# Convert compressed image back to PIL Image for Gradio output
|
53 |
compressed_image_pil = Image.fromarray((compressed_image * 255).astype(np.uint8))
|
|
|
55 |
return compressed_image_pil
|
56 |
|
57 |
# Gradio interface
|
58 |
+
gr_interface = gr.Interface(
|
59 |
+
fn=process_image, # Image processing function
|
60 |
+
inputs=[
|
61 |
+
gr.Image(type="pil", label="Upload Image"),
|
62 |
+
gr.Slider(1, 100, step=1, value=50, label="Compression Rank") # Compression rank slider
|
63 |
+
],
|
64 |
+
outputs=gr.Image(type="pil", label="Compressed Image"),
|
65 |
+
title="Fast Interactive Image Compression with Face Detection",
|
66 |
+
description="Upload an image and adjust the compression rank. The app detects faces and compresses the image using Truncated SVD for faster processing."
|
67 |
+
)
|
68 |
|
69 |
+
# Launch the Gradio interface
|
70 |
gr_interface.launch()
|