Skip to content

Commit

Permalink
Add conda environment file
Browse files Browse the repository at this point in the history
  • Loading branch information
epassaro committed Apr 28, 2023
1 parent 1b2049d commit ace959f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 163 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
# diaxi-training-tools
# diaxi-training-tools

Herramientas para la creación del dataset y el entrenamiento del modelo de Desafío IA por la Identidad

## Instalación

> Requiere tener instalado [**Miniconda**](https://docs.conda.io/en/latest/miniconda.html) o una variante de [**Miniforge**](https://github.com/conda-forge/miniforge/releases/latest) (se recomienda `mambaforge`).
```
$ mamba env create -f environment.yml
$ conda activate diaxi-training-tools
```
95 changes: 48 additions & 47 deletions create_diaxi_dataset.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,53 @@
"len(lost) # Perdimos 51 archivos en el camino :("
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Enderezar las imágenes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import math\n",
"from typing import Tuple, Union\n",
"\n",
"import cv2\n",
"import numpy as np\n",
"from deskew import determine_skew\n",
"\n",
"THRESHOLD = 10 # límite de 10 grados\n",
"\n",
"def rotate(image: np.ndarray, angle: float, background: Union[int, Tuple[int, int, int]]) -> np.ndarray:\n",
" old_width, old_height = image.shape[:2]\n",
" angle_radian = math.radians(angle)\n",
" width = abs(np.sin(angle_radian) * old_height) + abs(np.cos(angle_radian) * old_width)\n",
" height = abs(np.sin(angle_radian) * old_width) + abs(np.cos(angle_radian) * old_height)\n",
"\n",
" image_center = tuple(np.array(image.shape[1::-1]) / 2)\n",
" rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)\n",
" rot_mat[1, 2] += (width - old_width) / 2\n",
" rot_mat[0, 2] += (height - old_height) / 2\n",
" \n",
" return cv2.warpAffine(image, rot_mat, (int(round(height)), int(round(width))), borderValue=background)\n",
"\n",
"for i in os.listdir(\"fixed/images/\"):\n",
" image = cv2.imread(f\"fixed/images/{i}\")\n",
" grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
" angle = determine_skew(grayscale)\n",
"\n",
" if abs(angle) < THRESHOLD:\n",
" rotated = rotate(image, angle, (0, 0, 0))\n",
" cv2.imwrite(f\"fixed/images/{i}\", rotated)"
]
},
{
"attachments": {},
"cell_type": "markdown",
Expand All @@ -86,7 +133,6 @@
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"from PIL import Image\n",
"\n",
Expand Down Expand Up @@ -310,51 +356,6 @@
" json.dump(result, f, indent=2)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Enderezar las imágenes\n",
"\n",
"Se procede a enderezar las imágenes con el script `deskew.py`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"from typing import Tuple, Union\n",
"\n",
"import cv2\n",
"import numpy as np\n",
"from deskew import determine_skew\n",
"\n",
"\n",
"def rotate(image: np.ndarray, angle: float, background: Union[int, Tuple[int, int, int]]) -> np.ndarray:\n",
" old_width, old_height = image.shape[:2]\n",
" angle_radian = math.radians(angle)\n",
" width = abs(np.sin(angle_radian) * old_height) + abs(np.cos(angle_radian) * old_width)\n",
" height = abs(np.sin(angle_radian) * old_width) + abs(np.cos(angle_radian) * old_height)\n",
"\n",
" image_center = tuple(np.array(image.shape[1::-1]) / 2)\n",
" rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)\n",
" rot_mat[1, 2] += (width - old_width) / 2\n",
" rot_mat[0, 2] += (height - old_height) / 2\n",
" \n",
" return cv2.warpAffine(image, rot_mat, (int(round(height)), int(round(width))), borderValue=background)\n",
"\n",
"for i in os.listdir(\"fixed/images\"):\n",
" image = cv2.imread(f\"fixed/images/{i}\")\n",
" grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
" angle = determine_skew(grayscale)\n",
" rotated = rotate(image, angle, (0, 0, 0))\n",
" cv2.imwrite(f\"fixed/images/{i}\", rotated)"
]
},
{
"attachments": {},
"cell_type": "markdown",
Expand Down Expand Up @@ -466,7 +467,7 @@
"cp -r fixed/images dataset-diaxi-coco\n",
"cp fixed/result_final.json dataset-diaxi-coco/result.json\n",
"cd dataset-diaxi-coco\n",
"zip -r ../dataset-diaxi-coco333.zip images result.json"
"zip -r ../dataset-diaxi-coco.zip images result.json"
]
}
],
Expand Down
18 changes: 18 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: diaxi-training-tools

channels:
- pytorch
- nvidia
- conda-forge

dependencies:
- python =3.8
- pip
- opencv
- pytorch >=1.8
- torchvision
- torchaudio
- pytorch-cuda =11.7

- pip:
- -r requirements.txt
4 changes: 1 addition & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
deskew
ipykernel
pillow
opencv-python
pyodi
git+https://github.com/facebookresearch/[email protected]#egg=detectron2
112 changes: 0 additions & 112 deletions scripts/cocosplit.py

This file was deleted.

0 comments on commit ace959f

Please sign in to comment.