Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,78 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from torchvision import models, transforms
|
4 |
-
from PIL import Image, ImageEnhance, ImageDraw
|
5 |
import numpy as np
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
model.eval()
|
10 |
-
|
11 |
-
# Image transformation
|
12 |
-
transform = transforms.Compose([
|
13 |
-
transforms.ToTensor()
|
14 |
-
])
|
15 |
-
|
16 |
-
# Detection function
|
17 |
-
def detect_dress(image):
|
18 |
-
image_tensor = transform(image).unsqueeze(0)
|
19 |
-
with torch.no_grad():
|
20 |
-
outputs = model(image_tensor)
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
draw = ImageDraw.Draw(image)
|
31 |
-
for box in dress_boxes:
|
32 |
-
draw.rectangle(box.tolist(), outline="red", width=3)
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
def crop_image(image, box):
|
38 |
-
return image.crop(box)
|
39 |
-
|
40 |
-
def adjust_color(image, factor):
|
41 |
-
enhancer = ImageEnhance.Color(image)
|
42 |
-
return enhancer.enhance(factor)
|
43 |
-
|
44 |
-
# Gradio interface function
|
45 |
-
def process_image(image, edit_type, factor):
|
46 |
-
detected_image, boxes = detect_dress(image)
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
|
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
fn=process_image,
|
64 |
inputs=[
|
65 |
-
gr.Image(type="
|
66 |
-
gr.
|
67 |
-
gr.
|
68 |
-
|
69 |
-
|
70 |
-
gr.
|
71 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
],
|
73 |
-
|
|
|
|
|
|
|
74 |
)
|
75 |
|
76 |
-
|
77 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image, ImageOps, ImageEnhance, ImageFilter, ImageDraw
|
|
|
|
|
3 |
import numpy as np
|
4 |
|
5 |
+
def edit_image(image, grayscale, flip, rotate, brightness, contrast, color, crop, resize, blur, sharpness, draw_text, text_position, text_color, text_size):
|
6 |
+
img = Image.open(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
if grayscale:
|
9 |
+
img = ImageOps.grayscale(img)
|
10 |
+
if flip:
|
11 |
+
img = ImageOps.flip(img)
|
12 |
+
if rotate:
|
13 |
+
img = img.rotate(rotate)
|
14 |
|
15 |
+
# Apply brightness
|
16 |
+
enhancer = ImageEnhance.Brightness(img)
|
17 |
+
img = enhancer.enhance(brightness)
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Apply contrast
|
20 |
+
enhancer = ImageEnhance.Contrast(img)
|
21 |
+
img = enhancer.enhance(contrast)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
# Apply color
|
24 |
+
enhancer = ImageEnhance.Color(img)
|
25 |
+
img = enhancer.enhance(color)
|
26 |
+
|
27 |
+
# Apply crop
|
28 |
+
if crop:
|
29 |
+
img = img.crop(crop)
|
30 |
+
|
31 |
+
# Apply resize
|
32 |
+
if resize:
|
33 |
+
img = img.resize(resize)
|
34 |
+
|
35 |
+
# Apply blur
|
36 |
+
if blur > 0:
|
37 |
+
img = img.filter(ImageFilter.GaussianBlur(blur))
|
38 |
+
|
39 |
+
# Apply sharpness
|
40 |
+
enhancer = ImageEnhance.Sharpness(img)
|
41 |
+
img = enhancer.enhance(sharpness)
|
42 |
+
|
43 |
+
# Draw text
|
44 |
+
if draw_text:
|
45 |
+
draw = ImageDraw.Draw(img)
|
46 |
+
draw.text(text_position, draw_text, fill=text_color, font=None, anchor=None, spacing=4, align="left")
|
47 |
+
|
48 |
+
return img
|
49 |
|
50 |
+
def get_crop_coordinates(crop_start, crop_end):
|
51 |
+
return (crop_start[0], crop_start[1], crop_end[0], crop_end[1])
|
52 |
|
53 |
+
interface = gr.Interface(
|
54 |
+
fn=edit_image,
|
|
|
55 |
inputs=[
|
56 |
+
gr.Image(type="filepath", label="Upload Image"),
|
57 |
+
gr.Checkbox(label="Grayscale"),
|
58 |
+
gr.Checkbox(label="Flip Vertically"),
|
59 |
+
gr.Slider(minimum=0, maximum=360, step=1, value=0, label="Rotate Angle"),
|
60 |
+
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Brightness"),
|
61 |
+
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Contrast"),
|
62 |
+
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Color"),
|
63 |
+
gr.Textbox(label="Crop (left, upper, right, lower)", placeholder="e.g., 100,100,400,400"),
|
64 |
+
gr.Textbox(label="Resize (width, height)", placeholder="e.g., 800,600"),
|
65 |
+
gr.Slider(minimum=0, maximum=10, step=0.1, value=0, label="Blur"),
|
66 |
+
gr.Slider(minimum=0.1, maximum=2, step=0.1, value=1, label="Sharpness"),
|
67 |
+
gr.Textbox(label="Draw Text", placeholder="e.g., Hello World"),
|
68 |
+
gr.Textbox(label="Text Position (x, y)", placeholder="e.g., 100,100"),
|
69 |
+
gr.ColorPicker(label="Text Color"),
|
70 |
+
gr.Slider(minimum=10, maximum=100, step=1, value=30, label="Text Size")
|
71 |
],
|
72 |
+
outputs=gr.Image(),
|
73 |
+
live=True,
|
74 |
+
title="Advanced Image Editor",
|
75 |
+
description="Upload an image and apply various transformations including brightness, contrast, color adjustments, cropping, resizing, blurring, and adding text."
|
76 |
)
|
77 |
|
78 |
+
interface.launch()
|
|