` 本帖最后由 速度之上 于 2017-10-31 22:27 编辑
这篇帖子是接着前面的几个帖子的
emmm...如
想看开箱的请点击:【盈鹏飞EVB-T335开发板试用体验】NO.1开箱及其资源分析
连接网络请点击 :【盈鹏飞EVB-T335开发板试用体验】NO.2 eth艰难的联网过程
舵机云台图纸请点击:【盈鹏飞EVB-T335开发板试用体验】NO.3 舵机云台3D打印
摄像头调试请点击:【盈鹏飞EVB-T335开发板试用体验】NO.4 摄像头调试
opencv安装请点击:【盈鹏飞EVB-T335开发板试用体验】NO.5 opencv的编译与安装
emmm.....
网上大部分都是用c写的
这里我用python写,感觉在AI这方面
py还是好用些,并且更简洁和简单
先调用opencv库创建一个视频流
后续的代码都是在此基础上进行的添加与修改
- import cv2
- if __name__ == '__main__'
- video_name = 'BBB_Video'#emmm....创建一个主窗口
- cv2.nameWindow(video_name,cv2.WINDOW_NORMAL)
- video_cap = cv2.VideoCapture(-1)
- #emmm.....这里定义一个异常中断
- try:
- while True:
- ret,frame = video_cap.read()
- cv2.imshow(video_name,frame)
- cv2.waitKey(1)
- except KeyboardInterrupt:
- #触发异常的话就清空
- vidoe_cap.release()
- cv2.destroyALLWindows()
复制代码
运行起来结果就是图像一帧一帧的传送到窗口中来
(连接显示屏的话可以直接看到图像
(没显示屏的话,上vnc吧,这里不介绍了,很简单的
如果要停止的话,ctrl+c即可
emmm...随后,在此视频流的基础上
利用opencv的层叠分类器进行人脸识别
- import cv2
- if __name__ == '__main__'
- video_name = 'BBB_Video'#emmm....创建一个主窗口
- cv2.nameWindow(video_name,cv2.WINDOW_NORMAL)
- cascade = '/root/opencv/data/bbbfaceindentify/bbbface.xml'
- face_cascade = cv2.CascadeClassifier(cascade)
- video_cap = cv2.VideoCapture(-1)
- #emmm.....这里定义一个异常中断
- try:
- while True:
- ret,frame = video_cap.read()
- reduced = cv2.resize(frame,(0,0),fx=0.5,fy=0.5)
- reduced_grey = cv2.cas.cvtColor(reduced,cv2.COLOR_R2GRAY)
- faces = face_cascade.detectMultiscale(reduced_grey,1.3,2)
- for (x,y,w,h) in faces:
- origin = (x*2,y*2)
- size = (w*2,h*2)
- far = (origin[0] + size[0], origin[1] + size[1])
- cv2.rectangle(frame, origin ,far, (0.0.255), 2 )
- cv2.imshow(video_name,frame)
- cv2.waitKey(1)
- except KeyboardInterrupt:
- #触发异常的话就清空
- vidoe_cap.release()
- cv2.destroyALLWindows()
复制代码
人脸识别的工作都在创建的CascadeClassifier对象中实现了
分类器在图像捕获之后
返回一组矩形的参数,包括点的位置和矩形的大小
从而在原始图像中将人脸的位置标志出来
最后把舵机模块添加进来
实现脸部视觉追踪功能
- import Adafruit_BBIO.GPIO as GPIO
- import bbbservo
- import cv2
- import time
- from datetime import datetime
- BUTTON_PIN = 'P9_11'
- PAN_PIN = 'P9_14'
- TILT_PIN = 'P8_13'
- FOV = 75
- if __name__ == '__main__'
- try:
- video_name = 'BBB_Video'#emmm....创建一个主窗口
- cv2.nameWindow(video_name,cv2.WINDOW_NORMAL)
- GPIO.setup(BUTTON_PIN,GPIO.IN)
- GPIO.add_event_detect(BUTTON_PIN,GPIO.RISING)
- cascade = '/root/opencv/data/bbbfaceindentify/bbbface.xml'
- face_cascade = cv2.CascadeClassifier(cascade)
- video_cap = cv2.VideoCapture(-1)
- cam_frame_size = (video_cap.get(cv2.CAP_PROP_FRAME_WIDTH),video_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
- cam_frame_center = (cam_frame_size([0] / 2, cam_frame_size[1] / 2)
- pos_to_angle = ((FOV / 2) ? cam_frame_center[0],(FOV / 2)cam_frame_center[1])
- pan_servo = bbbservo.Servo('P9_14' , 0 servo_range = (-90,90) , ppm_range = (0.4 , 2.25))
- tilt_servo = bbbservo.Servo('P9_13' , 0 servo_range = (-90,90) , ppm_range = (0.8 , 2.25))
- time.sleep(1)
- last_found = time.time()
- while True:
- ret,frame = video_cap.read()
- reduced = cv2.resize(frame,(0,0),fx=0.5,fy=0.5)
- reduced_grey = cv2.cas.cvtColor(reduced,cv2.COLOR_R2GRAY)
- faces = face_cascade.detectMultiscale(reduced_grey,1.3,2)
- for i , (x,y,w,h) in enumerate(faces):
- origin = (x*2,y*2)
- face_center = (origin[0] + w , origin[1]+h)
- if i == 0
- cv2.circle(frame , face_center , w , (0 , 255 0) , 1)
- else:
- cv2.circle(frame , face_center , w , (0, 0 ,255) ,1)
- center_delta = ( cam_frame_center[0] - face_center[0] , cam_frame_center[1] - face_center[1])
- pan_servo.position = centlta[0] * pos_to_angle[0]
- tilt_servo.position = centlta[1] * pos_to_angle[1]
- last_found= time.time()
- if GPIO.event_detected(BUTTON_PIN):
- snap_time = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
- outputfile = 'snap / snap_{}.jpeg' . format(snap_time)
- jpeg_settings = [int(cv2.IMWRITE_QUALITY) , 90]
- cv2.imwrite(outputfile , frame , jpeg_settings)
- cv2.imshow(video_name,frame)
- cv2.waitKey(1)
- except KeyboardInterrupt:
- #触发异常的话就清空复位
- vidoe_cap.release()
- cv2.destroyALLWindows()
- pan_servo.position = 0
- tilt_servo.position = 0
- time.sleep(1)
- pan_servo.cleanup()
- tilt_servo.cleanup()
复制代码
这样,两轴舵机云台的视觉跟踪就成形了
最后成型的代码
我本来是想写注释来着
后来写完代码太累了
就没写注释2333
大家凑合看吧
`
|