Skip to content

Commit 38e643c

Browse files
Created using Colab
1 parent 660126c commit 38e643c

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

Crop_Image_from_Dataset.ipynb

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"mount_file_id": "1qWx9A_Hd8Zfr4YUc2LLcQyNnEo3pk7Cv",
8+
"authorship_tag": "ABX9TyPcv4NcEDksXiMZKsayKCcZ",
9+
"include_colab_link": true
10+
},
11+
"kernelspec": {
12+
"name": "python3",
13+
"display_name": "Python 3"
14+
},
15+
"language_info": {
16+
"name": "python"
17+
}
18+
},
19+
"cells": [
20+
{
21+
"cell_type": "markdown",
22+
"metadata": {
23+
"id": "view-in-github",
24+
"colab_type": "text"
25+
},
26+
"source": [
27+
"<a href=\"https://colab.research.google.com/github/masoudshahrian/DeepLearning-Code/blob/main/Crop_Image_from_Dataset.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"source": [
33+
"import cv2\n",
34+
"import dlib\n",
35+
"import os"
36+
],
37+
"metadata": {
38+
"id": "ZW3zk97M7cd3"
39+
},
40+
"execution_count": null,
41+
"outputs": []
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": null,
46+
"metadata": {
47+
"colab": {
48+
"base_uri": "https://localhost:8080/"
49+
},
50+
"id": "VqXFIcD-bqdQ",
51+
"outputId": "19a77c0a-1091-43ee-9e6a-a24f4985484f"
52+
},
53+
"outputs": [
54+
{
55+
"output_type": "stream",
56+
"name": "stdout",
57+
"text": [
58+
"تمامی تصاویر با موفقیت کراپ و ذخیره شدند.\n"
59+
]
60+
}
61+
],
62+
"source": [
63+
"\n",
64+
"\n",
65+
"# مسیر به دایرکتوری تصاویر CelebA\n",
66+
"input_dir = '/content/drive/MyDrive/kaggle-celebA/img_align_celeba/img_align_celeba/' # مسیر دایرکتوری تصاویر\n",
67+
"output_dir = '/content/drive/MyDrive/Image/CelebA_Image_Croped/' # مسیر دایرکتوری خروجی\n",
68+
"\n",
69+
"# ایجاد دایرکتوری خروجی در صورت عدم وجود\n",
70+
"os.makedirs(output_dir, exist_ok=True)\n",
71+
"\n",
72+
"# بارگذاری مدل شناسایی صورت\n",
73+
"detector = dlib.get_frontal_face_detector()\n",
74+
"\n",
75+
"# لیست کردن تصاویر در دایرکتوری ورودی\n",
76+
"for filename in os.listdir(input_dir):\n",
77+
" if filename.endswith('.jpg') or filename.endswith('.png'):\n",
78+
" # بارگذاری تصویر\n",
79+
" image_path = os.path.join(input_dir, filename)\n",
80+
" image = cv2.imread(image_path)\n",
81+
"\n",
82+
" # تبدیل تصویر به خاکستری\n",
83+
" gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
84+
"\n",
85+
" # شناسایی صورت‌ها\n",
86+
" faces = detector(gray)\n",
87+
"\n",
88+
" # پردازش هر صورت شناسایی شده\n",
89+
" for face in faces:\n",
90+
" # استخراج مختصات صورت\n",
91+
" x, y, w, h = (face.left(), face.top(), face.width(), face.height())\n",
92+
"\n",
93+
" # محاسبه مرکز صورت\n",
94+
" center_x = x + w // 2\n",
95+
" center_y = y + h // 2\n",
96+
"\n",
97+
" # محاسبه مختصات جدید برای کراپ\n",
98+
" crop_x1 = max(center_x - 64, 0)\n",
99+
" crop_x2 = min(center_x + 64, image.shape[1])\n",
100+
" crop_y1 = max(center_y - 64, 0)\n",
101+
" crop_y2 = min(center_y + 64, image.shape[0])\n",
102+
"\n",
103+
" # کراپ کردن تصویر\n",
104+
" cropped_image = image[crop_y1:crop_y2, crop_x1:crop_x2]\n",
105+
"\n",
106+
" # تغییر اندازه به 128x128\n",
107+
" resized_image = cv2.resize(cropped_image, (128, 128))\n",
108+
"\n",
109+
" # ذخیره تصویر کراپ شده\n",
110+
" output_path = os.path.join(output_dir, filename)\n",
111+
" cv2.imwrite(output_path, resized_image)\n",
112+
"\n",
113+
"print(\"تمامی تصاویر با موفقیت کراپ و ذخیره شدند.\")\n",
114+
"\n",
115+
"\n",
116+
"\n",
117+
"\n"
118+
]
119+
}
120+
]
121+
}

0 commit comments

Comments
 (0)