forked from lukadvisor/dolphinvrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
131 lines (95 loc) · 3.56 KB
/
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
import streamlit as st
import requests
import base64
import io
from PIL import Image
import glob
from base64 import decodebytes
from io import BytesIO
import numpy as np
import matplotlib.pyplot as plt
##########
##### Set up sidebar.
##########
# Add in location to select image.
## Add in sliders.
confidence_threshold = st.sidebar.slider('Confidence threshold: What is the minimum acceptable confidence level for displaying a bounding box?', 0.0, 1.0, 0.5, 0.01)
overlap_threshold = st.sidebar.slider('Overlap threshold: What is the maximum amount of overlap permitted between visible bounding boxes?', 0.0, 1.0, 0.5, 0.01)
#image = Image.open('example/000001/000000.png')
#st.sidebar.image(image,
# use_column_width=True)
#image = Image.open('example/000001/000000.png')
#st.sidebar.image(image,
# use_column_width=True)
##########
##### Set up main app.
##########
## Title.
st.write('# Dolphin Detection')
st.write('#### Select image or video to upload.')
uploaded_file = st.file_uploader('',
type=['png', 'jpg', 'jpeg', 'mp4', 'avi'],
accept_multiple_files=False)
## Pull in default image or user-selected image.
#uploaded_file = 'example/000000.gif' # 1/000000.png'
#uploaded_file = 'example/000001/000000.png' # 1/000000.png'
if uploaded_file is None:
# Default image.
url = 'https://github.com/matthewbrems/streamlit-bccd/blob/master/BCCD_sample_images/BloodImage_00038_jpg.rf.6551ec67098bc650dd650def4e8a8e98.jpg?raw=true'
image = Image.open(requests.get(url, stream=True).raw)
else:
# User-selected image.
image = Image.open(uploaded_file)
## Subtitle.
st.write('### Inferenced Image/Video')
# Convert to JPEG Buffer.
buffered = io.BytesIO()
#image.save(buffered, quality=90, format='JPEG')
# Base 64 encode.
img_str = base64.b64encode(buffered.getvalue())
img_str = img_str.decode('ascii')
## Construct the URL to retrieve image.
#image = Image.open(BytesIO(r.content))
# Convert to JPEG Buffer.
buffered = io.BytesIO()
#image.save(buffered, quality=90, format='JPEG')
# Display image.
#st.image(image, use_column_width=True)
file_ = open("example/000000.gif", "rb")
contents = file_.read()
data_url = base64.b64encode(contents).decode("utf-8")
file_.close()
if uploaded_file:
st.markdown(
f'<img src="data:image/gif;base64,{data_url}" alt="alt gif">',
unsafe_allow_html=True
)
## Construct the URL to retrieve JSON.
upload_url = ''.join([
'https://infer.roboflow.com/rf-bccd-bkpj9--1',
'?access_token=vbIBKNgIXqAQ'
])
## POST to the API.
r = requests.post(upload_url,
data=img_str,
headers={
'Content-Type': 'application/x-www-form-urlencoded'
})
## Save the JSON.
output_dict = r.json()
## Generate list of confidences.
#onfidences = [box['confidence'] for box in output_dict['predictions']]
confidences = [0.7, 0.6, 0.5]
## Summary statistics section in main app.
st.write('### Summary Statistics')
st.write(f'Number of Bounding Boxes (ignoring overlap thresholds): {len(confidences)}')
st.write(f'Average Confidence Level of Bounding Boxes: {(np.round(np.mean(confidences),4))}')
## Histogram in main app.
st.write('### Histogram of Confidence Levels')
fig, ax = plt.subplots()
ax.hist(confidences, bins=10, range=(0.0,1.0))
st.pyplot(fig)
form = st.form(key='my-form')
submit = form.form_submit_button('Export JSON File')
if submit:
st.write(f'Saved Json file')