qrcode.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import cv2
  2. import sys
  3. print(sys.argv[0])
  4. print('A demo program of WeChat QRCode Detector:')
  5. camIdx = -1
  6. if len(sys.argv) > 1:
  7. if sys.argv[1] == "-camera":
  8. camIdx = int(sys.argv[2]) if len(sys.argv)>2 else 0
  9. img = cv2.imread(sys.argv[1])
  10. else:
  11. print(" Usage: " + sys.argv[0] + " <input_image>")
  12. exit(0)
  13. # For python API generator, it follows the template: {module_name}_{class_name},
  14. # so it is a little weird.
  15. # The model is downloaded to ${CMAKE_BINARY_DIR}/downloads/wechat_qrcode if cmake runs without warnings,
  16. # otherwise you can download them from https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode.
  17. try:
  18. detector = cv2.wechat_qrcode_WeChatQRCode(
  19. "detect.prototxt", "detect.caffemodel", "sr.prototxt", "sr.caffemodel")
  20. except:
  21. print("---------------------------------------------------------------")
  22. print("Failed to initialize WeChatQRCode.")
  23. print("Please, download 'detector.*' and 'sr.*' from")
  24. print("https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode")
  25. print("and put them into the current directory.")
  26. print("---------------------------------------------------------------")
  27. exit(0)
  28. prevstr = ""
  29. if camIdx < 0:
  30. res, points = detector.detectAndDecode(img)
  31. print(res,points)
  32. else:
  33. cap = cv2.VideoCapture(camIdx)
  34. while True:
  35. res, img = cap.read()
  36. if img.empty():
  37. break
  38. res, points = detector.detectAndDecode(img)
  39. for t in res:
  40. if t != prevstr:
  41. print(t)
  42. if res:
  43. prevstr = res[-1]
  44. cv2.imshow("image", img)
  45. if cv2.waitKey(30) >= 0:
  46. break
  47. # When everything done, release the capture
  48. cap.release()
  49. cv2.destroyAllWindows()