AtthalaricNero commited on
Commit
06be3c9
·
1 Parent(s): af89ebc

Refactor preprocessing pipeline to integrate image background removal and resizing functionality

Browse files
Files changed (1) hide show
  1. app.py +82 -9
app.py CHANGED
@@ -57,12 +57,80 @@ def extract_lbp_features(gray_img, P=8, R=1, method="uniform"):
57
  return hist
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)
@@ -98,13 +166,18 @@ def index():
98
  if file:
99
  try:
100
  image = Image.open(file.stream)
 
 
 
 
 
 
 
 
101
  img_io = io.BytesIO()
102
- image.save(img_io, "PNG")
103
  encoded_img = base64.b64encode(img_io.getvalue()).decode("ascii")
104
  img_data = f"data:image/png;base64, {encoded_img}"
105
-
106
-
107
- features = preprocessing_pipeline(image)
108
 
109
  # Prediksi dengan probabilitas
110
  pred_index = model.predict(features)[0]
 
57
  return hist
58
 
59
 
60
+ def preprocess_image_like_dataset(pil_img, target_size=100):
61
+ """
62
+ Preprocessing gambar dari luar dataset menjadi seperti dataset:
63
+ - Background putih
64
+ - Ukuran 100x100
65
+ - Fokus pada buah (centered dan cropped)
66
+ """
67
+ # Step 1: Hapus background menggunakan rembg (AI-powered background removal)
68
+ img_no_bg = remove(pil_img)
69
+ img_array = np.array(img_no_bg)
70
+
71
+ # Step 2: Konversi RGBA ke RGB dengan background putih
72
+ if img_array.shape[-1] == 4:
73
+ # Ambil channel alpha
74
+ alpha = img_array[:, :, 3] / 255.0
75
+ rgb = img_array[:, :, :3]
76
+
77
+ # Gabungkan dengan background putih
78
+ white_bg = np.ones_like(rgb) * 255
79
+ img_array = (rgb * alpha[:, :, np.newaxis] + white_bg * (1 - alpha[:, :, np.newaxis])).astype(np.uint8)
80
+
81
+ # Step 3: Crop ke bounding box objek (hilangkan whitespace)
82
+ # Konversi ke grayscale untuk deteksi objek
83
+ gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
84
+ # Threshold untuk menemukan objek (non-putih)
85
+ _, thresh = cv2.threshold(gray, 250, 255, cv2.THRESH_BINARY_INV)
86
+
87
+ # Temukan contours
88
+ contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
89
+
90
+ if contours:
91
+ # Gabungkan semua contours untuk mendapat bounding box total
92
+ all_contours = np.vstack(contours)
93
+ x, y, w, h = cv2.boundingRect(all_contours)
94
+
95
+ # Tambahkan padding 5% untuk memberikan sedikit ruang
96
+ padding = int(max(w, h) * 0.05)
97
+ x = max(0, x - padding)
98
+ y = max(0, y - padding)
99
+ w = min(img_array.shape[1] - x, w + 2 * padding)
100
+ h = min(img_array.shape[0] - y, h + 2 * padding)
101
+
102
+ # Crop gambar
103
+ img_cropped = img_array[y:y+h, x:x+w]
104
+ else:
105
+ img_cropped = img_array
106
+
107
+ # Step 4: Resize dengan mempertahankan aspect ratio dan center pada canvas putih 100x100
108
+ h, w = img_cropped.shape[:2]
109
+
110
+ # Hitung scaling factor (fit ke target_size dengan margin)
111
+ scale = (target_size * 0.9) / max(h, w) # 0.9 untuk margin
112
+ new_w = int(w * scale)
113
+ new_h = int(h * scale)
114
+
115
+ # Resize objek
116
+ img_resized = cv2.resize(img_cropped, (new_w, new_h), interpolation=cv2.INTER_AREA)
117
+
118
+ # Buat canvas putih 100x100
119
+ canvas = np.ones((target_size, target_size, 3), dtype=np.uint8) * 255
120
+
121
+ # Hitung posisi untuk center objek
122
+ y_offset = (target_size - new_h) // 2
123
+ x_offset = (target_size - new_w) // 2
124
+
125
+ # Letakkan objek di tengah canvas
126
+ canvas[y_offset:y_offset+new_h, x_offset:x_offset+new_w] = img_resized
127
+
128
+ return canvas
129
+
130
 
131
+ def preprocessing_pipeline(pil_img):
132
+ # Preprocessing gambar menjadi 100x100 dengan background putih
133
+ img = preprocess_image_like_dataset(pil_img, target_size=100)
134
 
135
  img_float = img.astype(np.float32) / 255.0
136
  img_uint8 = (img_float * 255).astype(np.uint8)
 
166
  if file:
167
  try:
168
  image = Image.open(file.stream)
169
+
170
+ # Preprocessing image untuk prediksi dan tampilan
171
+ features = preprocessing_pipeline(image)
172
+
173
+ # Gunakan gambar yang sudah di-preprocessing untuk ditampilkan
174
+ preprocessed_img = preprocess_image_like_dataset(image, target_size=100)
175
+ preprocessed_pil = Image.fromarray(preprocessed_img)
176
+
177
  img_io = io.BytesIO()
178
+ preprocessed_pil.save(img_io, "PNG")
179
  encoded_img = base64.b64encode(img_io.getvalue()).decode("ascii")
180
  img_data = f"data:image/png;base64, {encoded_img}"
 
 
 
181
 
182
  # Prediksi dengan probabilitas
183
  pred_index = model.predict(features)[0]