一 准备工作

首先 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()



                                                          

 

 

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐