-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
97 lines (79 loc) · 2.68 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import warnings
warnings.filterwarnings('ignore')
import cv2
import numpy as np
from filter import filter_one
from filter import filter_two
from angle import segment_by_angle_kmeans
from intersection import segmented_intersections
from boxes import rect
#from boxesrc import rect
from contours import max_area
#import tensorflow as tf
import sudoku
import os
import sys
#img = cv2.imread('sudoku.jpeg')
debug = True
def main():
cap = cv2.VideoCapture(0)
down = False
filter = True
while True:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 90, 150, apertureSize=3)
kernel = np.ones((3, 3), np.uint8)
edges = cv2.dilate(edges, kernel, iterations=1)
kernel = np.ones((5, 5), np.uint8)
edges = cv2.erode(edges, kernel, iterations=1)
lines = cv2.HoughLines(edges, 1, np.pi/180, 150)
if lines is None:
continue
if filter:
line_flags = filter_one(lines)
#print('number of Hough lines:', len(lines))
filtered_lines = []
if filter:
filtered_lines = filter_two(lines,line_flags)
#print('Number of filtered lines:', len(filtered_lines))
else:
filtered_lines = lines
for line in filtered_lines:
rho, theta = line[0]
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
if debug:
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
segmented = segment_by_angle_kmeans(filtered_lines)
intersections = segmented_intersections(segmented) # points
# Try except may be used here. I removed from here.
if len(intersections) == 100 and len(filtered_lines) == 20:
intersections = [val[0] for val in intersections]
intersections = sorted(intersections,key=lambda val: val[0])
img, prediction, done = rect(img, intersections)
if done:
val = sudoku.SolveSudoku(prediction)
if val:
print(val)
sys.exit('Exiting')
else:
print('Bad Detection')
# Upto here
cv2.imshow('image', img)
if (cv2.waitKey(10) & 0xFF == ord('q')) or down == True:
break
cv2.destroyAllWindows()
cap.release()
if sudoku.SolveSudoku(prediction):
print(sudoku.SolveSudoku(prediction))
else:
print('bad luck')
if __name__ == '__main__':
main()