jetbot05 人脸检测之 实时 yoloface

这里用到了 darknet 框架

模型使用了 

https://github.com/dog-qiuqiu/MobileNet-Yolo

里的 yoloface_500k 

下载 mobileNet-Yolo make 编译在 jetpack 4.4.1 下过不了, 参考这里:

https://forums.developer.nvidia.com/t/jetpack-4-4-l4t-r32-4-3-issue-with-darknet-yolo/141191

 

主要介绍  python下 darknet 模型的调用流程, 完成一个 在  jetson nano 下调取 摄像头实时检测人脸的实例。

本例子的github 地址:

https://github.com/walletiger/yoloface_500k_export_jetson_nano

可以看到 DarkNet python 调用还是很方便的, 读懂 DarkNetWrap 包装 即可。

实测 jetson nano 下摄像头采集 640x360 可以 30fps 以上。数据 预处理可以优化。

 

 

#!/usr/bin/python3
# -*- coding:utf-8 -*-
import sys

sys.path.append('/workspace/hugo_py')

import cv2
import numpy as np
import time
from ctypes import *
from darknet import  detect_image
from darknet import  load_net_custom
from darknet import  load_meta
from darknet import  IMAGE
from darknet import network_width, network_height
from camera import JetCamera
import traceback 

cap_w = 640
cap_h = 360
cap_fps = 10


class DarkNetWrap(object):
    def __init__(self, config_path='', weight_path='', meta_path=''):
        self.net = load_net_custom(config_path.encode('utf-8'), weight_path.encode('utf-8'), 0, 1)
        self.meta = load_meta(meta_path.encode('utf-8'))
        self.thresh = .5
        self.hier_thresh = .5
        self.nms = .45

    def detect(self, img):
        image_list = [img]

        pred_height, pred_width, c = image_list[0].shape
        net_width, net_height = (network_width(self.net), network_height(self.net))
        img_list = []

        for custom_image_bgr in image_list:
            custom_image = cv2.cvtColor(custom_image_bgr, cv2.COLOR_BGR2RGB)
            custom_image = cv2.resize(
            custom_image, (net_width, net_height), interpolation=cv2.INTER_NEAREST)
            custom_image = custom_image.transpose(2, 0, 1)
            img_list.append(custom_image)

        arr = np.concatenate(img_list, axis=0)
        arr = np.ascontiguousarray(arr.flat, dtype=np.float32) / 255.0
        data = arr.ctypes.data_as(POINTER(c_float))
        im = IMAGE(net_width, net_height, c, data)


        ret_lst = detect_image(self.net, self.meta, im, self.thresh, self.hier_thresh, self.nms)
        ret_out_lst = []

        if ret_lst:
            for ret in ret_lst:
                x, y, w, h =  ret[2]
                x = x * pred_width / net_width
                y = y * pred_height / net_height 
                w = w * pred_width / net_width 
                h = h * pred_height / net_height 

                ret = (ret[0], ret[1], 
                    (int(x - w / 2) , int(y - h / 2),
                     int(x + w / 2) , int(y + h / 2)))

                ret_out_lst.append(ret)
                 
        return ret_out_lst


def main():
    cam = JetCamera(cap_w, cap_h, cap_fps)
    fd = DarkNetWrap(config_path='yoloface-500k-v2.cfg',
                      weight_path='yoloface-500k-v2.weights',
                      meta_path='face.data'
                                          )
    if not cam:
        return
    print(cam.cap_str)
    cam.open()

    cnt = 0
    while True:
        try:
            ret, frame = cam.read()
            #print("camera read one frame ")
            if not ret:
                break

            t0 = time.time()
            res = fd.detect(frame)
            t1 = time.time()

            cnt += 1

            if cnt % 100 == 0:
                print("frame cnt [%d] yoloface detect delay = %.1fms" % (cnt, (t1 - t0) * 1000))

            for ret in res:
                r = ret[2]
                #print("ret = %s, %s" % (ret, r))
                cv2.rectangle(frame, (int(r[0]), int(r[1])), (int(r[2]), int(r[3])), (255, 255, 0))

            cv2.imshow('haha', frame)
            cv2.waitKey(1)
        except:
            traceback.print_exc()
            break 

    cam.close()


if __name__ == '__main__':
    main()

 

  • 1
    点赞
  • 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、付费专栏及课程。

余额充值