|
| 1 | +from time import sleep |
| 2 | +import docscanner |
| 3 | +import numpy as np |
| 4 | +import cv2 |
| 5 | +import time |
| 6 | + |
| 7 | +# set license |
| 8 | +docscanner.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==") |
| 9 | + |
| 10 | +scanner = docscanner.createInstance() |
| 11 | + |
| 12 | +ret = scanner.setParameters(docscanner.Templates.color) |
| 13 | +print(ret) |
| 14 | + |
| 15 | +def showNormalizedImage(name, normalized_image): |
| 16 | + mat = docscanner.convertNormalizedImage2Mat(normalized_image) |
| 17 | + cv2.imshow(name, mat) |
| 18 | + return mat |
| 19 | + |
| 20 | +# detectFile() |
| 21 | +def test_detectFile(): |
| 22 | + print('') |
| 23 | + print('Test detectFile()') |
| 24 | + results = scanner.detectFile("images/1.png") |
| 25 | + assert len(results) > 0 |
| 26 | + image = cv2.imread("images/1.png") |
| 27 | + for result in results: |
| 28 | + x1 = result.x1 |
| 29 | + y1 = result.y1 |
| 30 | + x2 = result.x2 |
| 31 | + y2 = result.y2 |
| 32 | + x3 = result.x3 |
| 33 | + y3 = result.y3 |
| 34 | + x4 = result.x4 |
| 35 | + y4 = result.y4 |
| 36 | + |
| 37 | + print(x1, y1, x2, y2, x3, y3, x4, y4) |
| 38 | + |
| 39 | + normalized_image = scanner.normalizeFile("images/1.png", x1, y1, x2, y2, x3, y3, x4, y4) |
| 40 | + normalized_image.recycle() |
| 41 | + |
| 42 | + # assert len(results) > 0 |
| 43 | + # showNormalizedImage("Normalized Image", normalized_image) |
| 44 | + # cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2) |
| 45 | + |
| 46 | + # cv2.imshow('Document Image', image) |
| 47 | + # cv2.waitKey(0) |
| 48 | + |
| 49 | + |
| 50 | +# detectMat() |
| 51 | +def test_detectMat(): |
| 52 | + print('') |
| 53 | + print('Test detectMat()') |
| 54 | + |
| 55 | + image = cv2.imread("images/1.png") |
| 56 | + results = scanner.detectMat(image) |
| 57 | + assert len(results) > 0 |
| 58 | + for result in results: |
| 59 | + x1 = result.x1 |
| 60 | + y1 = result.y1 |
| 61 | + x2 = result.x2 |
| 62 | + y2 = result.y2 |
| 63 | + x3 = result.x3 |
| 64 | + y3 = result.y3 |
| 65 | + x4 = result.x4 |
| 66 | + y4 = result.y4 |
| 67 | + |
| 68 | + normalized_image = scanner.normalizeBuffer(image, x1, y1, x2, y2, x3, y3, x4, y4) |
| 69 | + # showNormalizedImage("Normalized Image", normalized_image) |
| 70 | + normalized_image.recycle() |
| 71 | + cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2) |
| 72 | + |
| 73 | + # cv2.imshow('Document Image', image) |
| 74 | + # cv2.waitKey(0) |
| 75 | + |
| 76 | +# detectMatAsync() |
| 77 | +def test_detectMatAsync(): |
| 78 | + print('') |
| 79 | + print('Test detectMatAsync()') |
| 80 | + def callback(results): |
| 81 | + assert len(results) > 0 |
| 82 | + for result in results: |
| 83 | + x1 = result.x1 |
| 84 | + y1 = result.y1 |
| 85 | + x2 = result.x2 |
| 86 | + y2 = result.y2 |
| 87 | + x3 = result.x3 |
| 88 | + y3 = result.y3 |
| 89 | + x4 = result.x4 |
| 90 | + y4 = result.y4 |
| 91 | + |
| 92 | + cv2.drawContours(image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2) |
| 93 | + |
| 94 | + # cv2.imshow('Document Image', image) |
| 95 | + # cv2.waitKey(0) |
| 96 | + |
| 97 | + import cv2 |
| 98 | + image = cv2.imread("images/1.png") |
| 99 | + scanner.addAsyncListener(callback) |
| 100 | + scanner.detectMatAsync(image) |
| 101 | + sleep(3) |
| 102 | + |
| 103 | + |
| 104 | + |
0 commit comments