forked from qobi/ece57000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_clusterer_gui.py
158 lines (143 loc) · 4.52 KB
/
video_clusterer_gui.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from gui import *
from distances import *
from medoid_classifier import *
import cv2
import numpy as np
videos = []
points = []
labels = []
medoids = []
distance = dtw(bidirectional(chamfer(L2_vector(L2_scalar)), plus))
def process(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
v = np.median(gray)
sigma = 0.33
lower_thresh = int(max(0, (1.0-sigma)*v))
upper_thresh = int(min(255, (1.0+sigma)*v))
edge = cv2.Canny(gray, lower_thresh, upper_thresh)
return edge
def edge_pixels(image):
m, n = image.shape
pixels = []
for i in range(0, m, 10):
for j in range(0, n, 10):
if image[i, j]>0:
pixels.append([i,j])
return pixels
def start_recording():
message("")
global camera
camera = cv2.VideoCapture(0)
global stop, video
stop = False
video = []
def internal():
if not stop:
return_value, image = camera.read()
if show_edges():
edge = process(image)
get_window().show_image(cv2.cvtColor(edge, cv2.COLOR_GRAY2BGR))
else:
get_window().show_image(image)
video.append(image)
get_window().after(10, internal)
internal()
def stop_recording():
global stop
stop = True
camera.release()
return video, [edge_pixels(process(video[i]))
for i in range(0, len(video), 5)]
def clear_command():
global videos, points, labels, medoids
videos = []
points = []
labels = []
medoids = []
message("")
def record_command():
message("")
video, edge_pixel_frames = stop_recording()
videos.append(video)
points.append(edge_pixel_frames)
labels.append(-1)
def random_labels_command():
global labels, medoids
labels = random_labels(points, 2)
medoids = []
message("")
def train_command():
def internal():
global medoids
medoids = train(distance, points, labels)
message("{:.3f}".format(cost(distance, points, labels, medoids)))
if not all_labels(labels, 2):
message("Missing class")
elif not all_labeled(labels):
message("Random labels first")
else:
message("Training")
get_window().after(10, internal)
def reclassify_all_command():
def internal():
global labels
labels = reclassify_all(distance, points, medoids)
message("{:.3f}".format(cost(distance, points, labels, medoids)))
if len(medoids)==0:
message("Train first")
else:
message("Reclassifying all")
get_window().after(10, internal)
def loop_command():
def internal(last_cost):
global labels, medoids
medoids = train(distance, points, labels)
labels = reclassify_all(distance, points, medoids)
this_cost = cost(distance, points, labels, medoids)
message("{:.3f}".format(this_cost))
if this_cost<last_cost:
get_window().after(500, lambda: internal(this_cost))
else:
message("Done")
if not all_labeled(labels):
message("Random labels first")
elif not all_labels(labels, 2):
message("Missing class")
else:
infinity = float("inf")
internal(infinity)
def play(i, j):
if i<len(videos):
if labels[i]==0:
message("Pick Up")
elif labels[i]==1:
message("Put Down")
else:
message("Unlabeled")
if j<len(videos[i]):
if show_edges():
edge = process(videos[i][j])
get_window().show_image(cv2.cvtColor(edge, cv2.COLOR_GRAY2BGR))
else:
get_window().show_image(videos[i][j])
get_window().after(int((1.0/fps)*1000), lambda: play(i, j+1))
else:
get_window().after(1000, lambda: play(i+1, 0))
def play_command():
play(0, 0)
add_button(0, 0, "Clear", clear_command, nothing)
show_edges = add_checkbox(0, 1, "Edges?", nothing)
add_button(0, 2, "Record", start_recording, record_command)
add_button(0, 3, "Random labels", random_labels_command, nothing)
add_button(1, 0, "Train", train_command, nothing)
add_button(1, 1, "Reclassify all", reclassify_all_command, nothing)
add_button(1, 2, "Loop", loop_command, nothing)
add_button(1, 3, "Play", play_command, nothing)
add_button(1, 4, "Exit", done, nothing)
message = add_message(2, 0, 5)
camera = cv2.VideoCapture(0)
width = camera.get(cv2.CAP_PROP_FRAME_WIDTH)
height = camera.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = camera.get(cv2.CAP_PROP_FPS)
camera.release()
start_video(width, height, 3, 5)