Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -60,15 +60,18 @@ IMAGE_HEIGHT_SEG = 576
|
|
| 60 |
path_yolo_model = os.path.join(os.getcwd(), 'app', 'detection_model.pt')
|
| 61 |
path_segmentation_model = os.path.join(os.getcwd(), 'app', 'segmentation_model.pth')
|
| 62 |
|
| 63 |
-
|
|
|
|
| 64 |
def get_image_paths(base_dir: str):
|
| 65 |
return glob.glob(f'{os.getcwd()}/{base_dir}/*.png')
|
| 66 |
|
| 67 |
-
|
|
|
|
| 68 |
def get_num_images():
|
| 69 |
return len(st.session_state['image_path_list'])
|
| 70 |
|
| 71 |
-
|
|
|
|
| 72 |
def increment_index(index_current: int, max_index: int, overflow=False, min_index=0):
|
| 73 |
index_new = index_current + 1
|
| 74 |
if index_new <= max_index:
|
|
@@ -78,7 +81,8 @@ def increment_index(index_current: int, max_index: int, overflow=False, min_inde
|
|
| 78 |
else:
|
| 79 |
return index_current
|
| 80 |
|
| 81 |
-
|
|
|
|
| 82 |
def decrement_index(index_current: int, min_index, overflow=False, max_index=-1):
|
| 83 |
index_new = index_current - 1
|
| 84 |
if index_new >= min_index:
|
|
@@ -88,30 +92,35 @@ def decrement_index(index_current: int, min_index, overflow=False, max_index=-1)
|
|
| 88 |
else:
|
| 89 |
return index_current
|
| 90 |
|
| 91 |
-
|
|
|
|
| 92 |
def callback_button_previous(overflow_index=True):
|
| 93 |
new_index = decrement_index(st.session_state['image_index_current'], min_index=0,
|
| 94 |
overflow=overflow_index, max_index=st.session_state['num_images']-1)
|
| 95 |
update_on_index_change(new_index)
|
| 96 |
|
| 97 |
-
|
|
|
|
| 98 |
def callback_button_next(overflow_index=True):
|
| 99 |
new_index = increment_index(st.session_state['image_index_current'], st.session_state['num_images'],
|
| 100 |
overflow=overflow_index, min_index=0)
|
| 101 |
update_on_index_change(new_index)
|
| 102 |
|
| 103 |
-
|
|
|
|
| 104 |
def update_on_index_change(new_index: int):
|
| 105 |
st.session_state['image_index_current'] = new_index
|
| 106 |
st.session_state['current_image_array'] = get_current_image()
|
| 107 |
# put the current bale boundaries into the list, regardless of whether they have been stored to the database
|
| 108 |
st.session_state['current_measurement'] = get_current_measurement()
|
| 109 |
|
| 110 |
-
|
|
|
|
| 111 |
def load_image_array(image_path: str):
|
| 112 |
return cv2.imread(image_path)
|
| 113 |
|
| 114 |
-
|
|
|
|
| 115 |
def get_current_image():
|
| 116 |
index_current = st.session_state['image_index_current']
|
| 117 |
this_img_current = st.session_state['image_data_list'][index_current]
|
|
@@ -122,7 +131,8 @@ def get_current_image():
|
|
| 122 |
st.session_state['image_data_list'][index_current] = this_img_current
|
| 123 |
return this_img_current
|
| 124 |
|
| 125 |
-
|
|
|
|
| 126 |
def callback_button_measure():
|
| 127 |
has_measurement, measurement_result = get_current_measurement()
|
| 128 |
if has_measurement:
|
|
@@ -130,12 +140,14 @@ def callback_button_measure():
|
|
| 130 |
else:
|
| 131 |
display_calculate_measurement_data()
|
| 132 |
|
| 133 |
-
|
|
|
|
| 134 |
def display_cached_measurement_data():
|
| 135 |
st.info('Getting cached measurement', icon="ℹ️")
|
| 136 |
display_measurement()
|
| 137 |
|
| 138 |
-
|
|
|
|
| 139 |
def display_calculate_measurement_data():
|
| 140 |
with st.spinner('Calculating Profile Height....'):
|
| 141 |
this_image_path = st.session_state['image_path_list'][st.session_state['image_index_current']]
|
|
@@ -144,7 +156,8 @@ def display_calculate_measurement_data():
|
|
| 144 |
st.success('Measurement is done !')
|
| 145 |
display_measurement()
|
| 146 |
|
| 147 |
-
|
|
|
|
| 148 |
def measure_image(image_path: str):
|
| 149 |
measurement_result = measure_strip(img_path=image_path,
|
| 150 |
model_yolo=st.session_state['models']['detection'],
|
|
@@ -166,7 +179,8 @@ def measure_image(image_path: str):
|
|
| 166 |
arr_1[:, 0] = np.abs(arr_1[:, 0])
|
| 167 |
return arr_0, arr_1
|
| 168 |
|
| 169 |
-
|
|
|
|
| 170 |
def get_current_measurement():
|
| 171 |
this_measurement = st.session_state['measurement_data_list'][st.session_state['image_index_current']]
|
| 172 |
if this_measurement is not None:
|
|
@@ -174,18 +188,21 @@ def get_current_measurement():
|
|
| 174 |
else:
|
| 175 |
return False, None
|
| 176 |
|
| 177 |
-
|
|
|
|
| 178 |
def update_measurements(measurement, index_measurement):
|
| 179 |
st.session_state['measurement_data_list'][index_measurement] = measurement
|
| 180 |
|
| 181 |
-
|
|
|
|
| 182 |
def display_measurement():
|
| 183 |
has_measurement, measurement_data = get_current_measurement()
|
| 184 |
if has_measurement:
|
| 185 |
st.subheader(f'Profile Height')
|
| 186 |
measurement_to_streamlit_chart(measurement_data[0], measurement_data[1])
|
| 187 |
|
| 188 |
-
|
|
|
|
| 189 |
def measurement_to_streamlit_chart(profile_array_1, profile_array_2):
|
| 190 |
height_list = []
|
| 191 |
coord_list = []
|
|
@@ -200,7 +217,8 @@ def measurement_to_streamlit_chart(profile_array_1, profile_array_2):
|
|
| 200 |
fig = px.line(df, x='x', y='y', color='indicator', symbol="indicator")
|
| 201 |
st.plotly_chart(fig, use_container_width=True)
|
| 202 |
|
| 203 |
-
|
|
|
|
| 204 |
if 'image_path_list' not in st.session_state:
|
| 205 |
st.session_state['image_path_list'] = get_image_paths(IMG_BASE_DIR)
|
| 206 |
|
|
@@ -227,7 +245,8 @@ if 'models' not in st.session_state:
|
|
| 227 |
model_segmentation_path=path_segmentation_model)
|
| 228 |
st.session_state['models'] = {'segmentation': seg_dplv3_model, 'detection': yolo_nn_model}
|
| 229 |
|
| 230 |
-
|
|
|
|
| 231 |
image_emoji = '📷'
|
| 232 |
model_emoji = '⚙️'
|
| 233 |
profile_emoji = '📈'
|
|
|
|
| 60 |
path_yolo_model = os.path.join(os.getcwd(), 'app', 'detection_model.pt')
|
| 61 |
path_segmentation_model = os.path.join(os.getcwd(), 'app', 'segmentation_model.pth')
|
| 62 |
|
| 63 |
+
# Function: get_image_paths
|
| 64 |
+
# Description: Returns a list of image paths in the specified directory.
|
| 65 |
def get_image_paths(base_dir: str):
|
| 66 |
return glob.glob(f'{os.getcwd()}/{base_dir}/*.png')
|
| 67 |
|
| 68 |
+
# Function: get_num_images
|
| 69 |
+
# Description: Returns the number of images in the current session state.
|
| 70 |
def get_num_images():
|
| 71 |
return len(st.session_state['image_path_list'])
|
| 72 |
|
| 73 |
+
# Function: increment_index
|
| 74 |
+
# Description: Increments the current image index by 1, taking into account the maximum index and optional overflow behavior.
|
| 75 |
def increment_index(index_current: int, max_index: int, overflow=False, min_index=0):
|
| 76 |
index_new = index_current + 1
|
| 77 |
if index_new <= max_index:
|
|
|
|
| 81 |
else:
|
| 82 |
return index_current
|
| 83 |
|
| 84 |
+
# Function: decrement_index
|
| 85 |
+
# Description: Decrements the current image index by 1, taking into account the minimum index and optional overflow behavior.
|
| 86 |
def decrement_index(index_current: int, min_index, overflow=False, max_index=-1):
|
| 87 |
index_new = index_current - 1
|
| 88 |
if index_new >= min_index:
|
|
|
|
| 92 |
else:
|
| 93 |
return index_current
|
| 94 |
|
| 95 |
+
# Function: callback_button_previous
|
| 96 |
+
# Description: Callback function for the "previous Image" button. Updates the current image index and calls update_on_index_change.
|
| 97 |
def callback_button_previous(overflow_index=True):
|
| 98 |
new_index = decrement_index(st.session_state['image_index_current'], min_index=0,
|
| 99 |
overflow=overflow_index, max_index=st.session_state['num_images']-1)
|
| 100 |
update_on_index_change(new_index)
|
| 101 |
|
| 102 |
+
# Function: callback_button_next
|
| 103 |
+
# Description: Callback function for the "next Image" button. Updates the current image index and calls update_on_index_change.
|
| 104 |
def callback_button_next(overflow_index=True):
|
| 105 |
new_index = increment_index(st.session_state['image_index_current'], st.session_state['num_images'],
|
| 106 |
overflow=overflow_index, min_index=0)
|
| 107 |
update_on_index_change(new_index)
|
| 108 |
|
| 109 |
+
# Function: update_on_index_change
|
| 110 |
+
# Description: Updates the session state variables related to the current image index and the current image array. Calls get_current_image and get_current_measurement.
|
| 111 |
def update_on_index_change(new_index: int):
|
| 112 |
st.session_state['image_index_current'] = new_index
|
| 113 |
st.session_state['current_image_array'] = get_current_image()
|
| 114 |
# put the current bale boundaries into the list, regardless of whether they have been stored to the database
|
| 115 |
st.session_state['current_measurement'] = get_current_measurement()
|
| 116 |
|
| 117 |
+
# Function: load_image_array
|
| 118 |
+
# Description: Loads an image array from the specified image path.
|
| 119 |
def load_image_array(image_path: str):
|
| 120 |
return cv2.imread(image_path)
|
| 121 |
|
| 122 |
+
# Function: get_current_image
|
| 123 |
+
# Description: Returns the current image array based on the current image index. If the image array is not already loaded, it loads it using load_image_array.
|
| 124 |
def get_current_image():
|
| 125 |
index_current = st.session_state['image_index_current']
|
| 126 |
this_img_current = st.session_state['image_data_list'][index_current]
|
|
|
|
| 131 |
st.session_state['image_data_list'][index_current] = this_img_current
|
| 132 |
return this_img_current
|
| 133 |
|
| 134 |
+
# Function: callback_button_measure
|
| 135 |
+
# Description: Callback function for the "Measure" button. Calls either display_cached_measurement_data or display_calculate_measurement_data based on whether the current image has a cached measurement.
|
| 136 |
def callback_button_measure():
|
| 137 |
has_measurement, measurement_result = get_current_measurement()
|
| 138 |
if has_measurement:
|
|
|
|
| 140 |
else:
|
| 141 |
display_calculate_measurement_data()
|
| 142 |
|
| 143 |
+
# Function: display_cached_measurement_data
|
| 144 |
+
# Description: Displays the cached measurement data for the current image.
|
| 145 |
def display_cached_measurement_data():
|
| 146 |
st.info('Getting cached measurement', icon="ℹ️")
|
| 147 |
display_measurement()
|
| 148 |
|
| 149 |
+
# Function: display_calculate_measurement_data
|
| 150 |
+
# Description: Calculates the measurement data for the current image and updates the session state. Displays the measurement data.
|
| 151 |
def display_calculate_measurement_data():
|
| 152 |
with st.spinner('Calculating Profile Height....'):
|
| 153 |
this_image_path = st.session_state['image_path_list'][st.session_state['image_index_current']]
|
|
|
|
| 156 |
st.success('Measurement is done !')
|
| 157 |
display_measurement()
|
| 158 |
|
| 159 |
+
# Function: measure_image
|
| 160 |
+
# Description: Calls the measure_strip function from the strip_measure_4_0 module to measure the strip in the current image. Returns the measurement result.
|
| 161 |
def measure_image(image_path: str):
|
| 162 |
measurement_result = measure_strip(img_path=image_path,
|
| 163 |
model_yolo=st.session_state['models']['detection'],
|
|
|
|
| 179 |
arr_1[:, 0] = np.abs(arr_1[:, 0])
|
| 180 |
return arr_0, arr_1
|
| 181 |
|
| 182 |
+
# Function: get_current_measurement
|
| 183 |
+
# Description: Returns the current measurement data based on the current image index. If the measurement data is not available, returns False and None.
|
| 184 |
def get_current_measurement():
|
| 185 |
this_measurement = st.session_state['measurement_data_list'][st.session_state['image_index_current']]
|
| 186 |
if this_measurement is not None:
|
|
|
|
| 188 |
else:
|
| 189 |
return False, None
|
| 190 |
|
| 191 |
+
# Function: update_measurements
|
| 192 |
+
# Description: Updates the measurement data for the specified index in the session state.
|
| 193 |
def update_measurements(measurement, index_measurement):
|
| 194 |
st.session_state['measurement_data_list'][index_measurement] = measurement
|
| 195 |
|
| 196 |
+
# Function: display_measurement
|
| 197 |
+
# Description: Displays the measurement data for the current image.
|
| 198 |
def display_measurement():
|
| 199 |
has_measurement, measurement_data = get_current_measurement()
|
| 200 |
if has_measurement:
|
| 201 |
st.subheader(f'Profile Height')
|
| 202 |
measurement_to_streamlit_chart(measurement_data[0], measurement_data[1])
|
| 203 |
|
| 204 |
+
# Function: measurement_to_streamlit_chart
|
| 205 |
+
# Description: Converts the measurement data into a Pandas DataFrame and plots a line chart using Plotly.
|
| 206 |
def measurement_to_streamlit_chart(profile_array_1, profile_array_2):
|
| 207 |
height_list = []
|
| 208 |
coord_list = []
|
|
|
|
| 217 |
fig = px.line(df, x='x', y='y', color='indicator', symbol="indicator")
|
| 218 |
st.plotly_chart(fig, use_container_width=True)
|
| 219 |
|
| 220 |
+
# Initialization section
|
| 221 |
+
|
| 222 |
if 'image_path_list' not in st.session_state:
|
| 223 |
st.session_state['image_path_list'] = get_image_paths(IMG_BASE_DIR)
|
| 224 |
|
|
|
|
| 245 |
model_segmentation_path=path_segmentation_model)
|
| 246 |
st.session_state['models'] = {'segmentation': seg_dplv3_model, 'detection': yolo_nn_model}
|
| 247 |
|
| 248 |
+
# Display section
|
| 249 |
+
|
| 250 |
image_emoji = '📷'
|
| 251 |
model_emoji = '⚙️'
|
| 252 |
profile_emoji = '📈'
|