环境搭建
创建python环境
conda create -n py39 python=3.9
1
安装依赖
apt-get install python3-dev
apt install libgl1-mesa-glx -y
1
2
2
vim 修改Makefile文件
修改cuda环境:
修改cudnn环境
修改python include环境为:/root/miniconda3/envs/py39/include/python3.9/
修改python lib环境为:/root/miniconda3/envs/py39/lib/
1
2
3
4
2
3
4
编译
# make clean
make run -j24
1
2
2
常见问题
/usr/bin/ld: cannot find -lnvcuvid: No such file or directory
/usr/bin/ld: cannot find -lnvidia-encode: No such file or directory
1
2
2
下载Video_Codec_SDK
测试
硬件解码,支持视频和rtsp
需要设置ffmpeg环境变量
export LD_LIBRARY_PATH=../ffmpeg/lib/:$LD_LIBRARY_PATH
1
代码如下
import libffhdd as ffhdd
import cv2
def ffhd_reader(video_path="/workspace/weights/139_20221203180000.mp4", device_id=0, return_numpy=False):
demuxer = ffhdd.FFmpegDemuxer(video_path)
decoder = ffhdd.CUVIDDecoder(
bUseDeviceFrame=True,
codec=demuxer.get_video_codec(),
max_cache=-1,
gpu_id=device_id,
output_bgr=True
)
pps, size = demuxer.get_extra_data()
decoder.decode(pps, size, 0)
stream = decoder.get_stream()
n_frames = 0
while True:
pdata, pbytes, time_pts, iskeyframe, ok = demuxer.demux()
nframe_decoded = decoder.decode(pdata, pbytes, time_pts)
for i in range(nframe_decoded):
if return_numpy:
ptr, pts, idx, image = decoder.get_frame(return_numpy=True)
else:
ptr, pts, idx = decoder.get_frame(return_numpy=False)
width = decoder.get_width()
height = decoder.get_height()
bytesize = decoder.get_frame_bytes()
n_frames += 1
if return_numpy:
yield(ptr, width, height, stream, image)
else:
yield (ptr, width, height, stream)
if pbytes <= 0:
break
def cpu_reader(video_path="/workspace/weights/139_20221203180000.mp4"):
cap = cv2.VideoCapture(video_path)
n_frames = 0
while True:
ret, frame = cap.read()
if frame is None:
break
n_frames += 1
yield frame
print(n_frames)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
decord
已测试
python
1