OpenCV - 如何从前面没有'b'的QR码中获取数据?
OpenCV - How to get the data from a QR code without 'b' at the front?
所以我正在使用 OpenCV 通过我的网络摄像头读取 QR 码并且它工作得很好但是数据的输出总是在前面有 'b' 并且它与我正在使用的数据库混淆。
font = cv2.FONT_HERSHEY_PLAIN
cap = cv2.VideoCapture(0)
flag = True
while flag == True:
_, frame = cap.read()
decodedObjects = pyzbar.decode(frame)
for obj in decodedObjects:
cv2.putText(frame, str(obj.data), (50,50), font, 3, (255, 0, 0), 3)
print(obj.data)
cv2.imshow("Frame", frame)
cv2.waitKey(1)
输出将类似于 b'Canon 50D' 或 b'Hello World',我只需要去掉 b。
文本前面的b
表示类型是bytes
不是str
,需要先解码再打印。例如:
bytes_text = b'Hola'
print('Text in bytes:', bytes_text)
>>> Text in bytes: b'Hola'
decoded_bytes = bytes_text.decode('utf-8')
print('Decoded text:', decoded_bytes)
>>> Decoded text: Hola
在你的例子中,如果前面带有 b
的文本是在这一行中生成的,只需像这样解码它:
print(obj.data.decode('utf-8'))
尝试 print(str(obj.data.decode("utf-8")))
而不是 print(obj.data)
另外,在你的 for 循环中,你可以写 cv2.putText(frame, str(obj.data.decode("utf-8")), (50,50), font, 3, (255, 0, 0), 3)
所以我正在使用 OpenCV 通过我的网络摄像头读取 QR 码并且它工作得很好但是数据的输出总是在前面有 'b' 并且它与我正在使用的数据库混淆。
font = cv2.FONT_HERSHEY_PLAIN
cap = cv2.VideoCapture(0)
flag = True
while flag == True:
_, frame = cap.read()
decodedObjects = pyzbar.decode(frame)
for obj in decodedObjects:
cv2.putText(frame, str(obj.data), (50,50), font, 3, (255, 0, 0), 3)
print(obj.data)
cv2.imshow("Frame", frame)
cv2.waitKey(1)
输出将类似于 b'Canon 50D' 或 b'Hello World',我只需要去掉 b。
文本前面的b
表示类型是bytes
不是str
,需要先解码再打印。例如:
bytes_text = b'Hola'
print('Text in bytes:', bytes_text)
>>> Text in bytes: b'Hola'
decoded_bytes = bytes_text.decode('utf-8')
print('Decoded text:', decoded_bytes)
>>> Decoded text: Hola
在你的例子中,如果前面带有 b
的文本是在这一行中生成的,只需像这样解码它:
print(obj.data.decode('utf-8'))
尝试 print(str(obj.data.decode("utf-8")))
而不是 print(obj.data)
另外,在你的 for 循环中,你可以写 cv2.putText(frame, str(obj.data.decode("utf-8")), (50,50), font, 3, (255, 0, 0), 3)