This commit is contained in:
quantulr
2023-09-19 15:46:31 +08:00
commit 8d7e9a21f6
12 changed files with 1538 additions and 0 deletions

162
.gitignore vendored Normal file
View File

@ -0,0 +1,162 @@
# 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/
.DS_Store
yolov5s_infer/

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.formatting.provider": "none"
}

0
README.md Normal file
View File

View File

View File

@ -0,0 +1,24 @@
import requests
import json
import cv2
import fastdeploy as fd
from fastdeploy.serving.utils import cv2_to_base64
if __name__ == "__main__":
url = "http://127.0.0.1:8000/fd/yolov5s"
headers = {"Content-Type": "application/json"}
im = cv2.imread("origin.jpg")
print(cv2_to_base64(im))
data = {"data": {"image": cv2_to_base64(im)}, "parameters": {}}
resp = requests.post(url=url, headers=headers, data=json.dumps(data))
if resp.status_code == 200:
r_json = json.loads(resp.json()["result"])
det_result = fd.vision.utils.json_to_detection(r_json)
vis_im = fd.vision.vis_detection(im, det_result, score_threshold=0.5)
cv2.imwrite("visualized_result.jpg", vis_im)
print("Visualized result save in ./visualized_result.jpg")
else:
print("Error code:", resp.status_code)
print(resp.text)

View File

@ -0,0 +1,51 @@
import json
from fastapi import APIRouter, HTTPException, UploadFile
import cv2
import fastdeploy as fd
from fastdeploy.serving.utils import cv2_to_base64
import aiohttp
import asyncio
import io
from fastapi.responses import StreamingResponse
import numpy as np
from image_identification.model import model_instance
router = APIRouter(prefix="/image")
async def request(url: str, data, headers):
async with aiohttp.ClientSession() as session:
async with session.post(url=url, data=data, headers=headers) as response:
return await response.json()
@router.post("/get_visuallized_image")
async def getVisualized_image(image: UploadFile):
contents = await image.read()
nparr = np.fromstring(contents, np.uint8)
im = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
url = "http://127.0.0.1:8000/fd/yolov5s"
headers = {"Content-Type": "application/json"}
data = {"data": {"image": cv2_to_base64(im)}, "parameters": {}}
try:
resp = await asyncio.gather(
*[request(url=url, data=json.dumps(data), headers=headers)]
)
r_json = json.loads(resp[0]["result"])
det_result = fd.vision.utils.json_to_detection(r_json)
vis_im = fd.vision.vis_detection(im, det_result, score_threshold=0.5)
_res, im_jpg = cv2.imencode(".jpg", vis_im)
return StreamingResponse(io.BytesIO(im_jpg.tobytes()), media_type="image/jpeg")
except:
raise HTTPException(status_code=500, detail="获取失败")
@router.post("/analyze", tags=["analyze"])
async def analyze(image: UploadFile):
image_content = await image.read()
nparr = np.fromstring(image_content, np.uint8)
im = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
result = model_instance.predict(im)
vis_im = fd.vision.vis_detection(im, result, score_threshold=0.5)
im_jpg = cv2.imencode(".jpg", vis_im)[1]
return StreamingResponse(io.BytesIO(im_jpg.tobytes()), media_type="image/jpeg")

View File

@ -0,0 +1,23 @@
import fastdeploy as fd
from fastdeploy.serving.server import SimpleServer
import os
import logging
import uvicorn
from .controllers import image
from .model import model_instance
logging.getLogger().setLevel(logging.INFO)
# Create server, setup REST API
app = SimpleServer()
app.register(
task_name="fd/yolov5s",
model_handler=fd.serving.handler.VisionModelHandler,
predictor=model_instance,
)
app.include_router(image.router)
def runApp():
uvicorn.run("image_identification.main:app", host="0.0.0.0", port=8000, reload=True)

View File

@ -0,0 +1,25 @@
import os
import fastdeploy as fd
# Configurations
model_dir = "yolov5s_infer"
device = "cpu"
use_trt = False
# Prepare model
model_file = os.path.join(model_dir, "model.pdmodel")
params_file = os.path.join(model_dir, "model.pdiparams")
# Setup runtime option to select hardware, backend, etc.
option = fd.RuntimeOption()
if device.lower() == "gpu":
option.use_gpu()
if use_trt:
option.use_trt_backend()
option.set_trt_input_shape("images", [1, 3, 640, 640])
option.set_trt_cache_file("yolov5s.trt")
# Create model instance
model_instance = fd.vision.detection.YOLOv5(
model_file, params_file, runtime_option=option, model_format=fd.ModelFormat.PADDLE
)

1224
poetry.lock generated Normal file

File diff suppressed because it is too large Load Diff

23
pyproject.toml Normal file
View File

@ -0,0 +1,23 @@
[tool.poetry]
name = "image-identification"
version = "0.1.0"
description = ""
authors = ["quantulr <35954003+quantulr@users.noreply.github.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = ">=3.9,<3.13"
fastdeploy-python = {url = "https://bj.bcebos.com/fastdeploy/release/wheels/fastdeploy_python-1.0.7-cp39-cp39-macosx_10_14_x86_64.whl"}
numpy = "^1.26.0"
opencv-python = "^4.8.0.76"
python-multipart = "^0.0.6"
aiohttp = "^3.8.5"
sqlalchemy = "^2.0.21"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
[tool.poetry.scripts]
serve = "image_identification.main:runApp"

0
tests/__init__.py Normal file
View File