Skip to content

Commit 8e73361

Browse files
Created using Colab
1 parent 50336b3 commit 8e73361

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

celeba_dataset_face_croped.ipynb

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
{
2+
"metadata": {
3+
"kernelspec": {
4+
"language": "python",
5+
"display_name": "Python 3",
6+
"name": "python3"
7+
},
8+
"language_info": {
9+
"pygments_lexer": "ipython3",
10+
"nbconvert_exporter": "python",
11+
"version": "3.6.4",
12+
"file_extension": ".py",
13+
"codemirror_mode": {
14+
"name": "ipython",
15+
"version": 3
16+
},
17+
"name": "python",
18+
"mimetype": "text/x-python"
19+
},
20+
"kaggle": {
21+
"accelerator": "none",
22+
"dataSources": [
23+
{
24+
"sourceId": 37705,
25+
"sourceType": "datasetVersion",
26+
"datasetId": 29561
27+
}
28+
],
29+
"isInternetEnabled": true,
30+
"language": "python",
31+
"sourceType": "notebook",
32+
"isGpuEnabled": false
33+
},
34+
"colab": {
35+
"name": "celeba-dataset-face-croped",
36+
"provenance": [],
37+
"include_colab_link": true
38+
}
39+
},
40+
"nbformat_minor": 0,
41+
"nbformat": 4,
42+
"cells": [
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {
46+
"id": "view-in-github",
47+
"colab_type": "text"
48+
},
49+
"source": [
50+
"<a href=\"https://colab.research.google.com/github/masoudshahrian/DeepLearning-Code/blob/main/celeba_dataset_face_croped.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
51+
]
52+
},
53+
{
54+
"source": [
55+
"# IMPORTANT: RUN THIS CELL IN ORDER TO IMPORT YOUR KAGGLE DATA SOURCES,\n",
56+
"# THEN FEEL FREE TO DELETE THIS CELL.\n",
57+
"# NOTE: THIS NOTEBOOK ENVIRONMENT DIFFERS FROM KAGGLE'S PYTHON\n",
58+
"# ENVIRONMENT SO THERE MAY BE MISSING LIBRARIES USED BY YOUR\n",
59+
"# NOTEBOOK.\n",
60+
"import kagglehub\n",
61+
"jessicali9530_celeba_dataset_path = kagglehub.dataset_download('jessicali9530/celeba-dataset')\n",
62+
"\n",
63+
"print('Data source import complete.')\n"
64+
],
65+
"metadata": {
66+
"id": "nW8T2OlPS23i"
67+
},
68+
"cell_type": "code",
69+
"outputs": [],
70+
"execution_count": null
71+
},
72+
{
73+
"cell_type": "code",
74+
"source": [
75+
"# # This Python 3 environment comes with many helpful analytics libraries installed\n",
76+
"# # It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n",
77+
"# # For example, here's several helpful packages to load\n",
78+
"\n",
79+
"# import numpy as np # linear algebra\n",
80+
"# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
81+
"\n",
82+
"# # Input data files are available in the read-only \"../input/\" directory\n",
83+
"# # For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n",
84+
"\n",
85+
"# import os\n",
86+
"# for dirname, _, filenames in os.walk('/kaggle/input'):\n",
87+
"# for filename in filenames:\n",
88+
"# print(os.path.join(dirname, filename))\n",
89+
"\n",
90+
"# # You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\"\n",
91+
"# # You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session"
92+
],
93+
"metadata": {
94+
"_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
95+
"_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
96+
"trusted": true,
97+
"id": "eSYs8S5MS23m"
98+
},
99+
"outputs": [],
100+
"execution_count": null
101+
},
102+
{
103+
"cell_type": "code",
104+
"source": [
105+
"!pip install dlib"
106+
],
107+
"metadata": {
108+
"trusted": true,
109+
"id": "bTkl19pJS23n"
110+
},
111+
"outputs": [],
112+
"execution_count": null
113+
},
114+
{
115+
"cell_type": "code",
116+
"source": [
117+
"import cv2\n",
118+
"import dlib\n",
119+
"import os\n",
120+
"\n",
121+
"# Path to the CelebA images directory\n",
122+
"input_dir = '/kaggle/input/celeba-dataset/img_align_celeba/img_align_celeba/' # Path to the images directory\n",
123+
"output_dir_128 = '/kaggle/working/img_align_celeba/CelebA_Image_Cropped_128/' # Path to the output directory for 128x128 images\n",
124+
"output_dir_64 = '/kaggle/working/img_align_celeba/CelebA_Image_Cropped_64/' # Path to the output directory for 64x64 images\n",
125+
"\n",
126+
"# Create output directories if they do not exist\n",
127+
"os.makedirs(output_dir_128, exist_ok=True)\n",
128+
"os.makedirs(output_dir_64, exist_ok=True)\n",
129+
"\n",
130+
"# Load the face detection model\n",
131+
"detector = dlib.get_frontal_face_detector()\n",
132+
"\n",
133+
"# List images in the input directory\n",
134+
"image_count = 0 # Counter for the number of processed images\n",
135+
"\n",
136+
"for filename in os.listdir(input_dir):\n",
137+
" if filename.endswith('.jpg') or filename.endswith('.png'):\n",
138+
" # Load the image\n",
139+
" image_path = os.path.join(input_dir, filename)\n",
140+
" image = cv2.imread(image_path)\n",
141+
"\n",
142+
" # Convert the image to grayscale\n",
143+
" gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
144+
"\n",
145+
" # Detect faces\n",
146+
" faces = detector(gray)\n",
147+
"\n",
148+
" # Process each detected face\n",
149+
" for face in faces:\n",
150+
" # Extract face coordinates\n",
151+
" x, y, w, h = (face.left(), face.top(), face.width(), face.height())\n",
152+
"\n",
153+
" # Calculate the center of the face\n",
154+
" center_x = x + w // 2\n",
155+
" center_y = y + h // 2\n",
156+
"\n",
157+
" # Calculate new coordinates for cropping\n",
158+
" crop_x1 = max(center_x - 64, 0)\n",
159+
" crop_x2 = min(center_x + 64, image.shape[1])\n",
160+
" crop_y1 = max(center_y - 64, 0)\n",
161+
" crop_y2 = min(center_y + 64, image.shape[0])\n",
162+
"\n",
163+
" # Crop the image\n",
164+
" cropped_image = image[crop_y1:crop_y2, crop_x1:crop_x2]\n",
165+
"\n",
166+
" # Resize to 128x128\n",
167+
" resized_image_128 = cv2.resize(cropped_image, (128, 128))\n",
168+
" # Save the cropped image with dimensions 128x128\n",
169+
" output_path_128 = os.path.join(output_dir_128, f'cropped_128_{image_count}.jpg')\n",
170+
" cv2.imwrite(output_path_128, resized_image_128)\n",
171+
"\n",
172+
" # Resize to 64x64\n",
173+
" resized_image_64 = cv2.resize(cropped_image, (64, 64))\n",
174+
" # Save the cropped image with dimensions 64x64\n",
175+
" output_path_64 = os.path.join(output_dir_64, f'cropped_64_{image_count}.jpg')\n",
176+
" cv2.imwrite(output_path_64, resized_image_64)\n",
177+
"\n",
178+
" image_count += 1 # Increment the counter\n",
179+
"\n",
180+
"print(f\"All {image_count} images have been successfully cropped and saved.\")"
181+
],
182+
"metadata": {
183+
"trusted": true,
184+
"id": "126soy8fS23n"
185+
},
186+
"outputs": [],
187+
"execution_count": null
188+
}
189+
]
190+
}

0 commit comments

Comments
 (0)