基于OpenCV-Python的摄像头人脸检测追踪控制系统 资源:https://download.csdn.net/download/weixin_53403301/24966761
采用OpenCV-Python进行人脸识别 并计算出相对位置
输出左右中三种信号
可外接单片机设备控制步进电机并实现摄像头自动追踪
程序测试如图: 左 中 右
单片机及步进电机系统仿真图: Python程序如下:
import cv2
import threading
cap = cv2.VideoCapture(0)
while True:
ok, faceImg = cap.read()
if ok is False:
print('无法读取到摄像头!')
break
high=faceImg.shape[0]
width=faceImg.shape[1]
gray = cv2.cvtColor(faceImg,cv2.COLOR_BGR2GRAY)
classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
def track():
faceRects = classifier.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=3,minSize=(32, 32))
if len(faceRects):
for faceRect in faceRects:
x,y,w,h = faceRect
cv2.rectangle(faceImg,(x, y), (x + w, y + h), (0,255,0), 2)
print(x+w/2)
if x+w/2 > width/2+50:
print("Left")
elif x+w/2 < width/2-50:
print("Right")
elif width/2-50 < x+w/2 < width/2+50:
print("Central")
cv2.imshow("faceImg",faceImg)
thread = threading.Thread(target=track)
thread.start()
k = cv2.waitKey(10)
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
单片机C语言程序如下:
#include <reg51.h>
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
unsigned char BJDJ[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};
unsigned char i;
void delay(unsigned int time){
unsigned int j=0;
for(;time>0;time--)
for(j=0;j<125;j++);
}
void zd(void){
for(i=0;i<8;i++){
P2=BJDJ[i];
delay(100);
}
}
void fzd(void){
for(i=8;i>0;i--){
P2=BJDJ[i];
delay(100);
}
}
void zd2(void){
for(i=0;i<8;i++){
P2=BJDJ[i];
delay(10);
}
}
void fzd2(void){
for(i=8;i>0;i--){
P2=BJDJ[i];
delay(10);
}
}
void main(void){
while(!key3){
while(!key1){
if (!key2)zd2();
else zd();
}
while(key1){
if (!key2) fzd2();
else fzd();
}
}
}
|