-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataset_creater.py
More file actions
89 lines (66 loc) · 2.93 KB
/
Dataset_creater.py
File metadata and controls
89 lines (66 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
"""Untitled6.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1c5MoZkw2FXiG_RcYIe-StsDDLmMKYDqw
"""
import os
import cv2
import numpy as np
from PIL import Image
from tqdm import tqdm
# Function to apply Gaussian blur to an image
def apply_gaussian_blur(image, kernel_size=(5, 5), sigma=1):
"""Applies Gaussian blur to the input image.
Parameters:
- image: Input image (numpy array).
- kernel_size: Size of the Gaussian kernel.
- sigma: Standard deviation for the Gaussian kernel.
Returns:
- Blurred image.
"""
return cv2.GaussianBlur(image, kernel_size, sigma)
# Create directories for saving the paired images
def create_dirs(output_dir, sharp_dir_name='sharp', blurred_dir_name='blurred'):
"""Creates directories for storing sharp and blurred images."""
sharp_dir = os.path.join(output_dir, sharp_dir_name)
blurred_dir = os.path.join(output_dir, blurred_dir_name)
os.makedirs(sharp_dir, exist_ok=True)
os.makedirs(blurred_dir, exist_ok=True)
return sharp_dir, blurred_dir
# Process and save the paired dataset
def create_paired_dataset(input_dir, output_dir, kernel_size=(15, 15), sigma=3):
"""Creates a paired dataset of sharp and blurred images.
Parameters:
- input_dir: Directory containing the original (sharp) images.
- output_dir: Directory where the sharp and blurred images will be saved.
- kernel_size: Size of the Gaussian kernel.
- sigma: Standard deviation for the Gaussian kernel.
"""
sharp_dir, blurred_dir = create_dirs(output_dir)
# Loop through all images in the input directory
for img_name in tqdm(os.listdir(input_dir)):
img_path = os.path.join(input_dir, img_name)
# Open the image and convert it to a numpy array
image = np.array(Image.open(img_path))
# Apply Gaussian blur to the image
blurred_image = apply_gaussian_blur(image, kernel_size, sigma)
# Convert back to PIL Image for saving
sharp_image_pil = Image.fromarray(image)
blurred_image_pil = Image.fromarray(blurred_image)
# Save the sharp and blurred images in their respective directories
sharp_image_pil.save(os.path.join(sharp_dir, img_name))
blurred_image_pil.save(os.path.join(blurred_dir, img_name))
# Example usage
input_dir = '/content/drive/MyDrive/Faces' # Directory containing your sharp images
output_dir = '/content/' # Directory to save the sharp and blurred images
# Create paired dataset with Gaussian blur (kernel size 5x5 and sigma 1)
create_paired_dataset(input_dir, output_dir, kernel_size=(25, 25), sigma=3)
import shutil
# Path to the folder you want to zip
folder_to_zip = '/content/sharp'
# Output path for the zip file (without extension)
output_zip = '/content/sharp_zip'
# Create a zip archive
shutil.make_archive(output_zip, 'zip', folder_to_zip)
print(f"Folder zipped as {output_zip}.zip")