pyserial says TypeError: unicode strings are not supported, please encode to bytes

pyserial says TypeError: unicode strings are not supported, please encode to bytes

我正在使用一个程序通过我的网络摄像头跟踪人脸,并按照人所在的方向移动伺服电机。 Source。但是当 运行 软件和相机检测到人脸时,它会崩溃并给出此错误:

Connection to arduino...
{72: 327, 379: 634}
X :379
Y :72
x+w :634
y+h :327
506.5
199.5
Center of Rectangle is : (506.5, 199.5)
output = 'X506.500000Y199.500000Z'
Traceback (most recent call last):
  File "C:\Users\Jaxon\Desktop\Face Tracking\servo\face.py", line 51, in <module>
    arduino.write(data)
  File "C:\Users\Jaxon\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\serial\serialwin32.py", line 310, in write
    data = to_bytes(data)
  File "C:\Users\Jaxon\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\serial\serialutil.py", line 65, in to_bytes
    raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq))
TypeError: unicode strings are not supported, please encode to bytes: 'X506.500000Y199.500000Z'

完整代码:

import numpy as np
import serial
import time
import sys
import cv2

arduino = serial.Serial('COM5', 9600)
time.sleep(2)
print("Connection to arduino...")


face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

cap = cv2.VideoCapture(0)

while 1:
    ret, img = cap.read()
    if ret == True:
        cv2.namedWindow("img", cv2.WINDOW_NORMAL)
        cv2.resizeWindow('img', 500, 500)
        cv2.line(img, (500, 250), (0, 250), (0, 255, 0), 1)
        cv2.line(img, (250, 0), (250, 500), (0, 255, 0), 1)
        cv2.circle(img, (250, 250), 5, (255, 255, 255), -1)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3)

        for (x, y, w, h) in faces:
            cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 5)
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = img[y:y+h, x:x+w]

            arr = {y: y+h, x: x+w}
            print(arr)

            print('X :' + str(x))
            print('Y :'+str(y))
            print('x+w :' + str(x+w))
            print('y+h :' + str(y+h))

            xx = int(x+(x+h))/2
            yy = int(y+(y+w))/2

            print(xx)
            print(yy)

            center = (xx, yy)

            print("Center of Rectangle is :", center)
            data = "X{0:f}Y{1:f}Z".format(xx, yy)
            print("output = '" + data + "'")
            arduino.write(data)

        cv2.imshow('img', img)

        k = cv2.waitKey(30) & 0xff
        if k == 27:
            break
    else:
        break

我卡住了,查看了错误的答案,但找不到修复方法。

编辑:我很抱歉。见打印。改变这个:

data = "X{0:f}Y{1:f}Z".format(xx, yy)

至:

data = "X{0:d}Y{1:d}Z".format( int(xx), int(yy))

输出:输出='X328Y323Z'

二手 F-string:

 print("output = '" + data + "'")

print(f'output = {data } ')

编辑:我修复打印

您使用 pyserial 向 arduino 发送数据,pyserial 抱怨:

TypeError: unicode strings are not supported, please encode to bytes

Pyserial 需要字节,而不是 (unicode) 字符串。这样做:

arduino.write(data.encode('utf-8'))

假设 datastr (unicode) 类型,这会将其转换为使用 UTF-8 的二进制表示形式。如果字符串中没有任何奇怪的字符,您也可以使用 ascii