Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. # USAGE
  2. # python detect_mrz.py --images examples
  3.  
  4. # import the necessary packages
  5. from imutils import paths
  6. import numpy as np
  7. import argparse
  8. import imutils
  9. import cv2
  10. import requests
  11. from passporteye import read_mrz
  12.  
  13. # construct the argument parse and parse the arguments
  14. url = "http://192.168.43.1:8080/shot.jpg"
  15. img_resp = requests.get(url)
  16. img_arr = np.array(bytearray(img_resp.content), dtype=np.uint8)
  17. imgg = cv2.imdecode(img_arr, -1)
  18.  
  19. ap = argparse.ArgumentParser()
  20. ap.add_argument("-i", "--images", required=False, help="path to images directory")
  21. args = vars(ap.parse_args())
  22.  
  23. # initialize a rectangular and square structuring kernel
  24. rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (13, 5))
  25. sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 21))
  26.  
  27. # loop over the input image paths
  28. # load the image, resize it, and convert it to grayscale
  29. image = imgg
  30. image = imutils.resize(image, height=600)
  31. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  32.  
  33. # smooth the image using a 3x3 Gaussian, then apply the blackhat
  34. # morphological operator to find dark regions on a light background
  35. gray = cv2.GaussianBlur(gray, (3, 3), 0)
  36. blackhat = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, rectKernel)
  37.  
  38. # compute the Scharr gradient of the blackhat image and scale the
  39. # result into the range [0, 255]
  40. gradX = cv2.Sobel(blackhat, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1)
  41. gradX = np.absolute(gradX)
  42. (minVal, maxVal) = (np.min(gradX), np.max(gradX))
  43. gradX = (255 * ((gradX - minVal) / (maxVal - minVal))).astype("uint8")
  44.  
  45. # apply a closing operation using the rectangular kernel to close
  46. # gaps in between letters -- then apply Otsu's thresholding method
  47. gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel)
  48. thresh = cv2.threshold(gradX, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
  49.  
  50. # perform another closing operation, this time using the square
  51. # kernel to close gaps between lines of the MRZ, then perform a
  52. # serieso of erosions to break apart connected components
  53. thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel)
  54. thresh = cv2.erode(thresh, None, iterations=4)
  55.  
  56. # during thresholding, it's possible that border pixels were
  57. # included in the thresholding, so let's set 5% of the left and
  58. # right borders to zero
  59. p = int(image.shape[1] * 0.05)
  60. thresh[:, 0:p] = 0
  61. thresh[:, image.shape[1] - p:] = 0
  62.  
  63. # find contours in the thresholded image and sort them by their
  64. # size
  65. cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
  66. cv2.CHAIN_APPROX_SIMPLE)[-2]
  67. cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
  68.  
  69. # loop over the contours
  70. for c in cnts:
  71. # compute the bounding box of the contour and use the contour to
  72. # compute the aspect ratio and coverage ratio of the bounding box
  73. # width to the width of the image
  74. (x, y, w, h) = cv2.boundingRect(c)
  75. ar = w / float(h)
  76. crWidth = w / float(gray.shape[1])
  77.  
  78. # check to see if the aspect ratio and coverage width are within
  79. # acceptable criteria
  80. if ar > 5 and crWidth > 0.75:
  81. # pad the bounding box since we applied erosions and now need
  82. # to re-grow it
  83. pX = int((x + w) * 0.03)
  84. pY = int((y + h) * 0.03)
  85. (x, y) = (x - pX, y - pY)
  86. (w, h) = (w + (pX * 2), h + (pY * 2))
  87.  
  88. # extract the ROI from the image and draw a bounding box
  89. # surrounding the MRZ
  90. roi = image[y:y + h, x:x + w].copy()
  91. cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
  92. break
  93.  
  94. # show the output images
  95. cv2.imshow("Image", image)
  96. cv2.imshow("ROI", roi)
  97. mrz = read_mrz(roi)
  98. print(mrz)
  99. cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement