qrCodeOpencv.py 1020 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import cv2
  2. import numpy as np
  3. import sys
  4. import time
  5. if len(sys.argv)>1:
  6. inputImage = cv2.imread(sys.argv[1])
  7. else:
  8. inputImage = cv2.imread("qrcode-learnopencv.jpg")
  9. # Display barcode and QR code location
  10. def display(im, bbox):
  11. n = len(bbox)
  12. for j in range(n):
  13. cv2.line(im, tuple(bbox[j][0]), tuple(bbox[ (j+1) % n][0]), (255,0,0), 3)
  14. # Display results
  15. cv2.imshow("Results", im)
  16. # Create a qrCodeDetector Object
  17. qrDecoder = cv2.QRCodeDetector()
  18. # Detect and decode the qrcode
  19. t = time.time()
  20. data,bbox,rectifiedImage = qrDecoder.detectAndDecode(inputImage)
  21. print("Time Taken for Detect and Decode : {:.3f} seconds".format(time.time() - t))
  22. if len(data)>0:
  23. print("Decoded Data : {}".format(data))
  24. display(inputImage, bbox)
  25. rectifiedImage = np.uint8(rectifiedImage);
  26. cv2.imshow("Rectified QRCode", rectifiedImage);
  27. else:
  28. print("QR Code not detected")
  29. cv2.imshow("Results", inputImage)
  30. cv2.imwrite("output.jpg",inputImage)
  31. cv2.waitKey(0)
  32. cv2.destroyAllWindows()