-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathstreamlit_app.py
126 lines (99 loc) · 3.51 KB
/
streamlit_app.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
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.
st.sidebar.write('#### Select an image to upload.')
uploaded_file = st.sidebar.file_uploader('',
type=['png', 'jpg', 'jpeg'],
accept_multiple_files=False)
st.sidebar.write('[Find additional images on Roboflow.](https://public.roboflow.com/object-detection/bccd/)')
## 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('./images/roboflow_logo.png')
st.sidebar.image(image,
use_column_width=True)
image = Image.open('./images/streamlit_logo.png')
st.sidebar.image(image,
use_column_width=True)
##########
##### Set up main app.
##########
## Title.
st.write('# Blood Cell Count Object Detection')
## Pull in default image or user-selected image.
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')
# 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.
upload_url = ''.join([
'https://infer.roboflow.com/rf-bccd-bkpj9--1',
'?access_token=vbIBKNgIXqAQ',
'&format=image',
f'&overlap={overlap_threshold * 100}',
f'&confidence={confidence_threshold * 100}',
'&stroke=2',
'&labels=True'
])
## POST to the API.
r = requests.post(upload_url,
data=img_str,
headers={
'Content-Type': 'application/x-www-form-urlencoded'
})
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)
## 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.
confidences = [box['confidence'] for box in output_dict['predictions']]
## 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)
## Display the JSON in main app.
st.write('### JSON Output')
st.write(r.json())