File size: 2,244 Bytes
5ab0373
 
 
 
 
 
193f172
034aaec
5ab0373
a2bc65a
 
 
 
 
 
 
 
 
 
 
5ab0373
193f172
 
 
 
 
 
 
 
 
5ab0373
193f172
5ab0373
193f172
 
5ab0373
034aaec
 
 
193f172
 
5657a6c
193f172
034aaec
 
 
5ab0373
193f172
5ab0373
 
034aaec
5ab0373
193f172
5ab0373
 
 
 
 
 
 
 
 
 
 
193f172
 
034aaec
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
import json
import cv2
import base64

VIDEO_HEIGHT = 700


def init_frames(dataset, preds, index, 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
                )
            })
    """
    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']):
        
        end_index = min(index+1000, len(preds['frames']))
        for i, frame_info in enumerate(preds['frames'][index:end_index]):
            if gp: gp((index + i)/len(preds['frames']), "Extracting Frames")

            # Extract frames
            img_raw = dataset.didson.load_frames(start_frame=index+i, end_frame=index+i+1)[0]
            image = cv2.resize(cv2.cvtColor(img_raw, cv2.COLOR_GRAY2BGR), (w, h))
            #cv2.imwrite("annotation_frame_dir/" + str(i) + ".jpg", image)
            retval, buffer = cv2.imencode('.jpg', image)
            b64 = base64.b64encode(buffer).decode("utf-8")

            # Extract annotations
            frame = {
                'annotations': [],
                'base64': b64
            }
            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, end_index