File size: 2,173 Bytes
5ab0373
 
 
 
 
 
 
 
193f172
 
5ab0373
a2bc65a
 
 
 
 
 
 
 
 
 
 
5ab0373
 
193f172
 
 
 
 
 
 
 
 
 
 
 
5ab0373
193f172
5ab0373
193f172
 
5ab0373
193f172
 
 
 
 
 
 
5ab0373
193f172
5ab0373
 
 
193f172
5ab0373
 
 
 
 
 
 
 
 
 
 
193f172
 
 
 
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
from dataloader import create_dataloader_aris
from aris import BEAM_WIDTH_DIR
import json
import cv2
import base64

VIDEO_HEIGHT = 700


def init_frames(video, preds, gp=None):
    """Load frames for annotation editing

    
        Returns:
            list({
                frame: frame image as base64 string,
                annotations: list(
                    bbox: dict of int defining bounding box {left, right, top, bottom},
                    id: id of fish as int,
                    conf: confidence in bbox as float
                )
            })
    """

    if gp: gp(0, "Loading Frames")

    dataloader, dataset = create_dataloader_aris(video, BEAM_WIDTH_DIR, None)
    images = dataset.didson.load_frames(start_frame=0, end_frame=1)

    # assumes all frames the same size
    h, w = images[0].shape

    # enforce a standard size so that text/box thickness is consistent
    scale_factor = VIDEO_HEIGHT / h
    h = VIDEO_HEIGHT
    w = int(scale_factor*w)
    
    annotations = []

    if gp: gp(0, "Extracting Frames")
    if len(preds['frames']):
        
        for i, frame_info in enumerate(preds['frames']):
            if gp: gp(i/len(preds['frames']), "Extracting Frames")

            # Extract frames
            img_raw = dataset.didson.load_frames(start_frame=i, end_frame=i+1)[0]
            image = cv2.resize(cv2.cvtColor(img_raw, cv2.COLOR_GRAY2BGR), (w, h))
            cv2.imwrite("annotation_frame_dir/" + str(i) + ".jpg", image)

            # Extract annotations
            frame = {
                'annotations': [],
            }
            for fish in frame_info['fish']:
                xmin, ymin, xmax, ymax = fish['bbox']
                frame['annotations'].append({
                    'bbox': {
                        'left': int(round(xmin * w)),
                        'right': int(round(xmax * w)),
                        'top': int(round(ymin * h)),
                        'bottom': int(round(ymax * h)),
                    },
                    'id': str(fish['fish_id']),
                    'conf': fish['conf']
                })
            annotations.append(frame)
    
    return annotations