【V1.0】基于树莓派的OpenCV-Python摄像头人脸追踪系统

【V1.0】基于树莓派OpenCV的摄像头人脸追踪系统

该系统目前结合了树莓派+51单片机
树莓派主要用于运行Python程序 追踪人脸 同时用GPIO口给出信号
单片机用于控制42步进电机导轨左右移动

资源:
https://download.csdn.net/download/weixin_53403301/80270053

最新:
https://blog.csdn.net/weixin_53403301/article/details/123252275

视频:
【V1.0】基于树莓派的OpenCV-Python摄像头人脸追踪系统-哔哩哔哩 https://b23.tv/gk7YVaO
【V1.0】基于树莓派的OpenCV-Python摄像头人脸追踪系统

先前的文章:
人脸追踪部分:
https://blog.csdn.net/weixin_53403301/article/details/120497427
单片机控制42步进电机导轨部分:
https://blog.csdn.net/weixin_53403301/article/details/122658780

树莓派Python代码如下:

import cv2
import threading
import RPi.GPIO as GPIO
# import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.output(23, GPIO.HIGH)
GPIO.output(24, GPIO.HIGH)

def LEFT():
    GPIO.output(23, GPIO.LOW)
    GPIO.output(24, GPIO.HIGH)
    
def RIGHT():
    GPIO.output(23, GPIO.HIGH)
    GPIO.output(24, GPIO.LOW)
    
def STOP():
    GPIO.output(23, GPIO.HIGH)
    GPIO.output(24, GPIO.HIGH)

cap = cv2.VideoCapture(0)  # 开启摄像头
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
            # 框选出人脸   最后一个参数2是框线宽度
            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")
                LEFT()
#                 time.sleep(0.1)
            elif x+w/2 < width/2-50:
                print("Right")
                RIGHT()
#                 time.sleep(0.1)
            elif width/2-50 < x+w/2 < width/2+50:
                print("Central")
                STOP()
# 循环读取图像
while True:
    ok, faceImg = cap.read()  # 读取摄像头图像
    if ok is False:
        print('无法读取到摄像头!')
        break
    
   
#    faceImg = img
    high=faceImg.shape[0]
    width=faceImg.shape[1]
#    print(width,high)
    gray = cv2.cvtColor(faceImg,cv2.COLOR_BGR2GRAY)
    
    cv2.imshow("faceImg",faceImg)
    # 展示图像
    
    thread = threading.Thread(target=track)
    thread.start()

    if cv2.waitKey(10) == 27:   # 通过esc键退出摄像
        break

# 关闭摄像头
cap.release()
cv2.destroyAllWindows()



纯Python代码如下:

import cv2
import threading
#import RPi.GPIO as GPIO
# import time

#GPIO.setwarnings(False)
#GPIO.setmode(GPIO.BCM)
#GPIO.setup(23, GPIO.OUT)
#GPIO.setup(24, GPIO.OUT)
#GPIO.output(23, GPIO.HIGH)
#GPIO.output(24, GPIO.HIGH)
#
#def LEFT():
#    GPIO.output(23, GPIO.LOW)
#    GPIO.output(24, GPIO.HIGH)
#    
#def RIGHT():
#    GPIO.output(23, GPIO.HIGH)
#    GPIO.output(24, GPIO.LOW)
#    
#def STOP():
#    GPIO.output(23, GPIO.HIGH)
#    GPIO.output(24, GPIO.HIGH)

cap = cv2.VideoCapture(0)  # 开启摄像头
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
            # 框选出人脸   最后一个参数2是框线宽度
            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")
#                LEFT()
#                 time.sleep(0.1)
            elif x+w/2 < width/2-50:
                print("Right")
#                RIGHT()
#                 time.sleep(0.1)
            elif width/2-50 < x+w/2 < width/2+50:
                print("Central")
#                STOP()
# 循环读取图像
while True:
    ok, faceImg = cap.read()  # 读取摄像头图像
    if ok is False:
        print('无法读取到摄像头!')
        break
    
   
#    faceImg = img
    high=faceImg.shape[0]
    width=faceImg.shape[1]
#    print(width,high)
    gray = cv2.cvtColor(faceImg,cv2.COLOR_BGR2GRAY)
    
    cv2.imshow("faceImg",faceImg)
    # 展示图像
    
    thread = threading.Thread(target=track)
    thread.start()

    if cv2.waitKey(10) == 27:   # 通过esc键退出摄像
        break

# 关闭摄像头
cap.release()
cv2.destroyAllWindows()



版权声明:本文为CSDN博主「网易独家音乐人Mike Zhou」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_53403301/article/details/122898050

生成海报
点赞 0

网易独家音乐人Mike Zhou

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

电脑不识别STM32的USB虚拟串口

电脑不识别STM32的USB虚拟串口 现象 板子和电脑联调的时候发现,USB线插入板子以后电脑不识别虚拟串口,通过禁用设备再启用,可以正常工作。也可以按一下复位键才能识别。 以前似乎没有这个问题&#