AtthalaricNero commited on
Commit ·
af89ebc
1
Parent(s): 839dd40
Refactor image processing by removing the process_image function and integrating its logic into the preprocessing pipeline
Browse files41 unmodified lines4243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899010 unmodified lines10110210310410510610710810941 unmodified lines"Salak",]def process_image(pil_img, target_size=(100, 100)):img_io = io.BytesIO()pil_img.save(img_io, format='PNG')img_bytes = img_io.getvalue()output_array = remove(img_bytes)nparr = np.frombuffer(output_array, np.uint8)img_rgba = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)alpha = img_rgba[:, :, 3]coords = cv2.findNonZero(alpha)if coords is None:return np.array(pil_img.resize(target_size).convert("RGB"))x, y, w, h = cv2.boundingRect(coords)cropped_img = img_rgba[y:y+h, x:x+w]old_h, old_w = cropped_img.shape[:2]desired_w, desired_h = target_sizeratio = min(desired_w / old_w, desired_h / old_h)new_w = int(old_w * ratio)new_h = int(old_h * ratio)resized_img = cv2.resize(cropped_img, (new_w, new_h), interpolation=cv2.INTER_AREA)final_bg = np.zeros((desired_h, desired_w, 4), dtype=np.uint8)x_offset = (desired_w - new_w) // 2y_offset = (desired_h - new_h) // 2final_bg[y_offset:y_offset+new_h, x_offset:x_offset+new_w] = resized_imgwhite_bg = np.ones((desired_h, desired_w, 3), dtype=np.uint8) * 255alpha_channel = final_bg[:, :, 3] / 255.0for c in range(3):white_bg[:, :, c] = (final_bg[:, :, c] * alpha_channel + white_bg[:, :, c] * (1.0 - alpha_channel))return white_bg.astype(np.uint8)def extract_color_histogram(img, bins=(8, 8, 8)):hist = cv2.calcHist([img], [0, 1, 2], None, bins, [0, 256, 0, 256, 0, 256])hist = cv2.normalize(hist, hist).flatten()10 unmodified linesdef preprocessing_pipeline(pil_img):clean_img_rgb = process_image(pil_img)img_float = clean_img_rgb.astype(np.float32) / 255.0img_uint8 = (img_float * 255).astype(np.uint8)feat_color = extract_color_histogram(img_uint8)41 unmodified lines42434445464710 unmodified lines5859606162636465666768697041 unmodified lines"Salak",]def extract_color_histogram(img, bins=(8, 8, 8)):hist = cv2.calcHist([img], [0, 1, 2], None, bins, [0, 256, 0, 256, 0, 256])hist = cv2.normalize(hist, hist).flatten()10 unmodified linesdef preprocessing_pipeline(pil_img):img = np.array(pil_img)# ubah format dari RGBA menjadi RGBif img.shape[-1] == 4:img = img[:, :, :3]img_float = img.astype(np.float32) / 255.0img_uint8 = (img_float * 255).astype(np.uint8)feat_color = extract_color_histogram(img_uint8)
app.py+6-45