|
14 | 14 | Credit Card Type: Visa
|
15 | 15 | Credit Card #: 4000123456789010
|
16 | 16 |
|
| 17 | +
|
| 18 | +检测图像中信用卡的位置。 |
| 19 | +本地化四位数字,与信用卡上十六位数相关。 |
| 20 | +应用OCR来识别信用卡上的十六位数字。 |
| 21 | +识别信用卡类型(即Visa,万事达卡,美国运通等)。 |
| 22 | +
|
17 | 23 | """
|
18 | 24 |
|
19 | 25 | # import the necessary packages
|
|
23 | 29 | import imutils
|
24 | 30 | import cv2
|
25 | 31 |
|
26 |
| -# construct the argument parser and parse the arguments |
| 32 | +# construct the argument parser and parse the arguments解析命令行参数 |
27 | 33 | ap = argparse.ArgumentParser()
|
28 | 34 | ap.add_argument("-i", "--image", required=True,
|
29 | 35 | help="path to input image")
|
|
49 | 55 | ref = cv2.cvtColor(ref, cv2.COLOR_BGR2GRAY)
|
50 | 56 | ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1]
|
51 | 57 |
|
| 58 | +cv2.imshow('ref',ref) |
| 59 | +cv2.waitKey(0) |
| 60 | + |
| 61 | +''' |
52 | 62 | # find contours in the OCR-A image (i.e,. the outlines of the digits)
|
53 | 63 | # sort them from left to right, and initialize a dictionary to map
|
54 | 64 | # digit name to the ROI
|
55 |
| -refCnts = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL, |
56 |
| - cv2.CHAIN_APPROX_SIMPLE) |
57 |
| -refCnts = refCnts[0] if imutils.is_cv2() else refCnts[1] |
| 65 | +# refCnts = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)#有问题 |
| 66 | +refCnts = cv2.findContours(ref.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
| 67 | +# refCnts = refCnts[0] if imutils.is_cv2() else refCnts[1] |
| 68 | +refCnts = refCnts[1] |
| 69 | +print('len cnt:',len(refCnts)) |
58 | 70 | refCnts = contours.sort_contours(refCnts, method="left-to-right")[0]
|
| 71 | +print('sort_contours len cnt:',len(refCnts)) |
59 | 72 | digits = {}
|
60 | 73 |
|
61 | 74 | # 循环浏览轮廓,提取ROI并将其与相应的数字相关联
|
|
66 | 79 | (x, y, w, h) = cv2.boundingRect(c)
|
67 | 80 | roi = ref[y:y + h, x:x + w]
|
68 | 81 | roi = cv2.resize(roi, (57, 88))
|
| 82 | + cv2.imshow('roi', roi) |
| 83 | + cv2.waitKey(500) |
69 | 84 |
|
70 | 85 | # update the digits dictionary, mapping the digit name to the ROI
|
71 | 86 | digits[i] = roi
|
72 | 87 | # 从参考图像中提取数字,并将其与相应的数字名称相关联
|
| 88 | +print('digits:',digits.keys()) |
| 89 | +''' |
73 | 90 |
|
| 91 | +#try1 |
| 92 | +digits = {} |
| 93 | +rows,cols=ref.shape |
| 94 | +per=int(cols/10) |
| 95 | +for x in range(10): |
| 96 | + roi = ref[:, x*per:(x+1)*per] |
| 97 | + roi = cv2.resize(roi, (57, 88)) |
| 98 | + cv2.imshow('roi', roi) |
| 99 | + cv2.waitKey(500) |
| 100 | + |
| 101 | + # update the digits dictionary, mapping the digit name to the ROI |
| 102 | + digits[x] = roi |
| 103 | +# 从参考图像中提取数字,并将其与相应的数字名称相关联 |
| 104 | +print('digits:',digits.keys()) |
74 | 105 |
|
75 | 106 | # 初始化一对结构化的内核:
|
76 | 107 | # initialize a rectangular (wider than it is tall) and square
|
|
155 | 186 | digitCnts = cv2.findContours(group.copy(), cv2.RETR_EXTERNAL,
|
156 | 187 | cv2.CHAIN_APPROX_SIMPLE)
|
157 | 188 | digitCnts = digitCnts[0] if imutils.is_cv2() else digitCnts[1]
|
158 |
| - digitCnts = contours.sort_contours(digitCnts, |
159 |
| - method="left-to-right")[0] |
| 189 | + # digitCnts = contours.sort_contours(digitCnts,method="left-to-right")[0] |
160 | 190 |
|
161 | 191 | # loop over the digit contours
|
162 | 192 | for c in digitCnts:
|
|
191 | 221 | output.extend(groupOutput)
|
192 | 222 |
|
193 | 223 | # display the output credit card information to the screen
|
194 |
| -print("Credit Card Type: {}".format(FIRST_NUMBER[output[0]])) |
| 224 | +print("Credit Card Type: {}".format(FIRST_NUMBER.get(output[0],'None'))) |
195 | 225 | print("Credit Card #: {}".format("".join(output)))
|
196 | 226 | cv2.imshow("Image", image)
|
197 | 227 | cv2.waitKey(0)
|
0 commit comments