AtthalaricNero commited on
Commit
5e8081b
·
1 Parent(s): a57a8e5

Implement image processing function to remove background and resize images in preprocessing pipeline

Browse files
Files changed (1) hide show
  1. app.py +45 -6
app.py CHANGED
@@ -6,6 +6,7 @@ import io
6
  from flask import Flask, request, render_template
7
  from PIL import Image
8
  from skimage.feature import local_binary_pattern
 
9
 
10
  app = Flask(__name__)
11
 
@@ -41,6 +42,48 @@ CLASS_NAMES = [
41
  "Salak",
42
  ]
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  def extract_color_histogram(img, bins=(8, 8, 8)):
46
  hist = cv2.calcHist([img], [0, 1, 2], None, bins, [0, 256, 0, 256, 0, 256])
@@ -58,13 +101,9 @@ def extract_lbp_features(gray_img, P=8, R=1, method="uniform"):
58
 
59
 
60
  def preprocessing_pipeline(pil_img):
61
- img = np.array(pil_img)
62
-
63
- # ubah format dari RGBA menjadi RGB
64
- if img.shape[-1] == 4:
65
- img = img[:, :, :3]
66
 
67
- img_float = img.astype(np.float32) / 255.0
68
  img_uint8 = (img_float * 255).astype(np.uint8)
69
 
70
  feat_color = extract_color_histogram(img_uint8)
 
6
  from flask import Flask, request, render_template
7
  from PIL import Image
8
  from skimage.feature import local_binary_pattern
9
+ from rembg import remove
10
 
11
  app = Flask(__name__)
12
 
 
42
  "Salak",
43
  ]
44
 
45
+ def process_image(pil_img, target_size=(100, 100)):
46
+ img_io = io.BytesIO()
47
+ pil_img.save(img_io, format='PNG')
48
+ img_bytes = img_io.getvalue()
49
+
50
+ output_array = remove(img_bytes)
51
+ nparr = np.frombuffer(output_array, np.uint8)
52
+ img_rgba = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)
53
+
54
+ alpha = img_rgba[:, :, 3]
55
+ coords = cv2.findNonZero(alpha)
56
+
57
+ if coords is None:
58
+ return np.array(pil_img.resize(target_size).convert("RGB"))
59
+
60
+ x, y, w, h = cv2.boundingRect(coords)
61
+ cropped_img = img_rgba[y:y+h, x:x+w]
62
+
63
+ old_h, old_w = cropped_img.shape[:2]
64
+ desired_w, desired_h = target_size
65
+
66
+ ratio = min(desired_w / old_w, desired_h / old_h)
67
+ new_w = int(old_w * ratio)
68
+ new_h = int(old_h * ratio)
69
+
70
+ resized_img = cv2.resize(cropped_img, (new_w, new_h), interpolation=cv2.INTER_AREA)
71
+
72
+ final_bg = np.zeros((desired_h, desired_w, 4), dtype=np.uint8)
73
+
74
+ x_offset = (desired_w - new_w) // 2
75
+ y_offset = (desired_h - new_h) // 2
76
+ final_bg[y_offset:y_offset+new_h, x_offset:x_offset+new_w] = resized_img
77
+
78
+ white_bg = np.ones((desired_h, desired_w, 3), dtype=np.uint8) * 255
79
+
80
+ alpha_channel = final_bg[:, :, 3] / 255.0
81
+
82
+ for c in range(3):
83
+ white_bg[:, :, c] = (final_bg[:, :, c] * alpha_channel + white_bg[:, :, c] * (1.0 - alpha_channel))
84
+
85
+ return white_bg.astype(np.uint8)
86
+
87
 
88
  def extract_color_histogram(img, bins=(8, 8, 8)):
89
  hist = cv2.calcHist([img], [0, 1, 2], None, bins, [0, 256, 0, 256, 0, 256])
 
101
 
102
 
103
  def preprocessing_pipeline(pil_img):
104
+ clean_img_rgb = process_image(pil_img)
 
 
 
 
105
 
106
+ img_float = clean_img_rgb.astype(np.float32) / 255.0
107
  img_uint8 = (img_float * 255).astype(np.uint8)
108
 
109
  feat_color = extract_color_histogram(img_uint8)