nikunjkdtechnoland
commited on
Commit
•
d06defe
1
Parent(s):
ea71739
code error added
Browse files- app.py +1 -1
- only_gradio_server.py +19 -39
app.py
CHANGED
@@ -12,7 +12,7 @@ iface = gr.Interface(fn=process_images,
|
|
12 |
inputs=[gr.Image(type='filepath', label='Main Image where object identify', width="60%", height="60%"),
|
13 |
gr.Image(type='filepath', label='Object Image which placed on Main Image (PNG file only RGBA Channel)', image_mode="RGBA", width="60%", height="60%"),
|
14 |
gr.Dropdown(options_list, label='Replace Object Name (Default = chair)')],
|
15 |
-
outputs=gr.Image(type='numpy', label='Final Result', width="
|
16 |
title="AI Based Image Processing",
|
17 |
description="Object to Object Replacement (Note: due to limitation of free usage on this server task will take approx 3-5 minutes for process, But the actual speed of this process on pc or dedicated server is < 10 seconds)")
|
18 |
|
|
|
12 |
inputs=[gr.Image(type='filepath', label='Main Image where object identify', width="60%", height="60%"),
|
13 |
gr.Image(type='filepath', label='Object Image which placed on Main Image (PNG file only RGBA Channel)', image_mode="RGBA", width="60%", height="60%"),
|
14 |
gr.Dropdown(options_list, label='Replace Object Name (Default = chair)')],
|
15 |
+
outputs=gr.Image(type='numpy', label='Final Result', width="60%", height="60%"),
|
16 |
title="AI Based Image Processing",
|
17 |
description="Object to Object Replacement (Note: due to limitation of free usage on this server task will take approx 3-5 minutes for process, But the actual speed of this process on pc or dedicated server is < 10 seconds)")
|
18 |
|
only_gradio_server.py
CHANGED
@@ -13,6 +13,7 @@ from utils.tools import get_config
|
|
13 |
import torch.nn.functional as F
|
14 |
from iopaint.single_processing import batch_inpaint_cv2
|
15 |
from pathlib import Path
|
|
|
16 |
|
17 |
# set current working directory cache instead of default
|
18 |
os.environ["TORCH_HOME"] = "./pretrained-model"
|
@@ -49,43 +50,24 @@ def resize_image(input_image_path, width=640, height=640):
|
|
49 |
return im
|
50 |
|
51 |
except Exception as e:
|
52 |
-
|
53 |
-
return None # Or handle differently as needed
|
54 |
|
55 |
|
56 |
-
def
|
57 |
-
|
58 |
-
|
59 |
-
k: v.to(device)
|
60 |
-
for k, v in model_weights.items()
|
61 |
-
}
|
62 |
-
|
63 |
-
|
64 |
-
# Function to convert image to base64
|
65 |
-
def convert_image_to_base64(image):
|
66 |
-
# Convert image to bytes
|
67 |
-
_, buffer = cv2.imencode('.png', image)
|
68 |
-
# Convert bytes to base64
|
69 |
-
image_base64 = base64.b64encode(buffer).decode('utf-8')
|
70 |
-
return image_base64
|
71 |
-
|
72 |
-
|
73 |
-
def convert_to_base64(image):
|
74 |
-
# Read the image file as binary data
|
75 |
-
image_data = image.read()
|
76 |
-
# Encode the binary data as base64
|
77 |
-
base64_encoded = base64.b64encode(image_data).decode('utf-8')
|
78 |
-
return base64_encoded
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
image_data = cv2.imencode('.png', image)[1].tobytes()
|
83 |
-
# Encode the binary data as base64
|
84 |
-
base64_encoded = base64.b64encode(image_data).decode('utf-8')
|
85 |
-
return base64_encoded
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
-
def process_images(input_image, append_image, default_class="chair"):
|
89 |
# Static paths
|
90 |
config_path = Path('configs/config.yaml')
|
91 |
model_path = Path('pretrained-model/torch_model.p')
|
@@ -94,7 +76,7 @@ def process_images(input_image, append_image, default_class="chair"):
|
|
94 |
img = resize_image(input_image)
|
95 |
|
96 |
if img is None:
|
97 |
-
|
98 |
|
99 |
H, W, _ = img.shape
|
100 |
x_point = 0
|
@@ -139,13 +121,13 @@ def process_images(input_image, append_image, default_class="chair"):
|
|
139 |
resized_mask = cv2.resize(dilated_mask, (img.shape[1], img.shape[0]))
|
140 |
|
141 |
# call repainting and merge function
|
142 |
-
|
143 |
-
# Return the output
|
144 |
-
return
|
145 |
|
146 |
# return class not found in prediction
|
147 |
if not class_found:
|
148 |
-
|
149 |
|
150 |
def repaitingAndMerge(append_image_path, model_path, config_path, width, height, xposition, yposition, input_base, mask_base):
|
151 |
config = get_config(config_path)
|
@@ -160,10 +142,8 @@ def repaitingAndMerge(append_image_path, model_path, config_path, width, height,
|
|
160 |
final_image = Image.fromarray(inpaint_result_np)
|
161 |
|
162 |
print("merge start")
|
163 |
-
|
164 |
# Load the append image using cv2.imread
|
165 |
append_image = cv2.imread(append_image_path, cv2.IMREAD_UNCHANGED)
|
166 |
-
cv2.imwrite('appneded-image.png',append_image)
|
167 |
# Resize the append image while preserving transparency
|
168 |
resized_image = cv2.resize(append_image, (width, height), interpolation=cv2.INTER_AREA)
|
169 |
# Convert the resized image to RGBA format (assuming it's in BGRA format)
|
|
|
13 |
import torch.nn.functional as F
|
14 |
from iopaint.single_processing import batch_inpaint_cv2
|
15 |
from pathlib import Path
|
16 |
+
import gradio as gr
|
17 |
|
18 |
# set current working directory cache instead of default
|
19 |
os.environ["TORCH_HOME"] = "./pretrained-model"
|
|
|
50 |
return im
|
51 |
|
52 |
except Exception as e:
|
53 |
+
raise gr.Error("Error in resizing image!")
|
|
|
54 |
|
55 |
|
56 |
+
def process_images(input_image, append_image, default_class="chair"):
|
57 |
+
if not input_image:
|
58 |
+
raise gr.Error("Please upload a main image.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
if not append_image:
|
61 |
+
raise gr.Error("Please upload an object image.")
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
# Check if the append_image is a PNG file with RGBA mode
|
64 |
+
try:
|
65 |
+
with Image.open(append_image) as img:
|
66 |
+
if img.format != 'PNG' or img.mode != 'RGBA':
|
67 |
+
raise gr.Error("Please upload a valid PNG file with RGBA mode for the object image.")
|
68 |
+
except Exception as e:
|
69 |
+
raise gr.Error("Failed to validate object image: Upload new image")
|
70 |
|
|
|
71 |
# Static paths
|
72 |
config_path = Path('configs/config.yaml')
|
73 |
model_path = Path('pretrained-model/torch_model.p')
|
|
|
76 |
img = resize_image(input_image)
|
77 |
|
78 |
if img is None:
|
79 |
+
raise gr.Error("Failed to decode resized image!")
|
80 |
|
81 |
H, W, _ = img.shape
|
82 |
x_point = 0
|
|
|
121 |
resized_mask = cv2.resize(dilated_mask, (img.shape[1], img.shape[0]))
|
122 |
|
123 |
# call repainting and merge function
|
124 |
+
output_numpy = repaitingAndMerge(append_image,str(model_path), str(config_path),width, height, x_point, y_point, img, resized_mask)
|
125 |
+
# Return the output numpy image in the API response
|
126 |
+
return output_numpy
|
127 |
|
128 |
# return class not found in prediction
|
129 |
if not class_found:
|
130 |
+
raise gr.Error(f'{default_class} object not found in the image')
|
131 |
|
132 |
def repaitingAndMerge(append_image_path, model_path, config_path, width, height, xposition, yposition, input_base, mask_base):
|
133 |
config = get_config(config_path)
|
|
|
142 |
final_image = Image.fromarray(inpaint_result_np)
|
143 |
|
144 |
print("merge start")
|
|
|
145 |
# Load the append image using cv2.imread
|
146 |
append_image = cv2.imread(append_image_path, cv2.IMREAD_UNCHANGED)
|
|
|
147 |
# Resize the append image while preserving transparency
|
148 |
resized_image = cv2.resize(append_image, (width, height), interpolation=cv2.INTER_AREA)
|
149 |
# Convert the resized image to RGBA format (assuming it's in BGRA format)
|