Spaces:
Build error
Build error
Download preprocessing/process.py from vikaso001/phi2-rag-api: direct link, hf CLI and curl.
- Browser
- Download file 3.85 kB
-
https://huggingface.co/spaces/vikaso001/phi2-rag-api/resolve/main/preprocessing/process.py
- Command line
-
hf download hf://spaces/vikaso001/phi2-rag-api/preprocessing/process.py
-
curl -L -o process.py https://huggingface.co/spaces/vikaso001/phi2-rag-api/resolve/main/preprocessing/process.py
3.85 kB
| import cv2 | |
| from pathlib import Path | |
| import numpy as np | |
| from matplotlib import pyplot as plt | |
| def Advanced_gray(image_path): | |
| # Define the image you want to preprocess | |
| IMG_NAME = image_path # π Change to your image name | |
| SOURCE_IMG_PATH = Path("./") / IMG_NAME | |
| DEST_IMG_PATH = Path("test") / IMG_NAME | |
| # Ensure output directory exists | |
| DEST_IMG_PATH.parent.mkdir(parents=True, exist_ok=True) | |
| # Load in grayscale | |
| img = cv2.imread(str(SOURCE_IMG_PATH), cv2.IMREAD_GRAYSCALE) | |
| if img is None: | |
| print(f"β Failed to load image: {SOURCE_IMG_PATH}") | |
| return None | |
| else: | |
| print(f"β Loaded image: {SOURCE_IMG_PATH.name}") | |
| # Apply Gaussian blur to remove noise | |
| img = cv2.GaussianBlur(img, (5, 5), 0) | |
| # Apply adaptive thresholding | |
| img = cv2.adaptiveThreshold( | |
| img, 255, | |
| cv2.ADAPTIVE_THRESH_GAUSSIAN_C, | |
| cv2.THRESH_BINARY, | |
| 11, 2 | |
| ) | |
| # Save the processed image | |
| cv2.imwrite(str(DEST_IMG_PATH), img) | |
| print(f"β Processed image saved to: {DEST_IMG_PATH}") | |
| return img | |
| # Sharpening function | |
| def sharpen_image(image): | |
| kernel = np.array([[0, -1, 0], | |
| [-1, 5,-1], | |
| [0, -1, 0]]) | |
| sharpened = cv2.filter2D(image, -1, kernel) | |
| return sharpened | |
| def enhance_contrast(image): | |
| lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) | |
| l, a, b = cv2.split(lab) | |
| clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) | |
| cl = clahe.apply(l) | |
| limg = cv2.merge((cl, a, b)) | |
| enhanced_img = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR) | |
| return enhanced_img | |
| def modify_image(image_path): | |
| x = Advanced_gray(image_path) | |
| if x is None: | |
| return None | |
| y = sharpen_image(x) | |
| # Convert grayscale to BGR before enhancing contrast | |
| y_bgr = cv2.cvtColor(y, cv2.COLOR_GRAY2BGR) | |
| z = enhance_contrast(y_bgr) | |
| output_path = Path("test") / Path(image_path).name | |
| cv2.imwrite(str(output_path), z) | |
| return z | |
| def batch_process_images(image_dir, limit=10): | |
| image_dir_path = Path(image_dir) | |
| output_dir = Path("test") | |
| output_dir.mkdir(parents=True, exist_ok=True) | |
| images_processed = 0 | |
| processed_images = [] | |
| for img_path in image_dir_path.glob("*.jpg"): | |
| if images_processed >= limit: | |
| break | |
| print(f"Processing image: {img_path.name}") | |
| processed_img = modify_image(str(img_path)) | |
| if processed_img is not None: | |
| print(f"Saved processed image to: {output_dir / img_path.name}") | |
| processed_images.append(processed_img) | |
| images_processed += 1 | |
| else: | |
| print(f"Failed to process image: {img_path.name}") | |
| # Plot all processed images in a single plot | |
| if processed_images: | |
| cols = 5 | |
| rows = (len(processed_images) + cols - 1) // cols | |
| plt.figure(figsize=(15, 3 * rows)) | |
| for i, img in enumerate(processed_images): | |
| plt.subplot(rows, cols, i + 1) | |
| if len(img.shape) == 2: # grayscale | |
| plt.imshow(img, cmap='gray') | |
| else: | |
| plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) | |
| plt.axis('off') | |
| plt.tight_layout() | |
| plt.show() | |
| # if __name__ == "__main__": | |
| # # Process a single image | |
| # image_path = "data/10.jpg" | |
| # processed_img = modify_image(image_path) | |
| # if processed_img is not None: | |
| # plt.figure(figsize=(6,6)) | |
| # if len(processed_img.shape) == 2: | |
| # plt.imshow(processed_img, cmap='gray') | |
| # else: | |
| # plt.imshow(cv2.cvtColor(processed_img, cv2.COLOR_BGR2RGB)) | |
| # plt.axis('off') | |
| # plt.show() | |