Nano上CSI摄像头及人脸检测

Nano上CSI摄像头及人脸检测


上篇博客中我们的JetBot已经刷好系统了。做CV方向的数据分析离不开摄像头数据采集, Nano上自带CSI接口,适合使用树莓派摄像头。

购买树莓派摄像头一定要IMX219 sensor的树莓派摄像头,否则是无法使用的。

在这里插入图片描述

安装树莓派摄像头

首先把Nano的这个CSI接口的这个销子轻轻拔起,记住一定要小心,千万别拔断:

在这里插入图片描述

然后插入树莓派摄像头的排线线缆,一定要注意方向和正反,别插反了,把这个销子固定好,这样就安装好了,检查一下,将摄像头的保护贴膜去掉

打开你的第一个相机图像

看树莓派是否安装好,我们在终端输入 ls /dev/vid*

会显示/dev/video0, 这表明树莓派已经安装好。

输入这个指令:

$ gst-launch-1.0 nvarguscamerasrc ! ‘video/x-raw(memory:NVMM),width=3820, height=2464, framerate=21/1, format=NV12’ ! nvvidconv flip-method=0 ! ‘video/x-raw,width=960, height=616’ ! nvvidconv ! nvegltransform ! nveglglessink -e
将会看到摄像头采集到的数据信息。

测试一个列子:
import cv2

def gstreamer_pipeline (capture_width=1280, capture_height=720, display_width=1280, display_height=720, framerate=60, flip_method=0) :
return ('nvarguscamerasrc ! ’
'video/x-raw(memory:NVMM), ’
'width=(int)%d, height=(int)%d, ’
'format=(string)NV12, framerate=(fraction)%d/1 ! ’
'nvvidconv flip-method=%d ! ’
'video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! ’
'videoconvert ! ’
‘video/x-raw, format=(string)BGR ! appsink’ % (capture_width,capture_height,framerate,flip_method,display_width,display_height))

def show_camera():
# To flip the image, modify the flip_method parameter (0 and 2 are the most common)
print gstreamer_pipeline(flip_method=0)
cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
if cap.isOpened():
window_handle = cv2.namedWindow(‘CSI Camera’, cv2.WINDOW_AUTOSIZE)
# Window
while cv2.getWindowProperty(‘CSI Camera’,0) >= 0:
ret_val, img = cap.read();
cv2.imshow(‘CSI Camera’,img)
# This also acts as
keyCode = cv2.waitKey(30) & 0xff
# Stop the program on the ESC key
if keyCode == 27:
break
cap.release()
cv2.destroyAllWindows()
else:
print ‘Unable to open camera’

if name == ‘main’:
show_camera()

$ python camera.py

camera.py是一个Python脚本,它从摄像机中读取数据,然后使用OpenCV在屏幕上显示一个窗口.

测试一个opencv中人脸识别的例子:

import numpy as np
import cv2

def gstreamer_pipeline (capture_width=3280, capture_height=2464, display_width=820, display_height=616, framerate=21, flip_method=0) :
return ('nvarguscamerasrc ! ’
'video/x-raw(memory:NVMM), ’
'width=(int)%d, height=(int)%d, ’
'format=(string)NV12, framerate=(fraction)%d/1 ! ’
'nvvidconv flip-method=%d ! ’
'video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! ’
'videoconvert ! ’
‘video/x-raw, format=(string)BGR ! appsink’ % (capture_width,capture_height,framerate,flip_method,display_width,display_height))

def face_detect() :
face_cascade = cv2.CascadeClassifier(’/usr/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml’)
eye_cascade = cv2.CascadeClassifier(’/usr/share/OpenCV/haarcascades/haarcascade_eye.xml’)
cap = cv2.VideoCapture(gstreamer_pipeline(), cv2.CAP_GSTREAMER)
if cap.isOpened():
cv2.namedWindow(‘Face Detect’, cv2.WINDOW_AUTOSIZE)
while cv2.getWindowProperty(‘Face Detect’,0) >= 0:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)

        for (x,y,w,h) in faces:
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = img[y:y+h, x:x+w]
            eyes = eye_cascade.detectMultiScale(roi_gray)
            for (ex,ey,ew,eh) in eyes:
                cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

        cv2.imshow('Face Detect',img)
        keyCode = cv2.waitKey(30) & 0xff
        # Stop the program on the ESC key
        if keyCode == 27:
            break

    cap.release()
    cv2.destroyAllWindows()
else:
    print("Unable to open camera")

if name == ‘main’:
face_detect()

$ python face_detect.py
至此 我们可以利用CSI摄像头做CV方向数据分析的例子,下篇博客我们将对比目前NANO和其他设备的性能。

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值