nathbns commited on
Commit
e9341b1
·
verified ·
1 Parent(s): 41c1ae6

Upload laps.py

Browse files
Files changed (1) hide show
  1. laps.py +118 -0
laps.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import deps
2
+
3
+ import numpy as np
4
+ import cv2
5
+ import collections
6
+ import scipy
7
+ import scipy.cluster
8
+ import tensorflow as tf
9
+ import os
10
+
11
+ model_h5_path = "data/laps_models/laps.h5"
12
+ try:
13
+ NEURAL_MODEL = tf.keras.models.load_model(model_h5_path, compile=False)
14
+ from tensorflow.keras.optimizers import RMSprop
15
+ NEURAL_MODEL.compile(RMSprop(learning_rate=0.001),
16
+ loss='categorical_crossentropy',
17
+ metrics=['categorical_accuracy'])
18
+ except Exception as e:
19
+ print(f"Warning: Could not load model from {model_h5_path}: {e}")
20
+ from deps.laps import model as NEURAL_MODEL
21
+
22
+
23
+ def laps_intersections(lines):
24
+ __lines = [[(a[0], a[1]), (b[0], b[1])] for a, b in lines]
25
+ return deps.geometry.isect_segments(__lines)
26
+
27
+
28
+ def laps_cluster(points, max_dist=10):
29
+ Y = scipy.spatial.distance.pdist(points)
30
+ Z = scipy.cluster.hierarchy.single(Y)
31
+ T = scipy.cluster.hierarchy.fcluster(Z, max_dist, 'distance')
32
+ clusters = collections.defaultdict(list)
33
+ for i in range(len(T)):
34
+ clusters[T[i]].append(points[i])
35
+ clusters = clusters.values()
36
+ clusters = map(lambda arr: (np.mean(np.array(arr)[:, 0]),
37
+ np.mean(np.array(arr)[:, 1])), clusters)
38
+ return list(clusters)
39
+
40
+
41
+ def laps_detector(img):
42
+ global NC_LAYER
43
+
44
+ hashid = str(hash(img.tostring()))
45
+
46
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
47
+ img = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU)[1]
48
+ img = cv2.Canny(img, 0, 255)
49
+ img = cv2.resize(img, (21, 21), interpolation=cv2.INTER_CUBIC)
50
+
51
+ imgd = img
52
+
53
+ X = [np.where(img > int(255/2), 1, 0).ravel()]
54
+ X = X[0].reshape([-1, 21, 21, 1])
55
+
56
+ img = cv2.dilate(img, None)
57
+ mask = cv2.copyMakeBorder(img, top=1, bottom=1, left=1, right=1,
58
+ borderType=cv2.BORDER_CONSTANT, value=[255, 255, 255])
59
+ mask = cv2.bitwise_not(mask)
60
+ i = 0
61
+ contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL,
62
+ cv2.CHAIN_APPROX_NONE)
63
+
64
+ _c = np.zeros((23, 23, 3), np.uint8)
65
+
66
+ for cnt in contours:
67
+ (x, y), radius = cv2.minEnclosingCircle(cnt)
68
+ x, y = int(x), int(y)
69
+ approx = cv2.approxPolyDP(cnt, 0.1*cv2.arcLength(cnt, True), True)
70
+ if len(approx) == 4 and radius < 14:
71
+ cv2.drawContours(_c, [cnt], 0, (0, 255, 0), 1)
72
+ i += 1
73
+ else:
74
+ cv2.drawContours(_c, [cnt], 0, (0, 0, 255), 1)
75
+
76
+ if i == 4:
77
+ return (True, 1)
78
+
79
+ pred = NEURAL_MODEL.predict(X)
80
+ a, b = pred[0][0], pred[0][1]
81
+ t = a > b and b < 0.03 and a > 0.975
82
+
83
+ if t:
84
+ return (True, pred[0])
85
+ else:
86
+ return (False, pred[0])
87
+
88
+ ################################################################################
89
+
90
+
91
+ def LAPS(img, lines, size=10):
92
+
93
+ __points, points = laps_intersections(lines), []
94
+
95
+ for pt in __points:
96
+ pt = list(map(int, pt))
97
+
98
+ lx1 = max(0, int(pt[0]-size-1))
99
+ lx2 = max(0, int(pt[0]+size))
100
+ ly1 = max(0, int(pt[1]-size))
101
+ ly2 = max(0, int(pt[1]+size+1))
102
+
103
+ dimg = img[ly1:ly2, lx1:lx2]
104
+ dimg_shape = np.shape(dimg)
105
+
106
+ if dimg_shape[0] <= 0 or dimg_shape[1] <= 0:
107
+ continue
108
+
109
+ re_laps = laps_detector(dimg)
110
+ if not re_laps[0]:
111
+ continue
112
+
113
+ if pt[0] < 0 or pt[1] < 0:
114
+ continue
115
+ points += [pt]
116
+ points = laps_cluster(points)
117
+
118
+ return points