-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathapp.py
109 lines (94 loc) · 4 KB
/
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
import streamlit as st
import os
from streamlit_img_label import st_img_label
from streamlit_img_label.manage import ImageManager, ImageDirManager
def run(img_dir, labels):
st.set_option("deprecation.showfileUploaderEncoding", False)
idm = ImageDirManager(img_dir)
if "files" not in st.session_state:
st.session_state["files"] = idm.get_all_files()
st.session_state["annotation_files"] = idm.get_exist_annotation_files()
st.session_state["image_index"] = 0
else:
idm.set_all_files(st.session_state["files"])
idm.set_annotation_files(st.session_state["annotation_files"])
def refresh():
st.session_state["files"] = idm.get_all_files()
st.session_state["annotation_files"] = idm.get_exist_annotation_files()
st.session_state["image_index"] = 0
def next_image():
image_index = st.session_state["image_index"]
if image_index < len(st.session_state["files"]) - 1:
st.session_state["image_index"] += 1
else:
st.warning('This is the last image.')
def previous_image():
image_index = st.session_state["image_index"]
if image_index > 0:
st.session_state["image_index"] -= 1
else:
st.warning('This is the first image.')
def next_annotate_file():
image_index = st.session_state["image_index"]
next_image_index = idm.get_next_annotation_image(image_index)
if next_image_index:
st.session_state["image_index"] = idm.get_next_annotation_image(image_index)
else:
st.warning("All images are annotated.")
next_image()
def go_to_image():
file_index = st.session_state["files"].index(st.session_state["file"])
st.session_state["image_index"] = file_index
# Sidebar: show status
n_files = len(st.session_state["files"])
n_annotate_files = len(st.session_state["annotation_files"])
st.sidebar.write("Total files:", n_files)
st.sidebar.write("Total annotate files:", n_annotate_files)
st.sidebar.write("Remaining files:", n_files - n_annotate_files)
st.sidebar.selectbox(
"Files",
st.session_state["files"],
index=st.session_state["image_index"],
on_change=go_to_image,
key="file",
)
col1, col2 = st.sidebar.columns(2)
with col1:
st.button(label="Previous image", on_click=previous_image)
with col2:
st.button(label="Next image", on_click=next_image)
st.sidebar.button(label="Next need annotate", on_click=next_annotate_file)
st.sidebar.button(label="Refresh", on_click=refresh)
# Main content: annotate images
img_file_name = idm.get_image(st.session_state["image_index"])
img_path = os.path.join(img_dir, img_file_name)
im = ImageManager(img_path)
img = im.get_img()
resized_img = im.resizing_img()
resized_rects = im.get_resized_rects()
rects = st_img_label(resized_img, box_color="red", rects=resized_rects)
def annotate():
im.save_annotation()
image_annotate_file_name = img_file_name.split(".")[0] + ".xml"
if image_annotate_file_name not in st.session_state["annotation_files"]:
st.session_state["annotation_files"].append(image_annotate_file_name)
next_annotate_file()
if rects:
st.button(label="Save", on_click=annotate)
preview_imgs = im.init_annotation(rects)
for i, prev_img in enumerate(preview_imgs):
prev_img[0].thumbnail((200, 200))
col1, col2 = st.columns(2)
with col1:
col1.image(prev_img[0])
with col2:
default_index = 0
if prev_img[1]:
default_index = labels.index(prev_img[1])
select_label = col2.selectbox(
"Label", labels, key=f"label_{i}", index=default_index
)
im.set_annotation(i, select_label)
if __name__ == "__main__":
custom_labels = ["", "dog", "cat"]
run("img_dir", custom_labels)