0

I found a Stackoverflow answer for detecting barcode in an image. I am trying to apply the method in the Stackoverflow answer to realtime video capture because my current solution only detect barcodes on clean large surface. How can I apply the method to video capture Here is my code.

import cv2
import numpy as np
from pyzbar.pyzbar import decode

cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)


while True:

    success, img = cap.read()
    for barcode in decode(img):
        myData = barcode.data.decode('utf-8')
        print(myData)

        if myData in myDataList:
            myOutput = 'Authorized'
            myColor = (0,255,0)
        else:
            myOutput = 'Un-Authorized'
            myColor = (0, 0, 255)

        pts = np.array([barcode.polygon],np.int32)
        pts = pts.reshape((-1,1,2))
        cv2.polylines(img,[pts],True,myColor,5)
        pts2 = barcode.rect
        cv2.putText(img,myData,(pts2[0],pts2[1]),cv2.FONT_HERSHEY_SIMPLEX,
                    0.9,myColor,2)

    cv2.imshow('Result',img)
    cv2.waitKey(1)
yushulx
  • 11,695
  • 8
  • 37
  • 64
e.iluf
  • 1,389
  • 5
  • 27
  • 69
  • your code's formatting is broken. it won't run like this. what specific issue do you have? you seem to have video capture working. – Christoph Rackwitz Nov 30 '20 at 17:46
  • 1
    to run in real time, you put all of your code into a loop. The easiest is an infinite loop (`while true:`). But you'll have to replace cv2.imshow and cv2.waitKey with something else to display result. You might be able to use matplotlib. You won't get a high frame rate. Depending on the operations you do and the speed of your hardware, you might get 5 or 10 frames per second. – bfris Dec 01 '20 at 18:12

0 Answers0