Skip to content

Commit cff45ff

Browse files
Created using Colab
1 parent 94c6e7c commit cff45ff

File tree

1 file changed

+377
-0
lines changed

1 file changed

+377
-0
lines changed
+377
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,377 @@
1+
{
2+
"metadata": {
3+
"kernelspec": {
4+
"language": "python",
5+
"display_name": "Python 3",
6+
"name": "python3"
7+
},
8+
"language_info": {
9+
"name": "python",
10+
"version": "3.10.12",
11+
"mimetype": "text/x-python",
12+
"codemirror_mode": {
13+
"name": "ipython",
14+
"version": 3
15+
},
16+
"pygments_lexer": "ipython3",
17+
"nbconvert_exporter": "python",
18+
"file_extension": ".py"
19+
},
20+
"kaggle": {
21+
"accelerator": "gpu",
22+
"dataSources": [
23+
{
24+
"sourceId": 37705,
25+
"sourceType": "datasetVersion",
26+
"datasetId": 29561
27+
}
28+
],
29+
"dockerImageVersionId": 30822,
30+
"isInternetEnabled": true,
31+
"language": "python",
32+
"sourceType": "notebook",
33+
"isGpuEnabled": true
34+
},
35+
"colab": {
36+
"name": "celeba-dataset-face-croped-[64*64]",
37+
"provenance": [],
38+
"include_colab_link": true
39+
}
40+
},
41+
"nbformat_minor": 0,
42+
"nbformat": 4,
43+
"cells": [
44+
{
45+
"cell_type": "markdown",
46+
"metadata": {
47+
"id": "view-in-github",
48+
"colab_type": "text"
49+
},
50+
"source": [
51+
"<a href=\"https://colab.research.google.com/github/masoudshahrian/DeepLearning-Code/blob/main/celeba_dataset_face_croped_%5B64_64%5D.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
52+
]
53+
},
54+
{
55+
"source": [
56+
"# IMPORTANT: RUN THIS CELL IN ORDER TO IMPORT YOUR KAGGLE DATA SOURCES,\n",
57+
"# THEN FEEL FREE TO DELETE THIS CELL.\n",
58+
"# NOTE: THIS NOTEBOOK ENVIRONMENT DIFFERS FROM KAGGLE'S PYTHON\n",
59+
"# ENVIRONMENT SO THERE MAY BE MISSING LIBRARIES USED BY YOUR\n",
60+
"# NOTEBOOK.\n",
61+
"import kagglehub\n",
62+
"jessicali9530_celeba_dataset_path = kagglehub.dataset_download('jessicali9530/celeba-dataset')\n",
63+
"\n",
64+
"print('Data source import complete.')\n"
65+
],
66+
"metadata": {
67+
"id": "VzQ60JUpVJ2y"
68+
},
69+
"cell_type": "code",
70+
"outputs": [],
71+
"execution_count": null
72+
},
73+
{
74+
"cell_type": "code",
75+
"source": [
76+
"# # This Python 3 environment comes with many helpful analytics libraries installed\n",
77+
"# # It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n",
78+
"# # For example, here's several helpful packages to load\n",
79+
"\n",
80+
"# import numpy as np # linear algebra\n",
81+
"# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
82+
"\n",
83+
"# # Input data files are available in the read-only \"../input/\" directory\n",
84+
"# # For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n",
85+
"\n",
86+
"# import os\n",
87+
"# for dirname, _, filenames in os.walk('/kaggle/input'):\n",
88+
"# for filename in filenames:\n",
89+
"# print(os.path.join(dirname, filename))\n",
90+
"\n",
91+
"# # 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",
92+
"# # You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session"
93+
],
94+
"metadata": {
95+
"_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
96+
"_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
97+
"trusted": true,
98+
"execution": {
99+
"iopub.status.busy": "2024-12-31T14:00:22.340417Z",
100+
"iopub.execute_input": "2024-12-31T14:00:22.340718Z",
101+
"iopub.status.idle": "2024-12-31T14:00:22.344574Z",
102+
"shell.execute_reply.started": "2024-12-31T14:00:22.340692Z",
103+
"shell.execute_reply": "2024-12-31T14:00:22.343643Z"
104+
},
105+
"id": "SBWyO33hVJ20"
106+
},
107+
"outputs": [],
108+
"execution_count": null
109+
},
110+
{
111+
"cell_type": "code",
112+
"source": [
113+
"!pip install dlib"
114+
],
115+
"metadata": {
116+
"trusted": true,
117+
"execution": {
118+
"iopub.status.busy": "2024-12-31T19:23:53.286376Z",
119+
"iopub.execute_input": "2024-12-31T19:23:53.286655Z",
120+
"iopub.status.idle": "2024-12-31T19:23:57.60329Z",
121+
"shell.execute_reply.started": "2024-12-31T19:23:53.286634Z",
122+
"shell.execute_reply": "2024-12-31T19:23:57.602309Z"
123+
},
124+
"id": "vssMfka4VJ21",
125+
"outputId": "507f001c-17aa-4b08-fb40-666f61a32bc9"
126+
},
127+
"outputs": [
128+
{
129+
"name": "stdout",
130+
"text": "Requirement already satisfied: dlib in /usr/local/lib/python3.10/dist-packages (19.24.2)\n",
131+
"output_type": "stream"
132+
}
133+
],
134+
"execution_count": null
135+
},
136+
{
137+
"cell_type": "code",
138+
"source": [
139+
"import cv2\n",
140+
"import dlib\n",
141+
"import os\n",
142+
"\n",
143+
"# مسیر به دایرکتوری تصاویر CelebA\n",
144+
"input_dir = '/kaggle/input/celeba-dataset/img_align_celeba/img_align_celeba/' # مسیر دایرکتوری تصاویر\n",
145+
"output_dir_64 = '/kaggle/working/img_align_celeba/CelebA_Image_Cropped_64/' # مسیر دایرکتوری خروجی برای تصاویر 64x64\n",
146+
"\n",
147+
"# ایجاد دایرکتوری خروجی در صورت عدم وجود\n",
148+
"os.makedirs(output_dir_64, exist_ok=True)\n",
149+
"\n",
150+
"# بارگذاری مدل شناسایی صورت\n",
151+
"detector = dlib.get_frontal_face_detector()\n",
152+
"\n",
153+
"# لیست کردن تصاویر در دایرکتوری ورودی\n",
154+
"for filename in os.listdir(input_dir):\n",
155+
" if filename.endswith('.jpg') or filename.endswith('.png'):\n",
156+
" # بارگذاری تصویر\n",
157+
" image_path = os.path.join(input_dir, filename)\n",
158+
" image = cv2.imread(image_path)\n",
159+
"\n",
160+
" # تبدیل تصویر به خاکستری\n",
161+
" gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
162+
"\n",
163+
" # شناسایی صورت‌ها\n",
164+
" faces = detector(gray)\n",
165+
"\n",
166+
" # پردازش اولین صورت شناسایی شده\n",
167+
" for face in faces:\n",
168+
" # استخراج مختصات صورت\n",
169+
" x, y, w, h = (face.left(), face.top(), face.width(), face.height())\n",
170+
"\n",
171+
" # محاسبه مرکز صورت\n",
172+
" center_x = x + w // 2\n",
173+
" center_y = y + h // 2\n",
174+
"\n",
175+
" # محاسبه مختصات جدید برای کراپ\n",
176+
" crop_x1 = max(center_x - 32, 0)\n",
177+
" crop_x2 = min(center_x + 32, image.shape[1])\n",
178+
" crop_y1 = max(center_y - 32, 0)\n",
179+
" crop_y2 = min(center_y + 32, image.shape[0])\n",
180+
"\n",
181+
" # کراپ کردن تصویر\n",
182+
" cropped_image = image[crop_y1:crop_y2, crop_x1:crop_x2]\n",
183+
"\n",
184+
" # تغییر اندازه به 64x64\n",
185+
" resized_image_64 = cv2.resize(cropped_image, (64, 64))\n",
186+
"\n",
187+
" # ذخیره تصویر کراپ شده با همان نام اصلی\n",
188+
" output_path_64 = os.path.join(output_dir_64, filename)\n",
189+
" cv2.imwrite(output_path_64, resized_image_64)\n",
190+
"\n",
191+
" break # پردازش فقط اولین صورت شناسایی شده در هر تصویر\n",
192+
"\n",
193+
"print(\"تمامی تصاویر با موفقیت کراپ و ذخیره شدند.\")\n"
194+
],
195+
"metadata": {
196+
"trusted": true,
197+
"execution": {
198+
"iopub.status.busy": "2024-12-31T19:24:03.333117Z",
199+
"iopub.execute_input": "2024-12-31T19:24:03.333419Z",
200+
"iopub.status.idle": "2024-12-31T20:01:08.073201Z",
201+
"shell.execute_reply.started": "2024-12-31T19:24:03.333398Z",
202+
"shell.execute_reply": "2024-12-31T20:01:08.072318Z"
203+
},
204+
"id": "_ct7E0wsVJ22",
205+
"outputId": "b1f88e19-bcf2-4ff3-95c5-29ec465de04c"
206+
},
207+
"outputs": [
208+
{
209+
"name": "stdout",
210+
"text": "تمامی تصاویر با موفقیت کراپ و ذخیره شدند.\n",
211+
"output_type": "stream"
212+
}
213+
],
214+
"execution_count": null
215+
},
216+
{
217+
"cell_type": "code",
218+
"source": [
219+
"# import cv2\n",
220+
"# import dlib\n",
221+
"# import os\n",
222+
"\n",
223+
"# # مسیر به دایرکتوری تصاویر CelebA\n",
224+
"# input_dir = '/kaggle/input/celeba-dataset/img_align_celeba/img_align_celeba/' # مسیر دایرکتوری تصاویر\n",
225+
"# output_dir_128 = '/kaggle/working/img_align_celeba/CelebA_Image_Cropped_128/' # مسیر دایرکتوری خروجی برای تصاویر 128x128\n",
226+
"# output_dir_64 = '/kaggle/working/img_align_celeba/CelebA_Image_Cropped_64/' # مسیر دایرکتوری خروجی برای تصاویر 64x64\n",
227+
"\n",
228+
"# # ایجاد دایرکتوری خروجی در صورت عدم وجود\n",
229+
"# os.makedirs(output_dir_128, exist_ok=True)\n",
230+
"# os.makedirs(output_dir_64, exist_ok=True)\n",
231+
"\n",
232+
"# # بارگذاری مدل شناسایی صورت\n",
233+
"# detector = dlib.get_frontal_face_detector()\n",
234+
"\n",
235+
"# # لیست کردن تصاویر در دایرکتوری ورودی\n",
236+
"# image_count = 0 # شمارنده برای تعداد تصاویر پردازش شده\n",
237+
"\n",
238+
"# for filename in os.listdir(input_dir):\n",
239+
"# if filename.endswith('.jpg') or filename.endswith('.png'):\n",
240+
"# # بارگذاری تصویر\n",
241+
"# image_path = os.path.join(input_dir, filename)\n",
242+
"# image = cv2.imread(image_path)\n",
243+
"\n",
244+
"# # تبدیل تصویر به خاکستری\n",
245+
"# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
246+
"\n",
247+
"# # شناسایی صورت‌ها\n",
248+
"# faces = detector(gray)\n",
249+
"\n",
250+
"# # پردازش هر صورت شناسایی شده\n",
251+
"# for face in faces:\n",
252+
"# # استخراج مختصات صورت\n",
253+
"# x, y, w, h = (face.left(), face.top(), face.width(), face.height())\n",
254+
"\n",
255+
"# # محاسبه مرکز صورت\n",
256+
"# center_x = x + w // 2\n",
257+
"# center_y = y + h // 2\n",
258+
"\n",
259+
"# # محاسبه مختصات جدید برای کراپ\n",
260+
"# crop_x1 = max(center_x - 64, 0)\n",
261+
"# crop_x2 = min(center_x + 64, image.shape[1])\n",
262+
"# crop_y1 = max(center_y - 64, 0)\n",
263+
"# crop_y2 = min(center_y + 64, image.shape[0])\n",
264+
"\n",
265+
"# # کراپ کردن تصویر\n",
266+
"# cropped_image = image[crop_y1:crop_y2, crop_x1:crop_x2]\n",
267+
"\n",
268+
"# # تغییر اندازه به 128x128\n",
269+
"# resized_image_128 = cv2.resize(cropped_image, (128, 128))\n",
270+
"# # ذخیره تصویر کراپ شده با ابعاد 128x128\n",
271+
"# output_path_128 = os.path.join(output_dir_128, f'cropped_128_{image_count}.jpg')\n",
272+
"# cv2.imwrite(output_path_128, resized_image_128)\n",
273+
"\n",
274+
"# # تغییر اندازه به 64x64\n",
275+
"# resized_image_64 = cv2.resize(cropped_image, (64, 64))\n",
276+
"# # ذخیره تصویر کراپ شده با ابعاد 64x64\n",
277+
"# output_path_64 = os.path.join(output_dir_64, f'cropped_64_{image_count}.jpg')\n",
278+
"# cv2.imwrite(output_path_64, resized_image_64)\n",
279+
"\n",
280+
"# image_count += 1 # افزایش شمارنده\n",
281+
"\n",
282+
"# print(f\"تمامی {image_count} تصویر با موفقیت کراپ و ذخیره شدند.\")"
283+
],
284+
"metadata": {
285+
"trusted": true,
286+
"execution": {
287+
"iopub.status.busy": "2024-12-31T14:38:11.987533Z",
288+
"iopub.execute_input": "2024-12-31T14:38:11.987846Z",
289+
"iopub.status.idle": "2024-12-31T14:38:11.992025Z",
290+
"shell.execute_reply.started": "2024-12-31T14:38:11.987815Z",
291+
"shell.execute_reply": "2024-12-31T14:38:11.991185Z"
292+
},
293+
"id": "87JB2HbvVJ23"
294+
},
295+
"outputs": [],
296+
"execution_count": null
297+
},
298+
{
299+
"cell_type": "code",
300+
"source": [
301+
"# import cv2\n",
302+
"# import dlib\n",
303+
"# import os\n",
304+
"\n",
305+
"# # مسیر به دایرکتوری تصاویر CelebA\n",
306+
"# input_dir = '/kaggle/input/celeba-dataset/img_align_celeba/img_align_celeba/' # مسیر دایرکتوری تصاویر\n",
307+
"# output_dir_128 = '/kaggle/working/img_align_celeba/CelebA_Face_Cropped_128/' # مسیر دایرکتوری خروجی برای تصاویر 128x128\n",
308+
"# output_dir_64 = '/kaggle/working/img_align_celeba/CelebA_Face_Cropped_64/' # مسیر دایرکتوری خروجی برای تصاویر 64x64\n",
309+
"\n",
310+
"# # ایجاد دایرکتوری خروجی در صورت عدم وجود\n",
311+
"# os.makedirs(output_dir_128, exist_ok=True)\n",
312+
"# os.makedirs(output_dir_64, exist_ok=True)\n",
313+
"\n",
314+
"# # بارگذاری مدل شناسایی صورت\n",
315+
"# detector = dlib.get_frontal_face_detector()\n",
316+
"\n",
317+
"# # لیست کردن تصاویر در دایرکتوری ورودی\n",
318+
"# for filename in os.listdir(input_dir):\n",
319+
"# if filename.endswith('.jpg') or filename.endswith('.png'):\n",
320+
"# # بارگذاری تصویر\n",
321+
"# image_path = os.path.join(input_dir, filename)\n",
322+
"# image = cv2.imread(image_path)\n",
323+
"\n",
324+
"# # تبدیل تصویر به خاکستری\n",
325+
"# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
326+
"\n",
327+
"# # شناسایی صورت‌ها\n",
328+
"# faces = detector(gray)\n",
329+
"\n",
330+
"# # پردازش هر صورت شناسایی شده\n",
331+
"# for i, face in enumerate(faces):\n",
332+
"# # استخراج مختصات صورت\n",
333+
"# x, y, w, h = (face.left(), face.top(), face.width(), face.height())\n",
334+
"\n",
335+
"# # محاسبه مرکز صورت\n",
336+
"# center_x = x + w // 2\n",
337+
"# center_y = y + h // 2\n",
338+
"\n",
339+
"# # محاسبه مختصات جدید برای کراپ\n",
340+
"# crop_x1 = max(center_x - 64, 0)\n",
341+
"# crop_x2 = min(center_x + 64, image.shape[1])\n",
342+
"# crop_y1 = max(center_y - 64, 0)\n",
343+
"# crop_y2 = min(center_y + 64, image.shape[0])\n",
344+
"\n",
345+
"# # کراپ کردن تصویر\n",
346+
"# cropped_image = image[crop_y1:crop_y2, crop_x1:crop_x2]\n",
347+
"\n",
348+
"# # تغییر اندازه به 128x128\n",
349+
"# resized_image_128 = cv2.resize(cropped_image, (128, 128))\n",
350+
"# # ذخیره تصویر کراپ شده با ابعاد 128x128\n",
351+
"# output_path_128 = os.path.join(output_dir_128, f'{os.path.splitext(filename)[0]}.jpg')\n",
352+
"# cv2.imwrite(output_path_128, resized_image_128)\n",
353+
"\n",
354+
"# # تغییر اندازه به 64x64\n",
355+
"# resized_image_64 = cv2.resize(cropped_image, (64, 64))\n",
356+
"# # ذخیره تصویر کراپ شده با ابعاد 64x64\n",
357+
"# output_path_64 = os.path.join(output_dir_64, f'{os.path.splitext(filename)[0]}.jpg')\n",
358+
"# cv2.imwrite(output_path_64, resized_image_64)\n",
359+
"\n",
360+
"# print(\"تمامی تصاویر با موفقیت کراپ و ذخیره شدند.\")"
361+
],
362+
"metadata": {
363+
"trusted": true,
364+
"execution": {
365+
"iopub.status.busy": "2024-12-31T14:38:11.992884Z",
366+
"iopub.execute_input": "2024-12-31T14:38:11.993177Z",
367+
"iopub.status.idle": "2024-12-31T14:38:12.012442Z",
368+
"shell.execute_reply.started": "2024-12-31T14:38:11.993148Z",
369+
"shell.execute_reply": "2024-12-31T14:38:12.011681Z"
370+
},
371+
"id": "OcPhk7ehVJ23"
372+
},
373+
"outputs": [],
374+
"execution_count": null
375+
}
376+
]
377+
}

0 commit comments

Comments
 (0)