init
This commit is contained in:
165
.gitignore
vendored
Normal file
165
.gitignore
vendored
Normal file
@ -0,0 +1,165 @@
|
||||
# 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/
|
||||
framers/
|
||||
poetry.lock
|
||||
yolov5s.trt
|
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
8
.idea/image-identification.iml
generated
Normal file
8
.idea/image-identification.iml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
4
.idea/misc.xml
generated
Normal file
4
.idea/misc.xml
generated
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Poetry (image-identification)" project-jdk-type="Python SDK" />
|
||||
</project>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/image-identification.iml" filepath="$PROJECT_DIR$/.idea/image-identification.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"[python]": {
|
||||
"editor.defaultFormatter": "ms-python.black-formatter"
|
||||
},
|
||||
"python.formatting.provider": "none"
|
||||
}
|
0
image_identification/__init__.py
Normal file
0
image_identification/__init__.py
Normal file
5
image_identification/__main__.py
Normal file
5
image_identification/__main__.py
Normal file
@ -0,0 +1,5 @@
|
||||
import asyncio
|
||||
from .main import run_app, analyze_video
|
||||
|
||||
# run_app()
|
||||
asyncio.run(analyze_video())
|
0
image_identification/controllers/__init__.py
Normal file
0
image_identification/controllers/__init__.py
Normal file
106
image_identification/controllers/image.py
Normal file
106
image_identification/controllers/image.py
Normal file
@ -0,0 +1,106 @@
|
||||
import asyncio
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import time
|
||||
|
||||
import aiohttp
|
||||
import cv2
|
||||
import fastdeploy as fd
|
||||
import numpy as np
|
||||
from fastapi import APIRouter, HTTPException, UploadFile, WebSocket, WebSocketDisconnect
|
||||
from fastapi.responses import StreamingResponse
|
||||
from fastdeploy.serving.utils import cv2_to_base64
|
||||
|
||||
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", tags=["analyze"])
|
||||
# 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"], summary="summary", description="description")
|
||||
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")
|
||||
|
||||
|
||||
@router.websocket("/analyze_video")
|
||||
async def analyze_video(websocket: WebSocket):
|
||||
rtmp_url = "rtmp://localhost/live/livestream"
|
||||
cap = cv2.VideoCapture(rtmp_url)
|
||||
frame_number = 0
|
||||
frame_rate = 30
|
||||
|
||||
await websocket.accept()
|
||||
fourcc = cv2.VideoWriter.fourcc("M", "P", "4", "V")
|
||||
# os.mkdir("framers")
|
||||
print(cap.get(3))
|
||||
print(cap.get(4))
|
||||
os.chdir("framers")
|
||||
video = None
|
||||
try:
|
||||
while True:
|
||||
ret, frame = cap.read()
|
||||
if not ret:
|
||||
print("无法读取视频帧")
|
||||
break
|
||||
# 是否保存帧
|
||||
if True:
|
||||
# if frame_number % 4 == 0:
|
||||
# 处理每一帧的操作,这里只是保存为图像文件
|
||||
|
||||
# cv2.imwrite(frame_filename, frame)
|
||||
result = model_instance.predict(frame)
|
||||
vis_im = fd.vision.vis_detection(frame, result, score_threshold=0.5)
|
||||
# cv2.imwrite(frame_filename, vis_im)
|
||||
if video is None:
|
||||
video = cv2.VideoWriter(
|
||||
"__output__.mp4",
|
||||
fourcc,
|
||||
30.0,
|
||||
(vis_im.shape[1], vis_im.shape[0]),
|
||||
)
|
||||
video.write(vis_im)
|
||||
print(vis_im.shape)
|
||||
await websocket.send_text(fd.vision.fd_result_to_json(result=result))
|
||||
# await websocket.send_text(frame_filename)
|
||||
else:
|
||||
print(frame.shape)
|
||||
video.write(frame)
|
||||
frame_number += 1
|
||||
except WebSocketDisconnect:
|
||||
print("close")
|
||||
await websocket.send_text("error")
|
||||
await websocket.close()
|
73
image_identification/main.py
Normal file
73
image_identification/main.py
Normal file
@ -0,0 +1,73 @@
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
import cv2
|
||||
import uvicorn
|
||||
from fastdeploy.serving.server import SimpleServer
|
||||
import fastdeploy as fd
|
||||
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 run_app():
|
||||
uvicorn.run("image_identification.main:app", host="0.0.0.0", port=8000, reload=True)
|
||||
|
||||
|
||||
async def analyze_video():
|
||||
rtmp_url = "rtmp://localhost/live/livestream"
|
||||
cap = cv2.VideoCapture(rtmp_url)
|
||||
frame_number = 0
|
||||
frame_rate = 30
|
||||
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
||||
print(fps)
|
||||
fourcc = cv2.VideoWriter.fourcc("M", "P", "4", "V")
|
||||
# os.mkdir("framers")
|
||||
|
||||
os.chdir("framers")
|
||||
video = None
|
||||
while True:
|
||||
start = time.perf_counter()
|
||||
ret, frame = cap.read()
|
||||
if not ret:
|
||||
print("无法读取视频帧")
|
||||
break
|
||||
# 是否保存帧
|
||||
if True:
|
||||
# if frame_number % 4 == 0:
|
||||
# 处理每一帧的操作,这里只是保存为图像文件
|
||||
|
||||
result = model_instance.predict(frame)
|
||||
print(fd.vision.fd_result_to_json(result=result))
|
||||
print(f"第{frame_number}帧")
|
||||
vis_im = fd.vision.vis_detection(frame, result, score_threshold=0.5)
|
||||
|
||||
if video is None:
|
||||
video = cv2.VideoWriter(
|
||||
"__output__.mp4",
|
||||
fourcc,
|
||||
fps,
|
||||
(vis_im.shape[1], vis_im.shape[0]),
|
||||
)
|
||||
video.write(vis_im)
|
||||
else:
|
||||
video.write(frame)
|
||||
end = time.perf_counter()
|
||||
cost = end - start
|
||||
print(f"spend time: {cost}\n\n")
|
||||
frame_number += 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_app()
|
1
image_identification/model/__init__.py
Normal file
1
image_identification/model/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .model import model_instance
|
26
image_identification/model/model.py
Normal file
26
image_identification/model/model.py
Normal file
@ -0,0 +1,26 @@
|
||||
import os
|
||||
|
||||
import fastdeploy as fd
|
||||
|
||||
# Configurations
|
||||
model_dir = "yolov5s_infer"
|
||||
device = "gpu"
|
||||
use_trt = True
|
||||
|
||||
# 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
|
||||
)
|
26
image_identification/predict.py
Normal file
26
image_identification/predict.py
Normal file
@ -0,0 +1,26 @@
|
||||
import os
|
||||
|
||||
import fastdeploy as fd
|
||||
|
||||
# Configurations
|
||||
model_dir = "yolov5s_infer"
|
||||
device = "gpu"
|
||||
use_trt = True
|
||||
|
||||
# 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
|
||||
)
|
1339
poetry.lock
generated
Normal file
1339
poetry.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
29
pyproject.toml
Normal file
29
pyproject.toml
Normal file
@ -0,0 +1,29 @@
|
||||
[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-gpu-python = { url = "https://bj.bcebos.com/fastdeploy/release/wheels/fastdeploy_gpu_python-1.0.7-cp310-cp310-manylinux1_x86_64.whl" }
|
||||
numpy = "^1.26.0"
|
||||
opencv-python = "^4.8.0.76"
|
||||
python-multipart = "^0.0.6"
|
||||
aiohttp = "^3.8.5"
|
||||
websockets = "^11.0.3"
|
||||
setuptools = "^68.2.2"
|
||||
|
||||
|
||||
[[tool.poetry.source]]
|
||||
name = "mirrors"
|
||||
url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
|
||||
priority = "default"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
|
||||
[tool.poetry.scripts]
|
||||
serve = "image_identification.main:run_app"
|
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
Reference in New Issue
Block a user