0

so I am using OpenCV to read QR codes with my webcam and it is working great but the output for the data always has 'b' at the front and its messing with the database i'm working with.

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)

The output would be something like this b'Canon 50D' or b'Hello World' and I just need to get rid of the b.

2 Answers2

2

The b in front of the text means that the type is bytes not str, you need to decode it before printing it. For example:

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

In your case, if the text with the b in front is generated in this line just decode it like this:

print(obj.data.decode('utf-8'))
marcos
  • 4,473
  • 1
  • 10
  • 24
1

try print(str(obj.data.decode("utf-8"))) instead of print(obj.data)

also, in your for loop, you can write cv2.putText(frame, str(obj.data.decode("utf-8")), (50,50), font, 3, (255, 0, 0), 3)

Wicaledon
  • 710
  • 1
  • 11
  • 26