-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.py
More file actions
62 lines (58 loc) · 2.48 KB
/
Copy pathscripts.py
File metadata and controls
62 lines (58 loc) · 2.48 KB
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
import psycopg2
from sklearn.cluster import KMeans
import cv2
def main():
db = "classroom_tables"
conn = psycopg2.connect("dbname=%s user=postgres password=Bluesky7331" % db)
cur = conn.cursor()
query = "select positions, view_id from positions_of_packed_clusters order by(view_id)"
cur.execute(query)
cluster_locations = cur.fetchall()
num_clusters = 2
for i in range(0, len(cluster_locations) - 1):
points = cluster_locations[i][0].split(';')
if len(points) > 5:
rectangle_corners = []
rectangle_midpoints = []
points.remove('')
image = cv2.imread("C:\\Users\\AS58980\\Desktop\\v" + str(cluster_locations[i][1]) + ".png")
ii = 0
while ii < len(points):
y1 = int(points[ii])
x1 = int(points[ii + 1])
y2 = int(points[ii + 2])
x2 = int(points[ii + 3])
# constraint on the area of the rectangular
if (abs((x1 - x2) * (y1 - y2)) < 7000):
rectangle_corners.append([(x1, x2), (y1, y2)])
ii = ii + 4
for item in rectangle_corners:
rectangle_midpoints.append(((item[0][0] + item[0][1]) / 2, (item[1][0] + item[1][1]) / 2))
kmeans = KMeans(n_clusters=num_clusters)
cluster_labels = kmeans.fit_predict(rectangle_midpoints)
kkk = 0
for tag in (cluster_labels):
rectangle_corners[kkk].append(tag)
kkk += 1
confiners = []
for j in range(0, num_clusters):
same_cluster_x = []
same_cluster_y = []
for eleman in rectangle_corners:
if eleman[2] == j:
same_cluster_x.append(eleman[0][0])
same_cluster_x.append(eleman[0][1])
same_cluster_y.append(eleman[1][0])
same_cluster_y.append(eleman[1][1])
x1 = min(same_cluster_x)
x2 = max(same_cluster_x)
y1 = min(same_cluster_y)
y2 = max(same_cluster_y)
confiners.append(str(x1)+','+str(y1)+','+str(x2)+','+str(y2))
query = "insert into confiners values(" + "'" + str(confiners) + "'" + ", " + str(cluster_locations[i][1]) + ")"
print('\n')
print(query)
print('\n')
cur.execute(query)
conn.commit()
main()