#!/usr/bin/env python3
import os
import streamlit as st
import random
import json
from PIL import Image

def get_mispredicted_scans():
    with open('data/verify2.log', 'r') as f:
        lines = f.readlines()
    for line in lines:
        fields = line.split()
        if len(fields) != 6:
            continue
        if fields[1] != fields[2]:
            yield fields[0]

def main():
    st.title('Browser')

    # scan_ids = os.listdir('data/scans')
    # to_show = sorted(scan_ids, key=lambda x: int(x), reverse=True)[:100]
    to_show = list(get_mispredicted_scans())
    st.write(f'to show: {len(to_show)}')
    for sid in to_show:
        show_scan(sid)

def show_scan(scan_id):
    scan_dir = f'data/scans/{scan_id}'
    mdfile = f'{scan_dir}/metadata.json'
    md = json.load(open(mdfile))
    if not os.path.exists(mdfile):
        return
    sbs = Image.open(f'{scan_dir}/sbs.jpg')
    st.write(f'{scan_id}: {md["labels"]}')
    st.image(sbs.resize((512, 256)))
    st.divider()


if __name__ == '__main__':
    main()