add docker, tests, scripts, test images and bugfixes

This commit is contained in:
Michael 2024-01-20 17:45:24 +01:00
parent 294fb339ed
commit 9f3bbb0d87
No known key found for this signature in database
GPG Key ID: 066ED7D431960C9B
30 changed files with 148 additions and 5 deletions

View File

@ -3,4 +3,5 @@
<component name="MarkdownSettingsMigration"> <component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" /> <option name="stateVersion" value="1" />
</component> </component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (Projekt_IO)" project-jdk-type="Python SDK" />
</project> </project>

1
README.md Normal file
View File

@ -0,0 +1 @@
Cat detection

6
docker/.dockerignore Normal file
View File

@ -0,0 +1,6 @@
*.md
/venv
.git
__pycache__
.pytest_cache
/tests

17
docker/Dockerfile Normal file
View File

@ -0,0 +1,17 @@
# Użyj obrazu bazowego Python 3.8
FROM python:3.11
# Ustaw katalog roboczy na /app
WORKDIR /app
# Skopiuj zawartość bieżącego katalogu do /app w kontenerze
COPY . /app
# Zainstaluj zależności
RUN pip install --no-cache-dir -r requirements.txt
# Wyeksponuj port 5000
EXPOSE 5000
# Uruchom aplikację po uruchomieniu kontenera
CMD ["python", "main.py"]

10
docker/docker-compose.yml Normal file
View File

@ -0,0 +1,10 @@
version: '3.3'
services:
cat-detection:
image: cat-detection
build:
context: ../
dockerfile: ./docker/Dockerfile
ports:
- "5000:5000"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 204 KiB

View File

@ -1,3 +1,5 @@
import os
from jproperties import Properties from jproperties import Properties
""" """
@ -10,9 +12,11 @@ from jproperties import Properties
def translate(to_translate, lang): def translate(to_translate, lang):
try: try:
config = Properties() config = Properties()
script_directory = os.path.dirname(os.path.abspath(__file__))
resources_path = os.path.join(script_directory, "./resources")
# Load properties file for given lang # Load properties file for given lang
with open(f"resources/{lang}.properties", 'rb') as config_file: with open(os.path.join(resources_path, f"./{lang}.properties"), 'rb') as config_file:
config.load(config_file, encoding='UTF-8') config.load(config_file, encoding='UTF-8')
# Translate labels for given to_translate dictionary # Translate labels for given to_translate dictionary

View File

@ -103,4 +103,4 @@ def upload_file():
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True) app.run(host='0.0.0.0')

View File

@ -3,3 +3,6 @@ numpy==1.26.3
pillow==10.2.0 pillow==10.2.0
keras==2.15.0 keras==2.15.0
jproperties==2.1.1 jproperties==2.1.1
tensorflow==2.15.0
werkzeug==3.0.1
pytest==7.4.4

24
scripts/build_image.bat Normal file
View File

@ -0,0 +1,24 @@
@echo off
chcp 65001 >nul
docker -v >nul 2>&1
if %errorlevel% neq 0 (
echo Docker is not installed.
exit /b 1
)
docker info >nul 2>&1
if %errorlevel% neq 0 (
echo Docker engine is not running.
exit /b 1
)
cd ../docker
echo Building docker image...
docker compose build cat-detection
if %errorlevel% neq 0 (
echo Building cdocker imagew failed.
exit /b 1
)
echo The image was built successfully.

27
scripts/build_image.sh Normal file
View File

@ -0,0 +1,27 @@
#!/bin/bash
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
docker -v > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo -e "\033[31mDocker is not installed.\033[0m"
exit 1
fi
docker info > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo -e "\033[31mDocker engine is not running.\033[0m"
exit 1
fi
cd ../docker
echo -e "\033[32mBuilding docker image...\033[0m"
docker-compose build cat-detection
if [ $? -ne 0 ]; then
echo -e "\033[31mBuilding docker image failed.\033[0m"
exit 1
fi
echo -e "\033[32mThe image was built successfully.\033[0m"

14
scripts/run_tests.bat Normal file
View File

@ -0,0 +1,14 @@
@echo off
echo Running unit tests.
set PYTHONPATH=%CD%;%PYTHONPATH%
cd %~dp0
pytest ../tests
if %ERRORLEVEL% equ 0 (
echo Tests passed successfully.
) else (
Tests failed.
echo Tests failed.
)

8
scripts/start.bat Normal file
View File

@ -0,0 +1,8 @@
@echo off
chcp 65001 >nul
docker compose -f ../docker/docker-compose.yml up cat-detection -d
if %errorlevel% neq 0 (
echo Starting docker container failed.
exit /b 1
)

11
scripts/start.sh Normal file
View File

@ -0,0 +1,11 @@
#!/bin/bash
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
docker-compose -f ../docker/docker-compose.yml up cat-detection -d
if [ $? -ne 0 ]; then
echo -e "\033[31mStarting docker container failed.\033[0m"
exit 1
fi

Binary file not shown.

After

Width:  |  Height:  |  Size: 360 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 159 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 326 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 221 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 281 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 469 KiB

View File

Before

Width:  |  Height:  |  Size: 7.1 KiB

After

Width:  |  Height:  |  Size: 7.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 255 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 345 KiB

View File

Before

Width:  |  Height:  |  Size: 105 KiB

After

Width:  |  Height:  |  Size: 105 KiB

19
tests/test_1.py Normal file
View File

@ -0,0 +1,19 @@
import os
from werkzeug.datastructures import FileStorage
from main import app
def test_upload_file():
with app.test_client() as test_client:
script_directory = os.path.dirname(os.path.abspath(__file__))
image_path = os.path.join(script_directory, "./img/tiger_cat/cat1.jpg")
image = FileStorage(
stream=open(image_path, "rb"),
filename="cat1.jpg",
content_type="image/jpeg",
)
response = test_client.post('/api/v1/detect-cat', data={'image': image}, content_type='multipart/form-data')
assert response.status_code == 200

View File

@ -1,5 +1,3 @@
import imghdr
""" """
Validation method. Validation method.
If everything fine then returns empty list. If everything fine then returns empty list.