(input_image, save_path, lower_contrast=True, rescale=True)
| 37 | |
| 38 | # contrast correction, rescale and recenter |
| 39 | def image_preprocess(input_image, save_path, lower_contrast=True, rescale=True): |
| 40 | image_arr = np.array(input_image) |
| 41 | in_w, in_h = image_arr.shape[:2] |
| 42 | |
| 43 | if lower_contrast: |
| 44 | alpha = 0.8 # Contrast control (1.0-3.0) |
| 45 | beta = 0 # Brightness control (0-100) |
| 46 | # Apply the contrast adjustment |
| 47 | image_arr = cv2.convertScaleAbs(image_arr, alpha=alpha, beta=beta) |
| 48 | image_arr[image_arr[..., -1] > 200, -1] = 255 |
| 49 | |
| 50 | ret, mask = cv2.threshold( |
| 51 | np.array(input_image.split()[-1]), 0, 255, cv2.THRESH_BINARY |
| 52 | ) |
| 53 | x, y, w, h = cv2.boundingRect(mask) |
| 54 | max_size = max(w, h) |
| 55 | ratio = 0.75 |
| 56 | if rescale: |
| 57 | side_len = int(max_size / ratio) |
| 58 | else: |
| 59 | side_len = in_w |
| 60 | padded_image = np.zeros((side_len, side_len, 4), dtype=np.uint8) |
| 61 | center = side_len // 2 |
| 62 | padded_image[ |
| 63 | center - h // 2 : center - h // 2 + h, center - w // 2 : center - w // 2 + w |
| 64 | ] = image_arr[y : y + h, x : x + w] |
| 65 | rgba = Image.fromarray(padded_image).resize((256, 256), Image.LANCZOS) |
| 66 | rgba.save(save_path) |
| 67 | |
| 68 | def pred_bbox(image): |
| 69 | image_nobg = remove(image.convert("RGBA"), alpha_matting=True) |
no outgoing calls
no test coverage detected