| 268 | |
| 269 | |
| 270 | def build_canvas(input_image_path, resized_height, resized_width, top_left_height, top_left_width, bottom_right_height, bottom_right_width): |
| 271 | |
| 272 | # Init |
| 273 | canvas_color = (250, 249, 246) # This color is like white color used in painting paper |
| 274 | |
| 275 | |
| 276 | # Convert the string to integer |
| 277 | if not resized_height.isdigit(): |
| 278 | raise gr.Error("resized_height must be integer input!") |
| 279 | resized_height = int(resized_height) |
| 280 | |
| 281 | if not resized_width.isdigit(): |
| 282 | raise gr.Error("resized_width must be integer input!") |
| 283 | resized_width = int(resized_width) |
| 284 | |
| 285 | if not top_left_height.isdigit(): |
| 286 | raise gr.Error("top_left_height must be integer input!") |
| 287 | top_left_height = int(top_left_height) |
| 288 | |
| 289 | if not top_left_width.isdigit(): |
| 290 | raise gr.Error("top_left_width must be integer input!") |
| 291 | top_left_width = int(top_left_width) |
| 292 | |
| 293 | if not bottom_right_height.isdigit(): |
| 294 | raise gr.Error("bottom_right_height must be integer input!") |
| 295 | bottom_right_height = int(bottom_right_height) |
| 296 | |
| 297 | if not bottom_right_width.isdigit(): |
| 298 | raise gr.Error("bottom_right_width must be integer input!") |
| 299 | bottom_right_width = int(bottom_right_width) |
| 300 | |
| 301 | |
| 302 | |
| 303 | # Read the original image and preprare the placeholder |
| 304 | first_frame_img = np.uint8(np.asarray(Image.open(input_image_path))) # NOTE: this is BGR form, be careful for the later cropping process for ID Reference |
| 305 | print("first_frame_img shape is ", first_frame_img.shape) |
| 306 | |
| 307 | |
| 308 | # Resize to a uniform resolution |
| 309 | first_frame_img = cv2.resize(first_frame_img, (resized_width, resized_height), interpolation = cv2.INTER_AREA) |
| 310 | print("first_frame_img is resized to", first_frame_img.shape) |
| 311 | |
| 312 | |
| 313 | # Expand to Outside Region to form the Canvas |
| 314 | expand_height = resized_height + top_left_height + bottom_right_height |
| 315 | expand_width = resized_width + top_left_width + bottom_right_width |
| 316 | inference_canvas = np.uint8(np.zeros((expand_height, expand_width, 3))) # Whole Black Canvas, same as other inference |
| 317 | visual_canvas = np.full((expand_height, expand_width, 3), canvas_color, dtype=np.uint8) |
| 318 | print("Init Visual Canvas shape is", visual_canvas.shape) |
| 319 | print("Init Inference Canvs shape is", inference_canvas.shape) |
| 320 | |
| 321 | |
| 322 | # Sanity Check |
| 323 | if expand_height % 32 != 0: |
| 324 | raise gr.Error("The Height of resized_height + top_left_height + bottom_right_height must be divisible by 32!") |
| 325 | if expand_width % 32 != 0: |
| 326 | raise gr.Error("The Width of resized_width + top_left_width + bottom_right_width must be divisible by 32!") |
| 327 | |