出现错误:AttributeError:模块“serial”在尝试运行面部跟踪项目时没有属性“Serial” arduino uno

Getting error: AttributeError: module 'serial' has no attribute 'Serial' when trying to run a face tracking project arduino uno

提问人:Codi 提问时间:11/17/2023 最后编辑:Dan MašekCodi 更新时间:11/17/2023 访问量:25

问:

我组装了一个云台模块,并一直用于一个小项目,但想让它与面部跟踪一起工作。我发现了一些看起来不错的旧代码,但是当我运行它时,我收到错误:AttributeError:模块“serial”没有属性“Serial”。下面是一张图片: 如果有任何帮助,错误消息我的代码在下面链接。

import cv2
import numpy as np
import os 
import serial
import time
ard= serial.Serial()
ard.port = "COM9"
ard.baudrate = 9600
ard.open()

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('Trainer/Trainer.yml')
cascadePath = "E:\\Robotique\\projets\\tout terrain\\Open cv\\Library face and eyes\\haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
font = cv2.FONT_HERSHEY_SIMPLEX
#iniciate id counter
id = 0
# names related to ids: example ==> Marcelo: id=1,  etc
names = ['None', 'Haroun', 'Mohamed', 'Rayen', 'Z', 'W'] 
# Initialize and start realtime video capture
cam = cv2.VideoCapture(1)
while True:
    ret, img =cam.read()
    img = cv2.flip(img, -1) # Flip vertically
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    
    faces = faceCascade.detectMultiScale( 
        gray,
        scaleFactor = 1.2,
        minNeighbors = 5,
       minSize = (80, 80),#(int(minW), int(minH)
       )
    #cv2.circle(img,(320,240), 90, (255,0,0), 2)
    for(x,y,w,h) in faces:
        cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)
        id, confidence = recognizer.predict(gray[y:y+h,x:x+w])      
        # If confidence is less them 100 ==> "0" : perfect match 
        if (confidence < 100):
            id = names[id]
            confidence = "  {0}%".format(round(100 - confidence))
       
        cv2.putText(
                    img, 
                    str(id), 
                    (x+5,y-5), 
                    font, 
                    1, 
                    (255,255,255), 
                    2
                   )
        Xpos = x+(w/2)#calculates the X coordinate of the center of the face.
        Ypos = y+(h/2)#calculates the Y coordinate of the center of the face.
        if Xpos >= 380:
            ard.write('L'.encode())#The following code check if the face is on the left, 
            time.sleep(0.01)       # right, top or botton, 
        elif Xpos <= 260:           #with respect to the center of the frame
            ard.write('R'.encode())#if any conditions are true, it send a commant 
            time.sleep(0.01)       #to the arduino throught the serial bus.    
        
#       else:
#           ard.write('S'.encode())
#           #time.sleep(0.01)
        if Ypos > 300:
            ard.write('D'.encode())
            time.sleep(0.01)
        elif Ypos < 180:
            ard.write('U'.encode())
            time.sleep(0.01)
#       else:
#           ard.write('S'.encode())
#           time.sleep(0.01)
        break 
    
    cv2.imshow('camera',img) 
    k = cv2.waitKey(10) & 0xff # Press 'ESC' for exiting video
    if k == 27:
        break
# Do a bit of cleanup
print("\n [INFO] Exiting Program and cleanup stuff")
cam.release()
cv2.destroyAllWindows()

我尝试运行该程序,并希望它将我的相机移动到我的脸上。相反,它给了我一个错误。

蟒蛇 Arduino,

评论

0赞 jasonharper 11/17/2023
这个模块从何而来?我将大胆猜测您做了类似的事情,这为您提供了一个模块,该模块被描述为“用于将 JSON/YAML/XML 序列化/反序列化为 python 类实例的框架,反之亦然”。用于与串行端口通信的模块名为 。serialpip install serialpyserial
0赞 Codi 11/18/2023
我使用 PyCharm,所以我只是将其添加为扩展/包

答: 暂无答案