Pyserial说TypeError:不支持Unicode字符串,请编码为字节
我正在使用一个程序通过我的网络摄像头跟踪人们的面孔,并朝着该人的方向移动伺服电机。 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用
pyserial
将数据发送到arduino,pyserial
投诉:typeerror:不支持Unicode字符串,请编码到字节
pyserial需要给出字节,而不是(Unicode)字符串。这样做:
假设
数据
是类型str
(unicode
),这将其变成了使用UTF-8的二进制表示。如果您的字符串中没有任何奇怪的字符,也可以使用ascii
。You use
pyserial
to send data to an arduino, andpyserial
complains:TypeError: unicode strings are not supported, please encode to bytes
Pyserial wants to be given bytes, not (unicode) strings. Do it like this:
Assuming
data
is of typestr
(unicode
), this turns it into a binary representation using UTF-8. you could also useascii
if you don't have any weird characters in the string.编辑:我道歉。请参阅打印。更改以下:
TO:
输出:输出='X328Y323Z'
使用的F-string:
to
Edit:我修复了打印
Edit: My apologized. See print. Change this:
to:
The output : output ='X328Y323Z'
Used F-string:
to
Edit: I fix for print