资源简介
利用Haarcascade所建立的脸部特征模型,对OpenCV处理过的帧图像实现脸部检测。之后,根据识别到的区域,来确定所识别到物体的中心点。根据物体的中心点和视频画面的中心点之间的误差,可以确定出转动的方向。最后通过与Arduino互联完成舵机的驱动,并实现摄像头对主体脸部的跟踪。
代码片段和文件信息
import time
import cv2
import serial
import asyncio
import threading
class PID:
def __init__(self kP=0.1 kI=0.0 kD=0.0):
# initialize gains
self.kP = kP
self.kI = kI
self.kD = kD
def initialize(self):
# intialize the current and previous time
# self.currTime = time.time()
# self.prevTime = self.currTime
# initialize the previous error
self.prevError = 0
# initialize the term result variables
self.cP = 0
self.cI = 0
self.cD = 0
def update(self errordeltaTime):
# grab the current time and calculate delta time
self.currTime = time.time()
# delta error
deltaError = error - self.prevError
# proportional term
self.cP = error
# integral term
self.cI += error * deltaTime
# derivative term and prevent divide by zero
self.cD = (deltaError / deltaTime) if deltaTime > 0 else 0
# # save previous time and error for the next update
self.prevError = error
# sum the terms and return
return sum([
self.kP * self.cP
self.kI * self.cI
self.kD * self.cD])
def conter():
if cap.isOpened():
res img = cap.read()
(hw) = img.shape[:2]
return h//2w//2
def inrange(nummin=0max=180):
return (num > min and num < max)
async def face_capture():
global center_x #人脸中心的坐标
global center_y
global deltatime
global Scan
global flag
while cap.isOpened():
pretime = time.time()
res img = cap.read()
gray = cv2.cvtColor(img cv2.COLOR_BGR2GRAY)
Scan = face.detectMultiScale(grayscaleFactor=1.3minNeighbors=3)
# print(type(Scan))
for (x y w h) in Scan:
cv2.rectangle(img (x y) (x + w y + h) (255 0 0) 2) # 依靠对角线来定矩形
center_x = int(x + w/2)
center_y = int(y + h / 2)
cv2.imshow(“CaptureFace“img)
deltatime = time.time()-pretime
# print(deltatime)
await asyncio.sleep(0.01)
key = cv2.waitKey(50) & 0xff
if key == 27: # ESC的ASCALL码是27
flag = 1
break
cap.release()
cv2.destroyAllWindows()
async def caculatePID():
print(“Start“)
global pan_angle
global tilt_angle
p1i1d1 = 0.0200.0550.00031
# p1i1d1 = 000
# p2i2d2 = 000
p2i2d2 = 0.0220.00210.00038
pid_pan = PID(p1 i1 d1)
pid_tilt = PID(p2 i2 d2)
while flag == 0:
pid_pan.initialize()
pid_tilt.initialize()
if isinstance(Scan tuple) == False:
head_pan = pid_pan.update(cx-center_xdeltaTime=deltatime)
head_tilt = pid_tilt.update(cy-center_ydeltaTime=deltatime)
else:
head_pan = head_tilt = 0
if inrange
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2020-05-24 12:45 Face-tracking\
文件 3933 2020-05-22 13:11 Face-tracking\FaceTracking_PID.py
文件 930127 2020-03-16 11:20 Face-tracking\haarcascade_frontalface_default.xm
文件 140975 2020-03-17 17:47 Face-tracking\haarcascade_frontalface_default.zip
文件 694 2020-05-09 21:10 Face-tracking\pan-tilt.ino
- 上一篇:QT项目code.rar
- 下一篇:2018最新git视频教程
相关资源
- [MIT][计算机科学及编程导论][6.00][课程
- 花卉识别程序+数据集
- Dht11温湿度模块库文件
- SciHub神器,基于爬虫原理
- Arduino uno+esp8266+onenet+dht11温湿度传感器
- 机器学习-泰坦尼克号船员获救
- Arduino与LabVIEW互动设计
- 基于Flask的留言板Demo
- tensorflow-2.3.0-cp37-cp37m-win_amd64.whl
- tensorflow麻将智能出牌源码
- 智能小车超声波避障实验+源码(有舵
- Arduino通过ESP8266模块使用HTTP协议连接
- TLC5615 arduino库文件
- arduino开源平衡车源代码
- Arduino 格力空调红外编码库,当时做的
- PyCharm汉化后设置界面打不开完美解决
- Arduino-电子秤加数字显示
- Arduino通过RC522实现开门
- arduino烧录esp8266程序
- arduino通过红外遥控结合蜂鸣器的定时
- arduino mpu6050 dmp库文件
- arduino温湿度采集头文件.rar
- Arduino 步进电机接线方法含有代码
- QQLogin.ui
- 四位数码管+DS3231做电子时钟源码.in
- django试题发布和测验
- arduino控制57步进电机
- PIL中文手册
- 使用Processing+Arduino写的类似雷达扫描
- arduino上阿里云所需要用到的四个库文
评论
共有 条评论