Pyserial说TypeError:不支持Unicode字符串,请编码为字节

发布于 2025-01-25 01:16:13 字数 2795 浏览 5 评论 0原文

我正在使用一个程序通过我的网络摄像头跟踪人们的面孔,并朝着该人的方向移动伺服电机。 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

我卡住了,并在错误上查看了答案,但找不到修复程序。

I am using a program to track peoples faces through my webcam and move servo motors in the direction the person is. Source. But when running the software, and the camera detects a face it crashes and gives this error:

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'

Full Code:

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

Im stuck and have looked over answers on the error but cannot find a fix.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£烟消云散 2025-02-01 01:16:14

您使用pyserial将数据发送到arduino,pyserial投诉:

typeerror:不支持Unicode字符串,请编码到字节

pyserial需要给出字节,而不是(Unicode)字符串。这样做:

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

假设数据是类型strunicode),这将其变成了使用UTF-8的二进制表示。如果您的字符串中没有任何奇怪的字符,也可以使用ascii

You use pyserial to send data to an arduino, and pyserial complains:

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

Pyserial wants to be given bytes, not (unicode) strings. Do it like this:

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

Assuming data is of type str (unicode), this turns it into a binary representation using UTF-8. you could also use ascii if you don't have any weird characters in the string.

生活了然无味 2025-02-01 01:16:14

编辑:我道歉。请参阅打印。更改以下:

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

TO:

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

输出:输出='X328Y323Z'

使用的F-string:

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

to

print(f'output = {data } ')

Edit:我修复了打印

Edit: My apologized. See print. Change this:

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

to:

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

The output : output ='X328Y323Z'

Used F-string:

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

to

print(f'output = {data } ')

Edit: I fix for print

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文