-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from timurgainutdinov1/module1
Module1
- Loading branch information
Showing
5 changed files
with
270 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
### Python template | ||
# 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 | ||
|
||
# pdm | ||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. | ||
#pdm.lock | ||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it | ||
# in version control. | ||
# https://pdm.fming.dev/#use-with-ide | ||
.pdm.toml | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm | ||
__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 maintained 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/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,23 @@ | ||
from transformers import ViTImageProcessor, ViTForImageClassification | ||
from PIL import Image | ||
"""Использование готовой модели для классификации изображений.""" | ||
|
||
import requests | ||
from PIL import Image | ||
from transformers import ViTForImageClassification, ViTImageProcessor | ||
|
||
url = 'https://img.freepik.com/premium-photo/a-house-on-a-mountain-with-a-mountain-in-the-background_759095-3394.jpg' | ||
url = ( | ||
"https://img.freepik.com/premium-photo/" | ||
"a-house-on-a-mountain-with-a-mountain-" | ||
"in-the-background_759095-3394.jpg" | ||
) | ||
image = Image.open(requests.get(url, stream=True).raw) | ||
|
||
processor = ViTImageProcessor.from_pretrained('google/vit-base-patch16-224') | ||
model = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224') | ||
processor = (ViTImageProcessor | ||
.from_pretrained("google/vit-base-patch16-224")) | ||
model = (ViTForImageClassification | ||
.from_pretrained("google/vit-base-patch16-224")) | ||
|
||
inputs = processor(images=image, return_tensors="pt") | ||
outputs = model(**inputs) | ||
logits = outputs.logits | ||
# model predicts one of the 1000 ImageNet classes | ||
predicted_class_idx = logits.argmax(-1).item() | ||
print("Predicted class:", model.config.id2label[predicted_class_idx]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,76 @@ | ||
from transformers import ViTImageProcessor, ViTForImageClassification | ||
"""Приложение Streamlit для классификации изображений.""" | ||
|
||
import requests | ||
import streamlit as st | ||
from PIL import Image | ||
import requests | ||
from transformers import ViTForImageClassification, ViTImageProcessor | ||
|
||
|
||
@st.cache | ||
# Загрузка модели для классификации изображений | ||
@st.cache_resource | ||
def load_model(): | ||
return ViTForImageClassification.from_pretrained('google/vit-base-patch16-224') | ||
"""Загрузка модели""" | ||
return (ViTForImageClassification | ||
.from_pretrained("google/vit-base-patch16-224")) | ||
|
||
|
||
@st.cache | ||
# Загрузка процессора для представления изображений в требуемом формате | ||
@st.cache_resource | ||
def load_processor(): | ||
return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224') | ||
"""Загрузка процессора для обработки изображений.""" | ||
return (ViTImageProcessor | ||
.from_pretrained("google/vit-base-patch16-224")) | ||
|
||
|
||
# Отображение текстового поля для ввода ссылки на изображение | ||
def get_image_link(): | ||
"""Ввод URL-адреса с изображением.""" | ||
return st.text_input("Введите ссылку на изображение для распознавания") | ||
|
||
|
||
# Получение изображения и вывод его на экран | ||
def load_image(url): | ||
"""Загрузка изображения из указанного URL-адреса | ||
с помощью библиотеки requests.""" | ||
img = Image.open(requests.get(url, stream=True).raw) | ||
st.image(img) | ||
return img | ||
|
||
|
||
# Обработка и распознавание изображения | ||
def image_classification(picture): | ||
"""Обработка и распознавание изображения. | ||
Принимает изображение, преобразует его в требуемый формат | ||
с помощью процессора, пропускает его через модель, | ||
получает вероятности классов и возвращает предсказанный класс. | ||
""" | ||
inputs = processor(images=picture, return_tensors="pt") | ||
outputs = model(**inputs) | ||
logits = outputs.logits | ||
predicted_class_idx = logits.argmax(-1).item() | ||
return model.config.id2label[predicted_class_idx] | ||
|
||
|
||
# Вывод результатов на экран | ||
def show_results(results): | ||
"""Вывод результатов""" | ||
st.write(results) | ||
|
||
|
||
processor = load_processor() | ||
model = load_model() | ||
|
||
st.title('Модель для классификации изображений vit-base-patch16-224') | ||
st.title("Модель для классификации изображений vit-base-patch16-224") | ||
|
||
link = get_image_link() | ||
|
||
result = st.button('Распознать изображение') | ||
result = st.button("Распознать изображение") | ||
|
||
if result: | ||
try: | ||
loaded_image = load_image(link) | ||
with st.spinner('Идет обработка... Пожалуйста, подождите...'): | ||
with st.spinner("Идет обработка... Пожалуйста, подождите..."): | ||
result = image_classification(loaded_image) | ||
st.markdown(f'Результаты распознавания: :rainbow[{result}]') | ||
st.markdown(f"Результаты распознавания: :rainbow[{result}]") | ||
st.snow() | ||
# Обработка исключений, которые приведут к ошибке в случае отсутствия ссылки | ||
# или указания ссылки на объект, который не является изображением | ||
except IOError: | ||
st.error(' Не удалось найти изображение по указанной ссылке. Попробуйте снова!', icon="😞") | ||
st.error( | ||
"Не удалось найти изображение по указанной ссылке. " | ||
"Попробуйте снова!", | ||
icon="😞", | ||
) |
Oops, something went wrong.