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

Refactor preprocessing pipeline to streamline image processing and remove unused background removal logic

Browse files
Files changed (2) hide show
  1. app.py +11 -76
  2. requirements.txt +1 -3
app.py CHANGED
@@ -6,7 +6,6 @@ import io
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
 
@@ -57,80 +56,16 @@ def extract_lbp_features(gray_img, P=8, R=1, method="uniform"):
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)
@@ -167,11 +102,11 @@ def index():
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()
 
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
 
 
56
  return hist
57
 
58
 
59
+ def preprocessing_pipeline(pil_img):
60
  """
61
+ Pipeline preprocessing sesuai dengan data training:
62
+ - Convert ke RGB
63
+ - Resize ke 100x100
64
+ - Extract color histogram & LBP features
65
  """
66
+ # Convert ke RGB dan resize ke 100x100 (sesuai dataset training)
67
+ img = np.array(pil_img.convert('RGB'))
68
+ img = cv2.resize(img, (100, 100), interpolation=cv2.INTER_AREA)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  img_float = img.astype(np.float32) / 255.0
71
  img_uint8 = (img_float * 255).astype(np.uint8)
 
102
  try:
103
  image = Image.open(file.stream)
104
 
105
+ # Preprocessing sesuai dengan data training (hanya resize 100x100)
106
  features = preprocessing_pipeline(image)
107
 
108
+ # Untuk tampilan, resize ke 100x100
109
+ preprocessed_img = cv2.resize(np.array(image.convert('RGB')), (100, 100), interpolation=cv2.INTER_AREA)
110
  preprocessed_pil = Image.fromarray(preprocessed_img)
111
 
112
  img_io = io.BytesIO()
requirements.txt CHANGED
@@ -5,6 +5,4 @@ joblib
5
  scikit-image
6
  scikit-learn
7
  pillow
8
- opencv-python-headless
9
- rembg
10
- onnxruntime
 
5
  scikit-image
6
  scikit-learn
7
  pillow
8
+ opencv-python-headless