Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,68 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
3 |
import numpy as np
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
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 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
return (crop_start[0], crop_start[1], crop_end[0], crop_end[1])
|
52 |
|
53 |
-
|
54 |
-
fn=
|
55 |
inputs=[
|
56 |
-
gr.Image(type="
|
57 |
-
gr.
|
58 |
-
gr.
|
59 |
-
|
60 |
-
|
61 |
-
gr.
|
62 |
-
gr.
|
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 |
-
|
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 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from torchvision import models, transforms
|
4 |
+
from PIL import Image, ImageEnhance
|
5 |
import numpy as np
|
6 |
+
import cv2
|
7 |
|
8 |
+
model = models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
|
9 |
+
model.eval()
|
10 |
+
|
11 |
+
transform = transforms.Compose([
|
12 |
+
transforms.ToTensor()
|
13 |
+
])
|
14 |
+
|
15 |
+
def detect_dress(image):
|
16 |
+
image_tensor = transform(image).unsqueeze(0)
|
17 |
+
with torch.no_grad():
|
18 |
+
outputs = model(image_tensor)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
boxes = outputs[0]['boxes'].numpy()
|
21 |
+
scores = outputs[0]['scores'].numpy()
|
22 |
+
threshold = 0.8
|
23 |
+
dress_boxes = [box for box, score in zip(boxes, scores) if score > threshold]
|
24 |
+
|
25 |
+
draw = ImageDraw.Draw(image)
|
26 |
+
for box in dress_boxes:
|
27 |
+
draw.rectangle(box.tolist(), outline="red", width=3)
|
28 |
|
29 |
+
return image, dress_boxes
|
30 |
+
|
31 |
+
def crop_image(image, box):
|
32 |
+
return image.crop(box)
|
33 |
+
|
34 |
+
def adjust_color(image, factor):
|
35 |
+
enhancer = ImageEnhance.Color(image)
|
36 |
+
return enhancer.enhance(factor)
|
37 |
+
|
38 |
+
def process_image(image, edit_type, factor):
|
39 |
+
detected_image, boxes = detect_dress(image)
|
40 |
|
41 |
+
if not boxes:
|
42 |
+
return detected_image, "No dresses detected."
|
43 |
+
|
44 |
+
if edit_type == "Crop":
|
45 |
+
box = boxes[0]
|
46 |
+
edited_image = crop_image(image, box)
|
47 |
+
elif edit_type == "Adjust Color":
|
48 |
+
edited_image = adjust_color(image, factor)
|
49 |
+
else:
|
50 |
+
edited_image = image
|
51 |
|
52 |
+
return edited_image, "Edit applied."
|
|
|
53 |
|
54 |
+
iface = gr.Interface(
|
55 |
+
fn=process_image,
|
56 |
inputs=[
|
57 |
+
gr.inputs.Image(type="pil"),
|
58 |
+
gr.inputs.Radio(choices=["None", "Crop", "Adjust Color"]),
|
59 |
+
gr.inputs.Slider(0.5, 2.0, step=0.1, label="Factor")
|
60 |
+
],
|
61 |
+
outputs=[
|
62 |
+
gr.outputs.Image(type="pil"),
|
63 |
+
gr.outputs.Textbox(label="Result")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
],
|
65 |
+
live=True
|
|
|
|
|
|
|
66 |
)
|
67 |
|
68 |
+
iface.launch()
|