美文网首页
FFmpeg中的object detection和classif

FFmpeg中的object detection和classif

作者: 郭叶军 | 来源:发表于2021-06-15 10:05 被阅读0次

前段时间和Fu Ting同学一起为FFmpeg加了object detection和classification的相关功能,暂时告一段落,这里做个总结,记录一下备忘。

首先,最新结构图镇贴。


file

1. 用Docker演示

用docker的方式,在linux系统(我用的是ubuntu18.04)执行如下命令

$ cd /tmp/
$ git clone https://github.com/guoyejun/ffmpeg_dnn.git/
$ cd ffmpeg_dnn/
$ cd docker/
$ ls
build.sh  Dockerfile  run.sh

# 下面命令会生成一个image
$ ./build.sh

# 下面命令会进入刚刚生成image的container
$ ./run.sh

进入container后,执行如下命令

# 用OpenVINO后端进行face detection和emtion classifcation,
# 最后的转码结果是 faces.mp4
root@63b38426be65:/workspace# ./detect_face_classify_emotion_with_openvino.sh

# 用TensorFlow后端进行object detection,
# 最后的转码结果是objects.mp4
root@63b38426be65:/workspace# ./detect_objects_with_tensorflow.sh

回到host端,用如下命令可以获取container中的mp4文件,只需将命令行中的{container_id}换成你的container id即可,可用docker container ls来查看。

docker cp {container_id}:/workspace/objects.mp4 /tmp/
docker cp {container_id}:/workspace/faces.mp4 /tmp/

2. 几个文件内容

在这里顺带写一下几个文件的内容。

Dockerfile内容如下:

From openvino/ubuntu18_data_dev:2021.3

USER root

RUN apt-get update
RUN apt-get install -y git gcc nasm wget vainfo clinfo vim mediainfo yasm pkg-config make libfontconfig1-dev libx264-dev

WORKDIR /workspace

RUN mkdir tensorflow && cd tensorflow && \
    wget https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-2.4.0.tar.gz && \
    tar zxvf libtensorflow-cpu-linux-x86_64-2.4.0.tar.gz

ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/intel/openvino/inference_engine/lib/intel64
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/intel/openvino/inference_engine/external/tbb/lib
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/intel/openvino/deployment_tools/ngraph/lib
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/workspace/tensorflow/lib

COPY videos/cici.mp4 cici.mp4
COPY models/openvino/2021.1/* ./
COPY models/tensorflow/v2/ssd_mobilenet_v2_coco.pb .

RUN git clone --depth=1 https://github.com/FFmpeg/FFmpeg ffmpeg

RUN cd ffmpeg && \
    mkdir build && cd build && \
    ../configure \
        --enable-gpl --enable-libx264 \
        --enable-libtensorflow \
        --enable-libopenvino \
        --extra-cflags="-I/workspace/tensorflow/include -I/opt/intel/openvino/inference_engine/include/" \
        --extra-ldflags="-L/workspace/tensorflow/lib -L/opt/intel/openvino/inference_engine/lib/intel64" \
        --enable-libfontconfig --enable-libfreetype && \
    make -j$(nproc) && \
    make install

#RUN rm -rf /workspace/ffmpeg

RUN echo 'ffmpeg -i cici.mp4 -vf dnn_detect=dnn_backend=openvino:model=face-detection-adas-0001.xml:input=data:output=detection_out:confidence=0.6:labels=face-detection-adas-0001.label,dnn_classify=dnn_backend=openvino:model=emotions-recognition-retail-0003.xml:input=data:output=prob_emotion:confidence=0.3:labels=emotions-recognition-retail-0003.label:target=face,drawbox=box_source=side_data_detection_bboxes:t=5:color=red,drawtext=text_source=side_data_detection_bboxes:fontcolor=red:fontsize=30 -an -c:v libx264 -y faces.mp4' > detect_face_classify_emotion_with_openvino.sh
RUN echo 'echo Please check the transcoding result faces.mp4' >> detect_face_classify_emotion_with_openvino.sh
RUN echo 'echo You may run \"docker cp {container_id}:/workspace/faces.mp4 /tmp/\" to download it to host machine' >> detect_face_classify_emotion_with_openvino.sh
RUN chmod +x detect_face_classify_emotion_with_openvino.sh

RUN echo 'ffmpeg -i cici.mp4 -vf dnn_detect=dnn_backend=tensorflow:input=image_tensor:output="num_detections&detection_scores&detection_classes&detection_boxes":model=ssd_mobilenet_v2_coco.pb,drawbox=box_source=side_data_detection_bboxes:t=5:color=red,drawtext=text_source=side_data_detection_bboxes:fontcolor=red:fontsize=30 -c:v libx264 -y objects.mp4' > detect_objects_with_tensorflow.sh
RUN echo 'echo Please check the transcoding result objects.mp4' >> detect_objects_with_tensorflow.sh
RUN echo 'echo You may run \"docker cp {container_id}:/workspace/objects.mp4 /tmp/\" to download it to host machine' >> detect_objects_with_tensorflow.sh
RUN chmod +x detect_objects_with_tensorflow.sh

build.sh文件内容如下:

#!/bin/bash
docker build --network=host $(env | grep -E '_(proxy|REPO|VER)=' | sed 's/^/--build-arg /') -t ffmpeg_dnn_ubuntu:18.04 -f Dockerfile ..

run.sh文件内容如下:

#!/bin/bash
docker run -it --rm --device /dev/dri:/dev/dri ffmpeg_dnn_ubuntu:18.04

3. 一些解释

因为OpenVINO的配置有点麻烦,所以Dockerfile是基于OpenVINO dev image而写的,然后下载到tensorflow相关的压缩包解压。

由于OpenVINO和TensorFlow的库文件和头文件不在标准目录下,为了使得FFmpeg的configure命令成功执行,需要先设置好LD_LIBRARY_PATH,而且configure的参数要加上--extra-cflags和--extra-ldflags的选项。在默认情况下,openvino后端和tensorflow后端并不会被enable,所以,还需要加上--enable-libtensorflow和--enable-libopenvino选项。为了最后将检测结果可视化,还需要加上--enable-libfontconfig --enable-libfreetype选项,以启用drawtext相关功能。最后,因为用转码进行演示,所以还加了--enable-gpl --enable-libx264选项,以支持最后的libx264编码。

在运行ffmpeg命令时候,关于filte dnn_detect的参数之前已经介绍过,dnn_classify的参数是类似的,而drawbox和drawtext的参数都可以顾名思义,不再赘述。

以上内容是本人业余时间兴趣之作,限于水平,差错难免,仅代表个人观点,和本人任职公司无关。

本文由博客一文多发平台 OpenWrite 发布!

相关文章

网友评论

      本文标题:FFmpeg中的object detection和classif

      本文链接:https://www.haomeiwen.com/subject/hxxzeltx.html