Skip to content

Commit 643888e

Browse files
authored
Merge pull request #1 from ErickOF/feature-datagen
Adding datagen script
2 parents 6dab5e5 + 039c5e6 commit 643888e

File tree

6 files changed

+490
-0
lines changed

6 files changed

+490
-0
lines changed

.gitignore

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Created by https://www.toptal.com/developers/gitignore/api/c++,python,jupyternotebooks
2+
# Edit at https://www.toptal.com/developers/gitignore?templates=c++,python,jupyternotebooks
3+
4+
### C++ ###
15
# Prerequisites
26
*.d
37

@@ -30,3 +34,191 @@
3034
*.exe
3135
*.out
3236
*.app
37+
38+
### JupyterNotebooks ###
39+
# gitignore template for Jupyter Notebooks
40+
# website: http://jupyter.org/
41+
42+
.ipynb_checkpoints
43+
*/.ipynb_checkpoints/*
44+
45+
# IPython
46+
profile_default/
47+
ipython_config.py
48+
49+
# Remove previous ipynb_checkpoints
50+
# git rm -r .ipynb_checkpoints/
51+
52+
### Python ###
53+
# Byte-compiled / optimized / DLL files
54+
__pycache__/
55+
*.py[cod]
56+
*$py.class
57+
58+
# C extensions
59+
60+
# Distribution / packaging
61+
.Python
62+
build/
63+
develop-eggs/
64+
dist/
65+
downloads/
66+
eggs/
67+
.eggs/
68+
lib/
69+
lib64/
70+
parts/
71+
sdist/
72+
var/
73+
wheels/
74+
share/python-wheels/
75+
*.egg-info/
76+
.installed.cfg
77+
*.egg
78+
MANIFEST
79+
80+
# PyInstaller
81+
# Usually these files are written by a python script from a template
82+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
83+
*.manifest
84+
*.spec
85+
86+
# Installer logs
87+
pip-log.txt
88+
pip-delete-this-directory.txt
89+
90+
# Unit test / coverage reports
91+
htmlcov/
92+
.tox/
93+
.nox/
94+
.coverage
95+
.coverage.*
96+
.cache
97+
nosetests.xml
98+
coverage.xml
99+
*.cover
100+
*.py,cover
101+
.hypothesis/
102+
.pytest_cache/
103+
cover/
104+
105+
# Translations
106+
*.mo
107+
*.pot
108+
109+
# Django stuff:
110+
*.log
111+
local_settings.py
112+
db.sqlite3
113+
db.sqlite3-journal
114+
115+
# Flask stuff:
116+
instance/
117+
.webassets-cache
118+
119+
# Scrapy stuff:
120+
.scrapy
121+
122+
# Sphinx documentation
123+
docs/_build/
124+
125+
# PyBuilder
126+
.pybuilder/
127+
target/
128+
129+
# Jupyter Notebook
130+
131+
# IPython
132+
133+
# pyenv
134+
# For a library or package, you might want to ignore these files since the code is
135+
# intended to run in multiple environments; otherwise, check them in:
136+
# .python-version
137+
138+
# pipenv
139+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
140+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
141+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
142+
# install all needed dependencies.
143+
#Pipfile.lock
144+
145+
# poetry
146+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
147+
# This is especially recommended for binary packages to ensure reproducibility, and is more
148+
# commonly ignored for libraries.
149+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
150+
#poetry.lock
151+
152+
# pdm
153+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
154+
#pdm.lock
155+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
156+
# in version control.
157+
# https://pdm.fming.dev/#use-with-ide
158+
.pdm.toml
159+
160+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
161+
__pypackages__/
162+
163+
# Celery stuff
164+
celerybeat-schedule
165+
celerybeat.pid
166+
167+
# SageMath parsed files
168+
*.sage.py
169+
170+
# Environments
171+
.env
172+
.venv
173+
env/
174+
venv/
175+
ENV/
176+
env.bak/
177+
venv.bak/
178+
179+
# Spyder project settings
180+
.spyderproject
181+
.spyproject
182+
183+
# Rope project settings
184+
.ropeproject
185+
186+
# mkdocs documentation
187+
/site
188+
189+
# mypy
190+
.mypy_cache/
191+
.dmypy.json
192+
dmypy.json
193+
194+
# Pyre type checker
195+
.pyre/
196+
197+
# pytype static type analyzer
198+
.pytype/
199+
200+
# Cython debug symbols
201+
cython_debug/
202+
203+
# PyCharm
204+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
205+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
206+
# and can be added to the global gitignore or merged into this file. For a more nuclear
207+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
208+
#.idea/
209+
210+
### Python Patch ###
211+
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
212+
poetry.toml
213+
214+
# ruff
215+
.ruff_cache/
216+
217+
# LSP config files
218+
pyrightconfig.json
219+
220+
# End of https://www.toptal.com/developers/gitignore/api/c++,python,jupyternotebooks
221+
222+
# Ignore generate images from datagen tool
223+
tools/datagen/src/imgs/*_grayscale_image*
224+
tools/datagen/src/imgs/*_sobel_*

tools/datagen/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# **SystemC Image Data Generation Tool for Project Testbench**
2+
3+
This tool will help to generate the required data for the testbench. It
4+
generates grayscale, Sobel x, Sobel y, and combined Sobel x-y images.
5+
6+
7+
## **Installation**
8+
9+
Before running, make sure you have installed Python OpenCV. You can run:
10+
```shell
11+
cd tools/datagen/
12+
chmod +x install_pyopencv.sh
13+
./install_pyopencv.sh
14+
```
15+
16+
It will display the OpenCV version at the end of the installation.
17+
18+
19+
## **Usage**
20+
21+
There are two versions of the script, a JupyterNotebook and a normal Python
22+
script. For both of them, in the main function, the variables `img_name` and
23+
`img_ext` must be updated. The `img_name` is only the name of the image that
24+
is saved in `tools/datagen/src/imgs` and the `img_ext` is the extension of the
25+
image. For instance:
26+
27+
```python
28+
img_name: str = 'car'
29+
img_ext: str = 'jpg
30+
```
31+
32+
will load `tools/datagen/src/imgs/car.jpg` image and generate:
33+
34+
* tools/datagen/src/imgs/car\_grayscale\_image.jpg
35+
* tools/datagen/src/imgs/car\_sobel\_combined\_result.jpg
36+
* tools/datagen/src/imgs/car\_sobel\_x\_result.jpg
37+
* tools/datagen/src/imgs/car\_sobel\_y\_result.jpg
38+
39+
These generated images are ignored and must not be committed to the repository.
40+
41+
42+
### **Python Script**
43+
44+
To run the Python script:
45+
46+
```shell
47+
cd tools/datagen/
48+
python3 datagen.py
49+
```
50+
51+
52+
### **JupyterNotebook**
53+
54+
You can open the file from the JupyterNotebook app or upload it to
55+
[Google Colab](https://colab.research.google.com/).
56+

tools/datagen/datagen.ipynb

Lines changed: 149 additions & 0 deletions
Large diffs are not rendered by default.

tools/datagen/datagen.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/python3
2+
"""
3+
Script to generate the data required for SystemC testbench.
4+
"""
5+
from typing import Tuple
6+
7+
import cv2
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
11+
12+
def apply_sobel_filter(image_path: str
13+
)-> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
14+
"""Applies the Sobel filter in X and Y to a input image and returns each
15+
step.
16+
17+
Args:
18+
image_path (str): Path to the input image
19+
20+
Returns:
21+
Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: A tuple
22+
containing the gray image, the sobel in x, the sobel in y, and the
23+
sobel combining x and y.
24+
"""
25+
# Read the image
26+
image: np.ndarray = cv2.imread(image_path)
27+
28+
# BGR to grayscale
29+
gray_img: np.ndarray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
30+
31+
# Apply Sobel in x direction
32+
sobel_x_img: np.ndarray = cv2.Sobel(gray_img, cv2.CV_64F, 1, 0, ksize=3)
33+
sobel_x_uint8: np.ndarray = np.uint8(np.absolute(sobel_x_img))
34+
35+
# Apply Sobel in y direction
36+
sobel_y_img: np.ndarray = cv2.Sobel(gray_img, cv2.CV_64F, 0, 1, ksize=3)
37+
sobel_y_uint8: np.ndarray = np.uint8(np.absolute(sobel_y_img))
38+
39+
# Combine sobel
40+
sobel_combined_img: np.ndarray = cv2.magnitude(sobel_x_img, sobel_y_img)
41+
42+
# Convert to 8-bit image
43+
sobel_combined_uint8: np.ndarray = np.uint8(np.absolute(sobel_combined_img))
44+
45+
# Display the results
46+
plt.figure(figsize=(10, 8))
47+
48+
plt.subplot(2, 2, 1)
49+
plt.title('Original Image')
50+
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
51+
plt.axis('off')
52+
53+
plt.subplot(2, 2, 2)
54+
plt.title('Grayscale Image')
55+
plt.imshow(gray_img, cmap='gray')
56+
plt.axis('off')
57+
58+
plt.subplot(2, 2, 3)
59+
plt.title('Sobel X')
60+
plt.imshow(np.uint8(np.absolute(sobel_x_img)), cmap='gray')
61+
plt.axis('off')
62+
63+
plt.subplot(2, 2, 4)
64+
plt.title('Sobel Y')
65+
plt.imshow(np.uint8(np.absolute(sobel_y_img)), cmap='gray')
66+
plt.axis('off')
67+
68+
plt.figure(figsize=(5, 5))
69+
plt.title('Sobel Combined')
70+
plt.imshow(sobel_combined_uint8, cmap='gray')
71+
plt.axis('off')
72+
73+
plt.show()
74+
75+
return (gray_img, sobel_x_uint8, sobel_y_uint8, sobel_combined_uint8)
76+
77+
78+
if __name__ == '__main__':
79+
img_name: str = 'car'
80+
img_ext: str = 'jpg'
81+
img_path: str = f'src/imgs/{img_name}.{img_ext}'
82+
gray, sobel_x, sobel_y, sobel_combined = apply_sobel_filter(img_path)
83+
84+
cv2.imwrite(f'src/imgs/{img_name}_grayscale_image.{img_ext}', gray)
85+
cv2.imwrite(f'src/imgs/{img_name}_sobel_x_result.{img_ext}', np.uint8(np.absolute(sobel_x)))
86+
cv2.imwrite(f'src/imgs/{img_name}_sobel_y_result.{img_ext}', np.uint8(np.absolute(sobel_y)))
87+
cv2.imwrite(f'src/imgs/{img_name}_sobel_combined_result.{img_ext}', sobel_combined)
88+

tools/datagen/install_pyopencv.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
sudo pip3 install opencv-python
4+
5+
python3 -c "import cv2; print(f'OpenCV: {cv2.__version__}')"

tools/datagen/src/imgs/car.jpg

107 KB
Loading

0 commit comments

Comments
 (0)