NaimaAqeel commited on
Commit
b13bb13
·
verified ·
1 Parent(s): aaa7f7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -32
app.py CHANGED
@@ -1,20 +1,9 @@
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)
@@ -24,33 +13,27 @@ def truncated_svd_compress(image, k):
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))
54
 
55
  return compressed_image_pil
56
 
@@ -62,9 +45,9 @@ gr_interface = gr.Interface(
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()
 
1
  import numpy as np
2
  import gradio as gr
 
3
  from skimage import color
4
  from sklearn.decomposition import TruncatedSVD
 
5
  from PIL import Image
6
 
 
 
 
 
 
 
 
 
 
7
  def truncated_svd_compress(image, k):
8
  """Compress the image using Truncated SVD by keeping only the top k singular values."""
9
  svd = TruncatedSVD(n_components=k)
 
13
  compressed_image = np.dot(U, np.dot(np.diag(S), Vt))
14
  return compressed_image
15
 
 
 
 
 
 
 
16
  def process_image(image, k):
17
+ """Process the uploaded image, compress it using Truncated SVD, and return the result."""
18
  # Convert PIL Image to NumPy array
19
  image_np = np.array(image)
20
 
21
+ # Convert to grayscale for SVD
 
 
 
 
 
 
 
 
22
  gray_image = color.rgb2gray(image_np)
23
 
24
+ # Ensure the image is 2D for SVD
25
+ if len(gray_image.shape) == 3:
26
+ gray_image = gray_image[:, :, 0]
27
+
28
+ # Compress the image using Truncated SVD
29
  compressed_image = truncated_svd_compress(gray_image, k)
30
 
31
+ # Normalize the compressed image to the range [0, 255] and convert to uint8
32
+ compressed_image = np.clip(compressed_image, 0, 1) # Ensure values are within [0, 1]
33
+ compressed_image = (compressed_image * 255).astype(np.uint8)
34
+
35
  # Convert compressed image back to PIL Image for Gradio output
36
+ compressed_image_pil = Image.fromarray(compressed_image)
37
 
38
  return compressed_image_pil
39
 
 
45
  gr.Slider(1, 100, step=1, value=50, label="Compression Rank") # Compression rank slider
46
  ],
47
  outputs=gr.Image(type="pil", label="Compressed Image"),
48
+ title="Interactive Image Compression using Truncated SVD",
49
+ description="Upload an image and adjust the compression rank to see the compressed version. The app compresses the image while retaining important features."
50
  )
51
 
52
  # Launch the Gradio interface
53
+ gr_interface.launch()