-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid_detector.py
61 lines (48 loc) · 2.47 KB
/
id_detector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
from helpers import *
if __name__ == '__main__':
directory_path = "test-dataset"
# Traverse through the directory
for filename in os.listdir(directory_path):
if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".jpeg"):
print("filename:", filename)
# Read the image using OpenCV
image_path = os.path.join(directory_path, filename)
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Get homography between Gomhoreyet Masr template and ID
M, match_boundingbox = findOrientation(gray)
# If matches found Apply Orientation
if M is not None:
gray, img, M = applyOrientation(gray, img, M)
# blur gray for smoothing the image for the edge detection
gray = blur_img(gray)
# Adaptive threshold
bin = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
# Edge detection
edges = cv2.Canny(bin, 200, 600, apertureSize=3)
# cv2.imshow('edges through Canny', edges)
# cv2.waitKey(0)
# Getting largest Contour
contour_bbox, largest_contour = findLargestContour(edges)
if contour_bbox is not None:
for point in contour_bbox:
cv2.circle(img, point, thickness=5, color=(255, 0, 0), radius=3)
# cv2.imshow("Largest Contour:", img)
# cv2.waitKey(0)
cropped_img = output_img(img, largest_contour)
cropped_gray = cv2.cvtColor(cropped_img, cv2.COLOR_BGR2GRAY)
output_pth = 'output/' + filename
print(output_pth)
selections, bboxes = detect_text_regions(cropped_gray, cropped_img)
for i in selections:
x, y, x2, y2 = bboxes[i]
cv2.imwrite(f'output/{i}_{filename}', cropped_img[y:y2, x:x2])
cv2.rectangle(cropped_img, (x, y), (x2, y2), (0, 255, 0), 2)
cv2.imshow("Final Result:", cropped_img)
cv2.imwrite(output_pth, cropped_img)
cv2.waitKey(0)
else:
print("No contours")
else:
print("No ID Card Found / No contours")