Spaces:
Sleeping
Sleeping
outshine84@gmail.com commited on
Commit ·
8eb9eb9
1
Parent(s): 3350a85
initial retry
Browse files- app.py +114 -0
- requirements.txt +56 -0
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from sklearn.cluster import KMeans
|
| 6 |
+
import uvicorn
|
| 7 |
+
import tempfile
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# -----------------------------
|
| 11 |
+
# FUNZIONI
|
| 12 |
+
# -----------------------------
|
| 13 |
+
|
| 14 |
+
def posterize (image, colors: int):
|
| 15 |
+
data = image.reshape(-1, 3).astype(np.float32)
|
| 16 |
+
|
| 17 |
+
kmeans = KMeans(n_clusters=colors, random_state=42, n_init='auto')
|
| 18 |
+
kmeans.fit(data)
|
| 19 |
+
|
| 20 |
+
labels = kmeans.predict(data)
|
| 21 |
+
|
| 22 |
+
centers = np.uint8(kmeans.cluster_centers_)
|
| 23 |
+
posterized_image = centers[labels]
|
| 24 |
+
posterized_image = posterized_image.reshape(image.shape)
|
| 25 |
+
|
| 26 |
+
return posterized_image
|
| 27 |
+
|
| 28 |
+
def adjust_image (image, saturation: int, brightness: int):
|
| 29 |
+
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
| 30 |
+
h, s, v = cv2.split(hsv_image)
|
| 31 |
+
|
| 32 |
+
s = cv2.add(s, saturation) # Increase saturation by adding a constant
|
| 33 |
+
s = np.clip(s, 0, 255) # Clip values to 0-255
|
| 34 |
+
|
| 35 |
+
v = cv2.add(v, brightness) # Increase brightness by adding a constant
|
| 36 |
+
v = np.clip(v, 0, 255) # Clip values to 0-255
|
| 37 |
+
|
| 38 |
+
adjusted_hsv = cv2.merge([h, s, v])
|
| 39 |
+
adjusted_color_image = cv2.cvtColor(adjusted_hsv, cv2.COLOR_HSV2BGR)
|
| 40 |
+
return adjusted_color_image
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def cartoonize (image, saturation: int, brightness: int, edge_cut: int, paint_effect: int, blur: int , colors: int):
|
| 44 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 45 |
+
gray = cv2.medianBlur(gray, blur)
|
| 46 |
+
adjusted_color_image = adjust_image(image, saturation, brightness)
|
| 47 |
+
posterized_image = posterize(adjusted_color_image, colors)
|
| 48 |
+
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, edge_cut)
|
| 49 |
+
color = cv2.bilateralFilter(posterized_image, paint_effect*2+1, 250, 250)
|
| 50 |
+
cartoon = cv2.bitwise_and(color, color, mask=edges)
|
| 51 |
+
return cartoon
|
| 52 |
+
|
| 53 |
+
# -----------------------------
|
| 54 |
+
# FILTRI
|
| 55 |
+
# -----------------------------
|
| 56 |
+
|
| 57 |
+
def apply_filters(image, blur, edge_cut, saturation, brightness, paint_effect, colors):
|
| 58 |
+
|
| 59 |
+
if image is None:
|
| 60 |
+
return None
|
| 61 |
+
|
| 62 |
+
# Convertiamo da RGB (Gradio) a BGR (OpenCV)
|
| 63 |
+
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 64 |
+
|
| 65 |
+
cartoonized = cartoonize(img,saturation,brightness,edge_cut,paint_effect,blur,colors)
|
| 66 |
+
|
| 67 |
+
cartoonized = cv2.cvtColor(cartoonized, cv2.COLOR_BGR2RGB)
|
| 68 |
+
|
| 69 |
+
return cartoonized
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
with gr.Blocks() as demo:
|
| 73 |
+
gr.Markdown("# Cartoonizer")
|
| 74 |
+
|
| 75 |
+
with gr.Row():
|
| 76 |
+
input_img = gr.Image(label="Carica immagine",type='numpy')
|
| 77 |
+
output_img = gr.Image(label="Risultato")
|
| 78 |
+
|
| 79 |
+
with gr.Accordion("Filtri", open=True):
|
| 80 |
+
blur = gr.Slider(0, 10, value=5, step=1, label="Sfocatura")
|
| 81 |
+
edge_cut = gr.Slider(0, 9, value=4, step=1, label="Edge Cut")
|
| 82 |
+
saturation = gr.Slider(0, 50, value=30, step=5, label="Saturazione")
|
| 83 |
+
brightness = gr.Slider(0, 50, value=15, step=5, label="Luminosità")
|
| 84 |
+
paint_effect = gr.Slider(0, 9, value=9, step=1, label="Modalità Paint")
|
| 85 |
+
colors = gr.Slider(0, 20, value=12, step=1, label="Numero di colori")
|
| 86 |
+
|
| 87 |
+
btn = gr.Button("Applica Filtri")
|
| 88 |
+
save = gr.Button("Salva Immagine")
|
| 89 |
+
|
| 90 |
+
# Aggiornamento in tempo reale
|
| 91 |
+
btn.click(
|
| 92 |
+
apply_filters,
|
| 93 |
+
inputs=[input_img, blur, edge_cut, saturation,brightness,paint_effect, colors],
|
| 94 |
+
outputs=output_img
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Salvataggio file
|
| 98 |
+
|
| 99 |
+
def save_image(image):
|
| 100 |
+
if image is None:
|
| 101 |
+
return None
|
| 102 |
+
|
| 103 |
+
tmp_path = os.path.join(
|
| 104 |
+
tempfile.gettempdir(),
|
| 105 |
+
"cartoonized_image.png" # fixed name or generate unique
|
| 106 |
+
)
|
| 107 |
+
bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 108 |
+
cv2.imwrite(tmp_path, bgr)
|
| 109 |
+
return tmp_path
|
| 110 |
+
|
| 111 |
+
save.click(save_image, inputs=output_img, outputs=gr.File(label="File Salvato"))
|
| 112 |
+
|
| 113 |
+
# Avvio app
|
| 114 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==24.1.0
|
| 2 |
+
annotated-doc==0.0.4
|
| 3 |
+
annotated-types==0.7.0
|
| 4 |
+
anyio==4.12.0
|
| 5 |
+
brotli==1.2.0
|
| 6 |
+
certifi==2025.11.12
|
| 7 |
+
click==8.3.1
|
| 8 |
+
fastapi==0.122.0
|
| 9 |
+
ffmpy==1.0.0
|
| 10 |
+
filelock==3.20.0
|
| 11 |
+
fsspec==2025.10.0
|
| 12 |
+
gradio==6.0.1
|
| 13 |
+
gradio_client==2.0.0
|
| 14 |
+
groovy==0.1.2
|
| 15 |
+
h11==0.16.0
|
| 16 |
+
hf-xet==1.2.0
|
| 17 |
+
httpcore==1.0.9
|
| 18 |
+
httpx==0.28.1
|
| 19 |
+
huggingface_hub==1.1.6
|
| 20 |
+
idna==3.11
|
| 21 |
+
Jinja2==3.1.6
|
| 22 |
+
joblib==1.5.2
|
| 23 |
+
markdown-it-py==4.0.0
|
| 24 |
+
MarkupSafe==3.0.3
|
| 25 |
+
mdurl==0.1.2
|
| 26 |
+
numpy==2.2.6
|
| 27 |
+
opencv-python==4.12.0.88
|
| 28 |
+
orjson==3.11.4
|
| 29 |
+
packaging==25.0
|
| 30 |
+
pandas==2.3.3
|
| 31 |
+
pillow==12.0.0
|
| 32 |
+
pydantic==2.12.4
|
| 33 |
+
pydantic_core==2.41.5
|
| 34 |
+
pydub==0.25.1
|
| 35 |
+
Pygments==2.19.2
|
| 36 |
+
python-dateutil==2.9.0.post0
|
| 37 |
+
python-multipart==0.0.20
|
| 38 |
+
pytz==2025.2
|
| 39 |
+
PyYAML==6.0.3
|
| 40 |
+
rich==14.2.0
|
| 41 |
+
safehttpx==0.1.7
|
| 42 |
+
scikit-learn==1.7.2
|
| 43 |
+
scipy==1.16.3
|
| 44 |
+
semantic-version==2.10.0
|
| 45 |
+
shellingham==1.5.4
|
| 46 |
+
six==1.17.0
|
| 47 |
+
starlette==0.50.0
|
| 48 |
+
threadpoolctl==3.6.0
|
| 49 |
+
tomlkit==0.13.3
|
| 50 |
+
tqdm==4.67.1
|
| 51 |
+
typer==0.20.0
|
| 52 |
+
typer-slim==0.20.0
|
| 53 |
+
typing-inspection==0.4.2
|
| 54 |
+
typing_extensions==4.15.0
|
| 55 |
+
tzdata==2025.2
|
| 56 |
+
uvicorn==0.38.0
|