File size: 13,930 Bytes
7a4b92f
 
5ab0373
592e4db
 
7a4b92f
128e4f0
 
 
 
 
7e4e0ac
128e4f0
 
 
 
 
 
d9c7dce
7a4b92f
 
06fa6cd
7a4b92f
 
193f172
034aaec
5657a6c
058f18b
 
06fa6cd
7a4b92f
 
128e4f0
7a4b92f
128e4f0
 
d9c7dce
 
 
 
 
 
 
 
 
058f18b
128e4f0
 
 
058f18b
7a4b92f
 
 
 
 
058f18b
2a572c2
 
 
 
 
 
 
d8d9ab6
2a572c2
 
 
 
7e4e0ac
2a572c2
7e4e0ac
2a572c2
a63e231
 
17fa97d
 
d9c7dce
2a572c2
17fa97d
7a4b92f
 
 
a2bc65a
d9c7dce
a2bc65a
7a4b92f
 
 
a2bc65a
7a4b92f
 
 
 
 
 
 
 
 
 
 
 
 
5ab0373
7a4b92f
 
 
 
29e11ce
17fa97d
 
 
7a4b92f
5ab0373
7a4b92f
 
 
 
 
 
5ab0373
128e4f0
 
 
 
5ab0373
 
17fa97d
 
2a572c2
058f18b
29e11ce
17fa97d
128e4f0
 
 
5ab0373
 
592e4db
c780404
128e4f0
7a4b92f
 
 
 
592e4db
7a4b92f
 
 
5ab0373
7a4b92f
 
5ab0373
 
7a4b92f
a2bc65a
 
7a4b92f
 
 
128e4f0
a2bc65a
 
 
 
d9c7dce
a2bc65a
7a4b92f
128e4f0
 
 
 
 
0df5c51
193f172
 
128e4f0
 
 
a2bc65a
128e4f0
 
a2bc65a
 
128e4f0
 
193f172
128e4f0
 
 
 
 
592e4db
128e4f0
 
034aaec
128e4f0
 
 
034aaec
128e4f0
 
 
 
034aaec
128e4f0
 
 
 
 
 
 
 
 
 
034aaec
128e4f0
 
 
 
 
 
 
 
034aaec
128e4f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
592e4db
128e4f0
034aaec
128e4f0
 
034aaec
128e4f0
 
034aaec
193f172
5ab0373
128e4f0
 
 
 
 
 
 
 
5ab0373
128e4f0
 
 
7a4b92f
d9c7dce
7a4b92f
 
06fa6cd
a2bc65a
128e4f0
a2bc65a
 
 
128e4f0
a2bc65a
fbb3995
 
128e4f0
a2bc65a
592e4db
7a4b92f
 
5ab0373
 
a2bc65a
 
 
 
 
 
 
 
 
 
 
 
 
d9c7dce
 
 
a2bc65a
 
 
 
128e4f0
a2bc65a
 
128e4f0
d9c7dce
 
 
 
 
 
 
 
128e4f0
 
 
 
 
 
 
d9c7dce
 
 
 
 
7a4b92f
 
 
 
 
 
 
 
 
 
 
a2bc65a
 
 
 
 
d9c7dce
a2bc65a
7a4b92f
17fa97d
128e4f0
7a4b92f
 
 
a2bc65a
7a4b92f
 
128e4f0
a2bc65a
 
d9c7dce
a2bc65a
592e4db
d9c7dce
 
 
a2bc65a
128e4f0
a2bc65a
7a4b92f
 
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import gradio as gr
import numpy as np
import json
from zipfile import ZipFile
import os

from backend.dataloader import create_dataloader_aris
from backend.aws_handler import ping_server
from backend.predict import predict_task
from backend.uploader import save_data_to_dir, create_data_dir, save_data
from backend.InferenceConfig import InferenceConfig

from frontend.upload_ui import Upload_Gradio, models
from frontend.result_ui import Result_Gradio, update_result, create_metadata_table, table_headers, info_headers
from frontend.annotation_handler import load_annotation, prepare_annotation, js_store_frame_info, annotation_css
from frontend.state_handler import reset_state



#Initialize State & Result
state = {
    'version': "Experimental 1.0",
    'files': [],
    'index': 1,
    'total': 1,
    'annotation_index': -1,
    'frame_index': 0,
    'outputs': [],
    'config': None,
    'enable_annotation_editor': False,
}
result = {}
components = {}

# -------------------------------------------- ----- UPLOAD ARIS FILE ------------------------------------------------------
# Called when an Aris file is uploaded for inference - calls infer_next
def on_aris_input(
        file_list, 
        model_id, 
        conf_thresh, iou_thresh, 
        min_hits, max_age, 
        associative_tracker, boost_power, boost_decay, byte_low_conf, byte_high_conf, 
        min_length, max_length, min_travel, 
        output_formats
    ):

    if isinstance(file_list, tuple):
        file_list = [file_list]

    print(output_formats)

    # Reset Result
    reset_state(result, state)
    state['files'] = file_list
    state['total'] = len(file_list)
    state['outputs'] = output_formats
    state['config'] = InferenceConfig(
        weights = models[model_id] if model_id in models else models['master'],
        conf_thresh = conf_thresh,
        nms_iou = iou_thresh,
        min_hits = min_hits,
        max_age = max_age,
        min_length = min_length,
        max_length = max_length,
        min_travel = min_travel,
    )

    # Enable tracker if specified
    if (associative_tracker == "Confidence Boost"):
        state['config'].enable_conf_boost(boost_power, boost_decay)
    elif (associative_tracker == "ByteTrack"):
        state['config'].enable_byte_track(byte_low_conf, byte_high_conf)
    else: 
        state['config'].enable_sort_track()

    print(" ")
    print("Inference with:")
    print(state['config'].to_dict())
    print(" ")

    # Update loading_space to start inference on first file
    return {
        inference_handler: gr.update(value = str(np.random.rand()), visible=True),
        components['cancel_btn']: gr.update(visible=True),
        master_tabs: gr.update(selected=1)
    }

# Iterative function that performs inference on the next file in line
def infer_next(_, progress=gr.Progress()):

    if state['index'] >= state['total']: 
        return {
            result_handler: gr.update(),
            inference_handler: gr.update()
        }

    # Correct progress function for batch file input
    set_progress = lambda pct, msg : progress(pct, desc=msg)
    if state['total'] > 1:
        set_progress = lambda pct, msg : progress(pct, desc="File " + str(state['index']+1) + "/" + str(state['total']) + ": " + msg)
    set_progress(0, "Starting...")

    # Save file and create a new directory for result
    file_info = state['files'][state['index']]
    file_name = file_info[0].split("/")[-1]
    bytes = file_info[1]
    valid, file_path, dir_name = save_data(bytes, file_name)

    print("Directory: ", dir_name)
    print("Aris input: ", file_path)
    print(" ")

    # Check that the file was valid
    if not valid: 
        return {
            result_handler: gr.update(),
            inference_handler: gr.update()
        }

    # Send uploaded file to AWS 
    ping_server(file_name, state)
    #upload_file(file_path, "fishcounting", "webapp_uploads/files/" + file_name)

    #crop_clip(file_path, 65)

    # Do inference
    json_result, json_filepath, zip_filepath, video_filepath, marking_filepath = predict_task(
        file_path, 
        config = state['config'],
        output_formats = state['outputs'],
        gradio_progress = set_progress
    )

    # prepare dummy dataloader for visualizations
    _, dataset = create_dataloader_aris(file_path, num_frames_bg_subtract=0)
    
    # Store result for that file
    result['json_result'].append(json_result)
    result['aris_input'].append(file_path)
    result['datasets'].append(dataset)
    result["path_video"].append(video_filepath)
    result["path_zip"].append(zip_filepath)
    result["path_json"].append(json_filepath)
    result["path_marking"].append(marking_filepath)
    fish_table, fish_info = create_metadata_table(json_result, table_headers, info_headers)
    result["fish_table"].append(fish_table)
    result["fish_info"].append(fish_info)

    # Increase file index
    state['index'] += 1

    # Send of update to result_handler to show new result
    # Leave inference_handler update blank to avoid starting next inference until result is updated
    return {
        result_handler: gr.update(value = str(np.random.rand())),
        tab_labeler: gr.update(value = str(state['index'])),
        inference_handler: gr.update()
    }

# Cancel inference
def cancel_inference():
    return {
        master_tabs: gr.update(selected=0),
        inference_handler: gr.update(visible=False),
        components['cancel_btn']: gr.update(visible=False)
    }

# Show result
def on_result_ready():
    # Update result tab for last file
    i = state["index"] - 1
    return update_result(i, state, result, inference_handler)



# ------------------------------------------------- UPLOAD RESULT FILE -----------------------------------------------------
# Called when result file is uploaded for review
def on_result_upload():
    return {
        master_tabs: gr.update(selected=1),
        result_uploader: gr.update(value=str(np.random.rand()))
    }

# Called when result upload is finished processing
def on_result_upload_finish(zip_list, aris_list):

    if (zip_list == None): 
        zip_list = [("static/example/example_result.zip", None)]
        aris_path = "static/example/input_file.aris"
        aris_list = [(aris_path, bytearray(open(aris_path, 'rb').read()))]
    

    reset_state(result, state)
    state['outputs'] = ["Generate Annotated Video", "Generate Manual Marking", "Generate PDF"]

    component_updates = { 
        tab_labeler: gr.update(value = len(zip_list))
    }

    for i in range(len(zip_list)):
            
        # Create dir to unzip files
        dir_name = create_data_dir(str(i))

        # Check aris input 
        if (aris_list):
            aris_info = aris_list[i]
            file_name = aris_info[0].split("/")[-1]
            bytes = aris_info[1]
            valid, input_path, dir_name = save_data_to_dir(bytes, file_name, dir_name)
            _, dataset = create_dataloader_aris(input_path, num_frames_bg_subtract=0)
        else:
            input_path = None
            dataset = None

        # Unzip result
        zip_info = zip_list[i]
        zip_name = zip_info[0]
        print(zip_name)
        with ZipFile(zip_name) as zip_file:
            ZipFile.extractall(zip_file, path=dir_name)
        unzipped = os.listdir(dir_name)
        print(unzipped)

        for file in unzipped:
            if (file.endswith("_results.mp4")):
                result["path_video"].append(os.path.join(dir_name, file))
            elif (file.endswith("_results.json")):
                result["path_json"].append(os.path.join(dir_name, file))
            elif (file.endswith("_marking.txt")):
                result["path_marking"].append(os.path.join(dir_name, file))
        
        result["aris_input"].append(input_path)
        result["datasets"].append(dataset)
        with open(result['path_json'][-1]) as f:
            json_result = json.load(f)
            result['json_result'].append(json_result)
        fish_table, fish_info = create_metadata_table(json_result, table_headers, info_headers)
        result["fish_table"].append(fish_table)
        result["fish_info"].append(fish_info)

        update = update_result(i, state, result, inference_handler)

        for key in update.keys():
            component_updates[key] = update[key]
    
    component_updates.pop(inference_handler)
    return component_updates



# ------------------------------------------------- ANNOTATION EDITOR -----------------------------------------------------
def on_annotation_open(result_index):
    return prepare_annotation(state, result, result_index)

def annotate_next(_, progress=gr.Progress()):
    return load_annotation(state, result, progress)



# -------------------------------------------------- GRADIO ARCHITECTURE ----------------------------------------------------
with gr.Blocks() as demo:
    with gr.Blocks():

        # Title of page + style
        gr.HTML(
            """
            <h1 align="center" style="font-size:xxx-large">Caltech Fisheye - Experimental</h1>
            <style>
                /* Disable header of metadata list in result */
                #marking_json thead {
                    display: none !important;
                }
                /* Color of selected tab */
                .selected.svelte-kqij2n {
                    background: linear-gradient(180deg, #66eecb47, transparent);
                }
                """ + annotation_css + """
            </style>
            <style id="tab_style"></style>
        """
        )


        with gr.Tabs() as master_tabs:
            components['master_tabs'] = master_tabs

            # Master Tab for uploading aris or result files
            with gr.Tab("Upload", id=0):
                
                # Draw Gradio components related to the upload ui
                Upload_Gradio(components)

            # Master Tab for result visualization
            with gr.Tab("Result", id=1):

                # Define annotation progress bar for event listeres, but unrender since it will be displayed later on
                result_uploader = gr.HTML("", visible=False)
                components['result_uploader'] = result_uploader

                annotation_progress = gr.HTML("", visible=False).unrender()
                components['annotation_progress'] = annotation_progress
                
                # Draw the gradio components related to visualzing result
                visualization_components = Result_Gradio(on_annotation_open, components, state)
                
            # Master Tab for annotation editing
            if state['enable_annotation_editor']:
                with gr.Tab("Annotation Editor", id=2):

                    # Draw the annotation loading bar here
                    annotation_progress.render()

                    # Add annotation editor component
                    annotation_editor = gr.HTML("", visible=False)

                    # Event listener for batch loading of annotation frames 
                    annotation_progress.change(
                        annotate_next, 
                        annotation_progress, 
                        [annotation_editor, annotation_progress], 
                        _js=js_store_frame_info
                    )
                    
                    # Event listener for running javascript defined in 'annotation_editor.js'
                    # show_annotation
                    with open('gradio_scripts/annotation_editor.js', 'r') as f:
                        annotation_editor.change(lambda x: gr.update(), None, annotation_editor, _js=f.read())

    # Disclaimer at the bottom of page
    gr.HTML(
            """
        <p align="center">
        <b>Note</b>: The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. 
        In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.
        </p>
        """
    )

    # Extract important components for ease of code
    input = components['input']
    inference_handler = components['inference_handler']
    result_handler = components['result_handler']
    tab_labeler = components['tab_labeler']
    inference_comps = [inference_handler, master_tabs, components['cancel_btn']]

    # When a file is uploaded to the input, tell the inference_handler to start inference
    input.upload(on_aris_input, [input] + components['hyperparams'], inference_comps)
    components['inference_btn'].click(on_aris_input, [input] + components['hyperparams'], inference_comps)

    # When inference handler updates, tell result_handler to show the new result
    # Also, add inference_handler as the output in order to have it display the progress
    inference_event = inference_handler.change(infer_next, None, [inference_handler, result_handler, tab_labeler])

    # Send UI changes based on the new results to the UI_components, and tell the inference_handler to start next inference
    result_handler.change(on_result_ready, None, visualization_components + [inference_handler])

    # Cancel and skip buttons
    components['cancel_btn'].click(cancel_inference, None, inference_comps, cancels=[inference_event])

    # Button to load a previous result and view visualization
    components['open_result_btn'].click(on_result_upload, None, [result_uploader, master_tabs])
    components['result_uploader'].change(
        on_result_upload_finish, 
        [components['result_input'], components['result_aris_input']], 
        visualization_components + [tab_labeler]
    )

demo.queue().launch()