当前位置:   article > 正文

Python使用端口与Arduino联动 (视觉识别)_python opencv arduino

python opencv arduino

我这使用了serial 来让python可以与Arduino联动,通过Opencv的面部识别来打开开关


import cv2
import serial
import time
import gevent #导入库
ser = serial.Serial() #创建serial对象
ser.port = 'COM4' #设置自己arduino的端口号,可以在我的电脑里面的设备管理器里面看到
ser.baudrate = 9600 #频率
ser.open()  #打开serial对象

#opencv的面部识别
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml') 
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_smile.xml')
cap = cv2.VideoCapture(1) #自己的摄像头
def open(): #串口通信
    ser.write(b'a')
    #time.sleep(3)
    gevent.sleep(3)
    ser.write(b'b')


#摄像头的打开和识别的开启·
while(True):   
    ret, frame = cap.read()
    faces = face_cascade.detectMultiScale(frame, 1.3, 2)
    img = frame
    for (x,y,w,h) in faces:

        img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

        face_area = img[y:y+h, x:x+w]
        

        eyes = eye_cascade.detectMultiScale(face_area,1.3,10)
        for (ex,ey,ew,eh) in eyes:

            cv2.rectangle(face_area,(ex,ey),(ex+ew,ey+eh),(0,255,0),1)
        

#When the smile open, The finction OPen() Can work
        smiles = smile_cascade.detectMultiScale(face_area,scaleFactor= 1.16,minNeighbors=65,minSize=(25, 25),flags=cv2.CASCADE_SCALE_IMAGE)
        for (ex,ey,ew,eh) in smiles:
            #print("You Smile!")
            #open()
            gevent.joinall([gevent.spawn(open)])

            cv2.rectangle(face_area,(ex,ey),(ex+ew,ey+eh),(0,0,255),1)
            cv2.putText(img,'Smile',(x,y-7), 3, 1.2, (0, 0, 255), 2, cv2.LINE_AA)

        

    cv2.imshow('frame2',img)

    if cv2.waitKey(5) & 0xFF == ord('q'):
        break


cap.release()
cv2.destroyAllWindows()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/952981
推荐阅读
相关标签
  

闽ICP备14008679号