push streame
This commit is contained in:
@ -3,6 +3,7 @@ import io
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
|
import subprocess
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
@ -58,10 +59,12 @@ async def analyze(image: UploadFile):
|
|||||||
|
|
||||||
@router.websocket("/analyze_video")
|
@router.websocket("/analyze_video")
|
||||||
async def analyze_video(websocket: WebSocket):
|
async def analyze_video(websocket: WebSocket):
|
||||||
rtmp_url = "rtmp://localhost/live/livestream"
|
rtmp_url = "rtmp://192.168.0.136/live/livestream"
|
||||||
|
output_url = "rtmp://192.168.0.136/live/livestream_output"
|
||||||
cap = cv2.VideoCapture(rtmp_url)
|
cap = cv2.VideoCapture(rtmp_url)
|
||||||
frame_number = 0
|
frame_number = 0
|
||||||
frame_rate = 30
|
frame_rate = 30
|
||||||
|
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
||||||
|
|
||||||
await websocket.accept()
|
await websocket.accept()
|
||||||
fourcc = cv2.VideoWriter.fourcc("M", "P", "4", "V")
|
fourcc = cv2.VideoWriter.fourcc("M", "P", "4", "V")
|
||||||
@ -70,8 +73,11 @@ async def analyze_video(websocket: WebSocket):
|
|||||||
print(cap.get(4))
|
print(cap.get(4))
|
||||||
os.chdir("framers")
|
os.chdir("framers")
|
||||||
video = None
|
video = None
|
||||||
|
ffmpeg_command = None
|
||||||
|
ffmpeg_process = None
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
|
start = time.perf_counter()
|
||||||
ret, frame = cap.read()
|
ret, frame = cap.read()
|
||||||
if not ret:
|
if not ret:
|
||||||
print("无法读取视频帧")
|
print("无法读取视频帧")
|
||||||
@ -85,20 +91,56 @@ async def analyze_video(websocket: WebSocket):
|
|||||||
result = model_instance.predict(frame)
|
result = model_instance.predict(frame)
|
||||||
vis_im = fd.vision.vis_detection(frame, result, score_threshold=0.5)
|
vis_im = fd.vision.vis_detection(frame, result, score_threshold=0.5)
|
||||||
# cv2.imwrite(frame_filename, vis_im)
|
# cv2.imwrite(frame_filename, vis_im)
|
||||||
|
width = vis_im.shape[1]
|
||||||
|
height = vis_im.shape[0]
|
||||||
if video is None:
|
if video is None:
|
||||||
video = cv2.VideoWriter(
|
video = cv2.VideoWriter(
|
||||||
"__output__.mp4",
|
"__output__.mp4",
|
||||||
fourcc,
|
fourcc,
|
||||||
30.0,
|
30.0,
|
||||||
(vis_im.shape[1], vis_im.shape[0]),
|
(width, height),
|
||||||
)
|
)
|
||||||
|
if ffmpeg_command is None:
|
||||||
|
# 创建ffmpeg进程以推送视频流
|
||||||
|
ffmpeg_cmd = [
|
||||||
|
"ffmpeg",
|
||||||
|
"-y",
|
||||||
|
"-f",
|
||||||
|
"rawvideo",
|
||||||
|
"-vcodec",
|
||||||
|
"rawvideo",
|
||||||
|
"-s",
|
||||||
|
f"{width}x{height}",
|
||||||
|
"-pix_fmt",
|
||||||
|
"bgr24",
|
||||||
|
"-r",
|
||||||
|
str(fps),
|
||||||
|
"-i",
|
||||||
|
"-",
|
||||||
|
"-c:v",
|
||||||
|
"libx264",
|
||||||
|
"-pix_fmt",
|
||||||
|
"yuv420p",
|
||||||
|
"-preset",
|
||||||
|
"ultrafast",
|
||||||
|
"-f",
|
||||||
|
"flv",
|
||||||
|
output_url,
|
||||||
|
]
|
||||||
|
if ffmpeg_process is None:
|
||||||
|
ffmpeg_process = subprocess.Popen(ffmpeg_cmd, stdin=subprocess.PIPE)
|
||||||
|
ffmpeg_process.stdin.write(vis_im.tobytes())
|
||||||
video.write(vis_im)
|
video.write(vis_im)
|
||||||
print(vis_im.shape)
|
print(f"第{frame_number}帧")
|
||||||
|
print(result)
|
||||||
await websocket.send_text(fd.vision.fd_result_to_json(result=result))
|
await websocket.send_text(fd.vision.fd_result_to_json(result=result))
|
||||||
# await websocket.send_text(frame_filename)
|
# await websocket.send_text(frame_filename)
|
||||||
else:
|
else:
|
||||||
print(frame.shape)
|
print(frame.shape)
|
||||||
video.write(frame)
|
video.write(frame)
|
||||||
|
end = time.perf_counter()
|
||||||
|
cost = end - start
|
||||||
|
print(f"time spend {cost}")
|
||||||
frame_number += 1
|
frame_number += 1
|
||||||
except WebSocketDisconnect:
|
except WebSocketDisconnect:
|
||||||
print("close")
|
print("close")
|
||||||
|
@ -4,8 +4,8 @@ import fastdeploy as fd
|
|||||||
|
|
||||||
# Configurations
|
# Configurations
|
||||||
model_dir = "yolov5s_infer"
|
model_dir = "yolov5s_infer"
|
||||||
device = "gpu"
|
device = "cpu"
|
||||||
use_trt = True
|
use_trt = False
|
||||||
|
|
||||||
# Prepare model
|
# Prepare model
|
||||||
model_file = os.path.join(model_dir, "model.pdmodel")
|
model_file = os.path.join(model_dir, "model.pdmodel")
|
||||||
|
14
poetry.lock
generated
14
poetry.lock
generated
@ -420,13 +420,13 @@ url = "https://pypi.tuna.tsinghua.edu.cn/simple"
|
|||||||
reference = "mirrors"
|
reference = "mirrors"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fastdeploy-gpu-python"
|
name = "fastdeploy-python"
|
||||||
version = "1.0.7"
|
version = "1.0.7"
|
||||||
description = "Deploy Kit Tool For Deeplearning models."
|
description = "Deploy Kit Tool For Deeplearning models."
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = "*"
|
python-versions = "*"
|
||||||
files = [
|
files = [
|
||||||
{file = "fastdeploy_gpu_python-1.0.7-cp310-cp310-manylinux1_x86_64.whl", hash = "sha256:f92e9414e69b56b092d1327118912e8c5fa0acc32a0d9f4e971145e888f8d72d"},
|
{file = "fastdeploy_python-1.0.7-cp39-cp39-macosx_10_14_x86_64.whl", hash = "sha256:6f1b9c6eeefed4b63b9ba28335d857d6dd4fd9b6e9090a6e9688c60b553a4c42"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
@ -444,7 +444,7 @@ mypy = ["mypy (==0.600)"]
|
|||||||
|
|
||||||
[package.source]
|
[package.source]
|
||||||
type = "url"
|
type = "url"
|
||||||
url = "https://bj.bcebos.com/fastdeploy/release/wheels/fastdeploy_gpu_python-1.0.7-cp310-cp310-manylinux1_x86_64.whl"
|
url = "https://bj.bcebos.com/fastdeploy/release/wheels/fastdeploy_python-1.0.7-cp39-cp39-macosx_10_14_x86_64.whl"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fastdeploy-tools"
|
name = "fastdeploy-tools"
|
||||||
@ -737,13 +737,13 @@ reference = "mirrors"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pydantic"
|
name = "pydantic"
|
||||||
version = "2.4.1"
|
version = "2.4.2"
|
||||||
description = "Data validation using Python type hints"
|
description = "Data validation using Python type hints"
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.7"
|
python-versions = ">=3.7"
|
||||||
files = [
|
files = [
|
||||||
{file = "pydantic-2.4.1-py3-none-any.whl", hash = "sha256:2b2240c8d54bb8f84b88e061fac1bdfa1761c2859c367f9d3afe0ec2966deddc"},
|
{file = "pydantic-2.4.2-py3-none-any.whl", hash = "sha256:bc3ddf669d234f4220e6e1c4d96b061abe0998185a8d7855c0126782b7abc8c1"},
|
||||||
{file = "pydantic-2.4.1.tar.gz", hash = "sha256:b172505886028e4356868d617d2d1a776d7af1625d1313450fd51bdd19d9d61f"},
|
{file = "pydantic-2.4.2.tar.gz", hash = "sha256:94f336138093a5d7f426aac732dcfe7ab4eb4da243c88f891d65deb4a2556ee7"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
@ -1336,4 +1336,4 @@ reference = "mirrors"
|
|||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.0"
|
lock-version = "2.0"
|
||||||
python-versions = ">=3.9,<3.13"
|
python-versions = ">=3.9,<3.13"
|
||||||
content-hash = "f1ddca71df771166e502ffab7d603041816df3290b39513a7e5bacf7cb343f8a"
|
content-hash = "b1d0db5e3e467c63b7794237b977baadd64738b17490597f63dba35ded184000"
|
||||||
|
@ -7,7 +7,8 @@ readme = "README.md"
|
|||||||
|
|
||||||
[tool.poetry.dependencies]
|
[tool.poetry.dependencies]
|
||||||
python = ">=3.9,<3.13"
|
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" }
|
# fastdeploy-gpu-python = { url = "https://bj.bcebos.com/fastdeploy/release/wheels/fastdeploy_gpu_python-1.0.7-cp310-cp310-manylinux1_x86_64.whl" }
|
||||||
|
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"
|
numpy = "^1.26.0"
|
||||||
opencv-python = "^4.8.0.76"
|
opencv-python = "^4.8.0.76"
|
||||||
python-multipart = "^0.0.6"
|
python-multipart = "^0.0.6"
|
||||||
|
Reference in New Issue
Block a user