jetbot 03 人脸检测之 opencv dnn (cuda 加速)

一 准备工作

首先 jetson nano 下手动编译 opencv 4.5 和 opencv 4.5 - contrib , 编译时打开 cuda 的支持

这里有介绍 :

https://blog.csdn.net/walletiger/article/details/109736705#8.3%20%E7%BC%96%E8%AF%91%E5%AE%89%E8%A3%85%20opencv4.5%C2%A0

 

二 下载 camera 类放到自己的工作目录

https://github.com/walletiger/jetson_nano_py/blob/master/camera.py

 

三  jupyter 环境读取摄像头 通过opencv dnn模型来 实时 detect face 

备注: opencv的 haar cascade 检测器已过时 。。 效果和性能都很差, opencv 4 之后 引入了 dnn 模型可以尝试一下。

           经测试 jetson nano 下 opencv dnn 的性能 在 360p分辨率时能达到 21 fps 左右。算是一般的水平。

           程序里用到的 opencv_face_detector_uint8.pb 等模型文件请在 opencv 代码寻找吧

上代码  (在大牛基础上做的修改)

 

# coding: utf-8
#!/usr/bin/env python

from __future__ import division
import cv2
import time
import sys

import sys
import cv2

# path to store camera.py 
sys.path.append('/workspace/hugo_py')

from camera import JetCamera

cap_w = 640
cap_h = 360
cap_fps = 15

cam = JetCamera(cap_w, cap_h, cap_fps)

print(cam.cap_str)
cam.open()

import cv2
import traitlets
import ipywidgets.widgets as widgets
from IPython.display import display
color_image = widgets.Image(format='jpeg', width=cap_w, height=cap_h)
display(color_image)


def bgr8_to_jpeg(value, quality=75):
    return bytes(cv2.imencode('.jpg', value)[1])


def detectFaceOpenCVDnn(net, frame):
    frameOpencvDnn = frame.copy()
    frameHeight = frameOpencvDnn.shape[0]
    frameWidth = frameOpencvDnn.shape[1]
    '''检测'''
    blob = cv2.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], False, False)

    net.setInput(blob)
    detections = net.forward()

    net.setInput(blob)
    detections = net.forward()
    bboxes = []
    for i in range(detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        if confidence > conf_threshold:
            x1 = int(detections[0, 0, i, 3] * frameWidth)
            y1 = int(detections[0, 0, i, 4] * frameHeight)
            x2 = int(detections[0, 0, i, 5] * frameWidth)
            y2 = int(detections[0, 0, i, 6] * frameHeight)
            bboxes.append([x1, y1, x2, y2])
            cv2.rectangle(frameOpencvDnn, (x1, y1), (x2, y2), (0, 255, 0), int(round(frameHeight / 150)), 8)
    return frameOpencvDnn, bboxes


if __name__ == "__main__":

    # OpenCV DNN supports 2 networks.
    # 1. FP16 version of the original caffe implementation ( 5.4 MB )
    # 2. 8 bit Quantized version using Tensorflow ( 2.7 MB )
    '''模型加载'''
    DNN = "TF"
    if DNN == "CAFFE":
        modelFile = "./model/res10_300x300_ssd_iter_140000_fp16.caffemodel"
        configFile = "./model/deploy.prototxt"
        net = cv2.dnn.readNetFromCaffe(configFile, modelFile)
    else:
        modelFile = "./model/opencv_face_detector_uint8.pb"
        modelFile = "./model/opencv_face_detector_uint8.pb"
        configFile = "./model/opencv_face_detector.pbtxt"
        net = cv2.dnn.readNetFromTensorflow(modelFile, configFile)

    # 打开 OPENCV-DNN CUDA 支持
    net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
    net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)

    conf_threshold = 0.7

    source = 0
    if len(sys.argv) > 1:
        source = sys.argv[1]

    source = 0
    frame_count = 0
    tt_opencvDnn = 0
    while (1):
        hasFrame, frame = cam.read()
        if not hasFrame:
            break
        frame_count += 1

        t = time.time()
        outOpencvDnn, bboxes = detectFaceOpenCVDnn(net, frame)
        tt_opencvDnn += time.time() - t
        fpsOpencvDnn = frame_count / tt_opencvDnn
        label = "OpenCV DNN ; FPS : {:.2f}".format(fpsOpencvDnn)
        cv2.putText(outOpencvDnn, label, (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.4, (0, 0, 255), 3, cv2.LINE_AA)
        color_image.value = bgr8_to_jpeg(outOpencvDnn)

        #cv2.imshow("Face Detection Comparison", outOpencvDnn)

        #vid_writer.write(outOpencvDnn)
        if frame_count == 1:
            tt_opencvDnn = 0

        k = cv2.waitKey(10)
        if k == 27:
            break

    cam.close()
    cv2.destroyAllWindows()



                                                          

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

walletiger

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值