Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,18 +17,27 @@ def cartoonize_image(img, median_blur_value, block_size, c_value, d_value, sigma
|
|
| 17 |
blockSize=block_size,
|
| 18 |
C=c_value)
|
| 19 |
|
| 20 |
-
# Bilateral filter for color smoothing
|
| 21 |
color = cv2.bilateralFilter(img, d=d_value, sigmaColor=sigma_color, sigmaSpace=sigma_space)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Create a sketch effect by blending edges with the smoothed color image
|
| 24 |
sketch = cv2.bitwise_and(color, color, mask=edges)
|
| 25 |
|
| 26 |
# Ensure both images are of the same size before blending
|
| 27 |
-
if
|
| 28 |
-
sketch = cv2.resize(sketch, (
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
cartoon = cv2.addWeighted(
|
| 32 |
|
| 33 |
return cartoon
|
| 34 |
|
|
@@ -51,8 +60,8 @@ if uploaded_file is not None:
|
|
| 51 |
block_size = st.slider("Block Size", min_value=3, max_value=21, value=9, step=2)
|
| 52 |
c_value = st.slider("C Value", min_value=0, max_value=20, value=5) # Increased default value for more effect
|
| 53 |
d_value = st.slider("Diameter (d)", min_value=1, max_value=15, value=9)
|
| 54 |
-
sigma_color = st.slider("Sigma Color", min_value=0, max_value=400, value=
|
| 55 |
-
sigma_space = st.slider("Sigma Space", min_value=0, max_value=400, value=
|
| 56 |
|
| 57 |
# Show a spinner while processing the cartoonization
|
| 58 |
with st.spinner('Processing...'):
|
|
|
|
| 17 |
blockSize=block_size,
|
| 18 |
C=c_value)
|
| 19 |
|
| 20 |
+
# Bilateral filter for color smoothing (reduce colors)
|
| 21 |
color = cv2.bilateralFilter(img, d=d_value, sigmaColor=sigma_color, sigmaSpace=sigma_space)
|
| 22 |
|
| 23 |
+
# Convert color image to grayscale and then back to BGR
|
| 24 |
+
color_gray = cv2.cvtColor(color, cv2.COLOR_BGR2GRAY)
|
| 25 |
+
|
| 26 |
+
# Reduce colors by applying a threshold on the grayscale image
|
| 27 |
+
_, color_thresh = cv2.threshold(color_gray, 200, 255, cv2.THRESH_BINARY_INV)
|
| 28 |
+
|
| 29 |
# Create a sketch effect by blending edges with the smoothed color image
|
| 30 |
sketch = cv2.bitwise_and(color, color, mask=edges)
|
| 31 |
|
| 32 |
# Ensure both images are of the same size before blending
|
| 33 |
+
if sketch.shape[:2] != img.shape[:2]:
|
| 34 |
+
sketch = cv2.resize(sketch, (img.shape[1], img.shape[0]))
|
| 35 |
+
|
| 36 |
+
# Combine the sketch with a desaturated version of the original image
|
| 37 |
+
desaturated_color = cv2.addWeighted(color, 0.3, np.zeros_like(color), 0.7, 0) # Reduce saturation
|
| 38 |
|
| 39 |
+
# Blend sketch and desaturated color image
|
| 40 |
+
cartoon = cv2.addWeighted(desaturated_color, 0.5, sketch, 0.5, 0)
|
| 41 |
|
| 42 |
return cartoon
|
| 43 |
|
|
|
|
| 60 |
block_size = st.slider("Block Size", min_value=3, max_value=21, value=9, step=2)
|
| 61 |
c_value = st.slider("C Value", min_value=0, max_value=20, value=5) # Increased default value for more effect
|
| 62 |
d_value = st.slider("Diameter (d)", min_value=1, max_value=15, value=9)
|
| 63 |
+
sigma_color = st.slider("Sigma Color", min_value=0, max_value=400, value=100) # Reduced for more sketch effect
|
| 64 |
+
sigma_space = st.slider("Sigma Space", min_value=0, max_value=400, value=100) # Reduced for more sketch effect
|
| 65 |
|
| 66 |
# Show a spinner while processing the cartoonization
|
| 67 |
with st.spinner('Processing...'):
|