barcode-QRcodeScanner.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from __future__ import print_function
  2. import pyzbar.pyzbar as pyzbar
  3. import numpy as np
  4. import cv2
  5. def decode(im) :
  6. # Find barcodes and QR codes
  7. decodedObjects = pyzbar.decode(im)
  8. # Print results
  9. for obj in decodedObjects:
  10. print('Type : ', obj.type)
  11. print('Data : ', str(obj.data),'\n')
  12. return decodedObjects
  13. # Display barcode and QR code location
  14. def display(im, decodedObjects):
  15. # Loop over all decoded objects
  16. for decodedObject in decodedObjects:
  17. points = decodedObject.polygon
  18. # If the points do not form a quad, find convex hull
  19. if len(points) > 4 :
  20. hull = cv2.convexHull(np.array([point for point in points], dtype=np.float32))
  21. hull = list(map(tuple, np.squeeze(hull)))
  22. else :
  23. hull = points;
  24. # Number of points in the convex hull
  25. n = len(hull)
  26. # Draw the convext hull
  27. for j in range(0,n):
  28. cv2.line(im, hull[j], hull[ (j+1) % n], (255,0,0), 3)
  29. # Display results
  30. cv2.imshow("Results", im);
  31. cv2.waitKey(0);
  32. # Main
  33. if __name__ == '__main__':
  34. # Read image
  35. im = cv2.imread('zbar-test.jpg')
  36. decodedObjects = decode(im)
  37. display(im, decodedObjects)