| 4 | |
| 5 | |
| 6 | def decoder(image): |
| 7 | # Creating a black and white style image to avoid confusion |
| 8 | gray_img = cv2.cvtColor(image, 0) |
| 9 | # Decoding the gray image to some binary image matrix |
| 10 | barcode = decode(gray_img) |
| 11 | |
| 12 | # Algorithm to scan the obtained image |
| 13 | for obj in barcode: |
| 14 | points = obj.polygon |
| 15 | (x, y, w, h) = obj.rect |
| 16 | pts = np.array(points, np.int32) |
| 17 | pts = pts.reshape((-1, 1, 2)) |
| 18 | cv2.polylines(image, [pts], True, (0, 255, 0), 3) |
| 19 | |
| 20 | barcodeData = obj.data.decode("utf-8") |
| 21 | barcodeType = obj.type |
| 22 | string = "Data " + str(barcodeData) + " | Type " + str(barcodeType) |
| 23 | |
| 24 | cv2.putText( |
| 25 | frame, string, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2 |
| 26 | ) |
| 27 | print("Barcode: " + barcodeData + " | Type: " + barcodeType) |
| 28 | |
| 29 | |
| 30 | # Getting user's choice of usage |