Initial commit

This commit is contained in:
Aleksey Filippov 2023-05-20 13:23:06 +04:00
commit fa9178391f
30 changed files with 2740 additions and 0 deletions

255
.gitignore vendored Normal file
View File

@ -0,0 +1,255 @@
# Created by https://www.toptal.com/developers/gitignore/api/python,pycharm+all
# Edit at https://www.toptal.com/developers/gitignore?templates=python,pycharm+all
### PyCharm+all ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# AWS User-specific
.idea/**/aws.xml
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# SonarLint plugin
.idea/sonarlint/
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
### PyCharm+all Patch ###
# Ignores the whole .idea folder and all .iml files
# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360
.idea/*
# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023
*.iml
modules.xml
.idea/misc.xml
*.ipr
# Sonarlint plugin
.idea/sonarlint
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
# End of https://www.toptal.com/developers/gitignore/api/python,pycharm+all
yolov5s.pt

24
.run/main (check).run.xml Normal file
View File

@ -0,0 +1,24 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="main (check)" type="PythonConfigurationType" factoryName="Python">
<module name="VideoAnalysis-dnn"/>
<option name="INTERPRETER_OPTIONS" value=""/>
<option name="PARENT_ENVS" value="true"/>
<envs>
<env name="PYTHONUNBUFFERED" value="1"/>
</envs>
<option name="SDK_HOME" value=""/>
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$"/>
<option name="IS_MODULE_SDK" value="true"/>
<option name="ADD_CONTENT_ROOTS" value="true"/>
<option name="ADD_SOURCE_ROOTS" value="true"/>
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py"/>
<option name="PARAMETERS"
value="2d147dcb-9c45-310b-a3ae-67d35a141755 &quot;test-data/lectionAudi/2021-03-12 13-51-07.JPG&quot;"/>
<option name="SHOW_COMMAND_LINE" value="false"/>
<option name="EMULATE_TERMINAL" value="false"/>
<option name="MODULE_MODE" value="false"/>
<option name="REDIRECT_INPUT" value="false"/>
<option name="INPUT_FILE" value=""/>
<method v="2"/>
</configuration>
</component>

24
.run/main (empty).run.xml Normal file
View File

@ -0,0 +1,24 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="main (empty)" type="PythonConfigurationType" factoryName="Python">
<module name="VideoAnalysis-dnn"/>
<option name="INTERPRETER_OPTIONS" value=""/>
<option name="PARENT_ENVS" value="true"/>
<envs>
<env name="PYTHONUNBUFFERED" value="1"/>
</envs>
<option name="SDK_HOME" value=""/>
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$"/>
<option name="IS_MODULE_SDK" value="true"/>
<option name="ADD_CONTENT_ROOTS" value="true"/>
<option name="ADD_SOURCE_ROOTS" value="true"/>
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py"/>
<option name="PARAMETERS"
value="2d147dcb-9c45-310b-a3ae-67d35a141755 &quot;test-data/lectionAudi/2021-03-12 13-49-32.JPG&quot;"/>
<option name="SHOW_COMMAND_LINE" value="false"/>
<option name="EMULATE_TERMINAL" value="false"/>
<option name="MODULE_MODE" value="false"/>
<option name="REDIRECT_INPUT" value="false"/>
<option name="INPUT_FILE" value=""/>
<method v="2"/>
</configuration>
</component>

View File

@ -0,0 +1,24 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="main (not empty)" type="PythonConfigurationType" factoryName="Python">
<module name="VideoAnalysis-dnn"/>
<option name="INTERPRETER_OPTIONS" value=""/>
<option name="PARENT_ENVS" value="true"/>
<envs>
<env name="PYTHONUNBUFFERED" value="1"/>
</envs>
<option name="SDK_HOME" value=""/>
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$"/>
<option name="IS_MODULE_SDK" value="true"/>
<option name="ADD_CONTENT_ROOTS" value="true"/>
<option name="ADD_SOURCE_ROOTS" value="true"/>
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py"/>
<option name="PARAMETERS"
value="2d147dcb-9c45-310b-a3ae-67d35a141755 &quot;test-data/lectionAudi/2021-03-12 13-48-31.JPG&quot;"/>
<option name="SHOW_COMMAND_LINE" value="false"/>
<option name="EMULATE_TERMINAL" value="false"/>
<option name="MODULE_MODE" value="false"/>
<option name="REDIRECT_INPUT" value="false"/>
<option name="INPUT_FILE" value=""/>
<method v="2"/>
</configuration>
</component>

1142
DetectionTestOntology.owx Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

15
README.md Normal file
View File

@ -0,0 +1,15 @@
Окружение: Python 3.10
Сервис: http://kb.athene.tech/swagger-ui/index.html
Запуск:
```python
main.py < Ontology
UID > < Image
Path >
main.py
43
f3f5d1 - a339 - 3256 - a5d9 - fd2f14ec1d60
"test-data/lectionAudi/2021-03-12 13-48-31.JPG"
```

16
imageWorking.py Normal file
View File

@ -0,0 +1,16 @@
import cv2 as cv
import numpy as np
from main import img_size as size
def image_transform(image):
image = cv.resize(image, (size[0], size[1]))
return image[:, :, ::-1]
def get_image_as_array(image_name):
image = cv.imread(image_name)
image: np.ndarray # приведение типов
image = image_transform(image)
return image

77
main.py Normal file
View File

@ -0,0 +1,77 @@
import os
import sys
import cv2 as cv
import numpy as np
import requests
import torch
import imageWorking
import neuralNetwork
import ontologyWorking
url = 'http://kb.athene.tech/api/1.0/ontology/'
img_path = 'data'
img_size = (1280, 720)
def analyse_file(uid, image_path):
if ontologyWorking.is_ontology_exists(uid, url):
raise Exception(f'Онтология с uid {uid} не существует')
if not os.path.isfile(image_path):
raise Exception(f'Изображение {image_path} не существует')
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
model.names = neuralNetwork.rename_entity(model.names)
list_sqwrl = ontologyWorking.get_list_sqwrl(uid, url)
results = model(imageWorking.get_image_as_array(image_path))
object_properties = list()
data_properties = list()
for i, res in enumerate(results.pred):
results_ndarray = np.array(res)
request = ontologyWorking.get_request_data(model.names, results_ndarray)
object_properties += request[0]
data_properties += request[1]
data = {
'data':
{
'objectPropertyAssertions': object_properties,
'dataPropertyAssertions': data_properties
}
}
result = {
'QueryGetNotEmpty': '',
'QueryGetCheck': '',
'QueryGetEmpty': ''
}
for sqwrl in list_sqwrl['response']:
response = requests.post(url + f'{uid}/query/{sqwrl["name"]}', json=data).json()
if response['error']:
raise Exception(response['error'])
result[sqwrl['name']] = [resultSQWRL['name']['value'] for resultSQWRL in response['response']['rows']]
print(f'Запрос {sqwrl["name"]} выполнен')
print()
print('Результаты')
if result['QueryGetNotEmpty']:
print('Аудитория занята')
elif result['QueryGetCheck']:
print('Аудиторию необходимо проверить')
elif result['QueryGetEmpty']:
print('Аудитория пустая')
else:
print('Неизвестное состояние')
# Вывод изображения
cv.imshow('result', results.render()[0][:, :, ::-1])
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == '__main__':
if len(sys.argv) != 3:
print(f'Запуск: {sys.argv[0]} <Ontology UID> <Image path>')
exit(1)
analyse_file(sys.argv[1], sys.argv[2])

6
neuralNetwork.py Normal file
View File

@ -0,0 +1,6 @@
def rename_entity(list_names: dict):
temp_list = list()
for entity in list_names.values():
entity: str
temp_list.append(entity.title().replace(' ', ''))
return temp_list

44
ontologyWorking.py Normal file
View File

@ -0,0 +1,44 @@
import requests
def is_ontology_exists(uid, url):
list_ontologies = requests.get(url).json()['response']
for onto in list_ontologies:
if onto['uid'] == uid:
return False
return True
def get_list_sqwrl(uid, url):
return requests.get(url + f'{uid}/query/', verify=False).json()
def get_entity_square(results_ndarray_i):
square = float((results_ndarray_i[2] - results_ndarray_i[0]) *
(results_ndarray_i[3] - results_ndarray_i[1]))
return abs(square)
def get_request_data(entities, results_ndarray):
classroom = 'classroom'
object_properties = list()
data_properties = list()
for i, entity in enumerate(entities): # запись в лист имен объектов и присутствие
if (results_ndarray[:, -1] == i).sum() > 0: # если объект найден
object_properties.append({'domain': entity,
'property': 'locatedIn',
'range': classroom})
else:
object_properties.append({'domain': entity,
'property': 'notLocatedIn',
'range': classroom})
for i in range(results_ndarray.shape[0]):
data_properties.append({'domain': entities[int(results_ndarray[i, 5])],
'property': 'hasArea',
'value': get_entity_square(results_ndarray[i])})
data_properties.append({'domain': entities[int(results_ndarray[i, 5])],
'property': 'hasConfidence',
'value': float(results_ndarray[i, 4])})
return object_properties, data_properties

52
requirements.txt Normal file
View File

@ -0,0 +1,52 @@
absl-py==1.4.0
astunparse==1.6.3
cachetools==5.3.0
certifi==2023.5.7
charset-normalizer==3.1.0
flatbuffers==23.5.9
gast==0.4.0
google-auth==2.18.1
google-auth-oauthlib==1.0.0
google-pasta==0.2.0
grpcio==1.54.2
h5py==3.8.0
idna==3.4
jax==0.4.10
keras==2.12.0
libclang==16.0.0
Markdown==3.4.3
MarkupSafe==2.1.2
matplotlib==3.7.1
ml-dtypes==0.1.0
numpy==1.23.5
oauthlib==3.2.2
opencv-python==4.7.0.72
opt-einsum==3.3.0
packaging==23.1
pandas==2.0.1
Pillow==9.5.0
protobuf==4.23.1
psutil==5.9.5
PyYAML==6.0
pyasn1==0.5.0
pyasn1-modules==0.3.0
requests==2.30.0
requests-oauthlib==1.3.1
rsa==4.9
scipy==1.10.1
six==1.16.0
tensorboard==2.12.3
tensorboard-data-server==0.7.0
tensorflow==2.12.0
tensorflow-estimator==2.12.0
tensorflow-intel==2.12.0
tensorflow-io-gcs-filesystem==0.31.0
termcolor==2.3.0
torch==2.0.1
torchvision==0.15.2
tqdm==4.65.0
typing_extensions==4.5.0
ultralytics==8.0.105
urllib3==1.26.15
Werkzeug==2.3.4
wrapt==1.14.1

BIN
test-data/images/01.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

BIN
test-data/images/02.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

BIN
test-data/images/03.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 328 KiB

BIN
test-data/images/04.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

BIN
test-data/images/05.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 MiB

BIN
test-data/images/06.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 MiB