gaur3009 commited on
Commit
6246ec7
1 Parent(s): 8bfb11f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -66
app.py CHANGED
@@ -1,78 +1,68 @@
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()
 
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()