首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError:不支持unicode字符串,请将其编码为字节。

TypeError:不支持unicode字符串,请将其编码为字节。
EN

Stack Overflow用户
提问于 2022-04-29 13:43:59
回答 2查看 218关注 0票数 0

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

代码语言:javascript
运行
复制
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'

完整法典:

代码语言:javascript
运行
复制
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

我被困住了,已经检查了错误的答案,但找不到解决办法。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-29 15:22:21

编辑:我道歉了。见指纹。改变这一点:

代码语言:javascript
运行
复制
data = "X{0:f}Y{1:f}Z".format(xx, yy)

至:

代码语言:javascript
运行
复制
data = "X{0:d}Y{1:d}Z".format( int(xx), int(yy))

输出: output ='X328Y323Z‘

使用的F-字符串:

代码语言:javascript
运行
复制
 print("output = '" + data + "'")

代码语言:javascript
运行
复制
print(f'output = {data } ')

编辑:我修复打印

票数 0
EN

Stack Overflow用户

发布于 2022-04-29 15:51:36

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

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

Pyserial希望得到字节,而不是(unicode)字符串。这样做吧:

代码语言:javascript
运行
复制
arduino.write(data.encode('utf-8'))

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72058943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档