HuggingKatze commited on
Commit
7dc3471
·
1 Parent(s): 6c81508

finalizing

Browse files
Files changed (4) hide show
  1. .streamlit/config.toml +4 -0
  2. README.md +5 -2
  3. app.py +16 -6
  4. utils.py +26 -7
.streamlit/config.toml ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ [server]
2
+ enableXsrfProtection = false
3
+ enableCORS = false
4
+ maxUploadSize = 200
README.md CHANGED
@@ -39,8 +39,11 @@ uv run streamlit run app.py
39
 
40
  ## Hugging Face Space URL
41
 
42
- Add your public Space URL here after deployment, for example:
43
- `https://huggingface.co/spaces/<username>/<space-name>`
 
 
 
44
 
45
  ## Repository Structure
46
 
 
39
 
40
  ## Hugging Face Space URL
41
 
42
+ For the most stable viewing experience, use the direct app URL:
43
+ https://huggingkatze-ct-windowing-quantization-demo.hf.space
44
+
45
+ The Hugging Face project page is:
46
+ https://huggingface.co/spaces/HuggingKatze/ct-windowing-quantization-demo
47
 
48
  ## Repository Structure
49
 
app.py CHANGED
@@ -265,14 +265,16 @@ def select_current_slice(loaded, source_mode: str):
265
  current_slice_index = int(st.session_state["slice_index"])
266
  current_image = loaded.volume[current_slice_index]
267
 
268
- if source_mode == "Upload a file" and loaded.source_kind in {"dicom", "nifti"}:
269
  st.radio(
270
  "Rotate uploaded image",
271
  options=list(ROTATION_OPTIONS.keys()),
272
  key="rotation_choice",
273
  horizontal=False,
274
  )
275
- control_note("If an uploaded DICOM or NIfTI slice looks sideways, use rotation to fix its viewing orientation before comparing outputs.")
 
 
276
  rotation_k = ROTATION_OPTIONS[st.session_state["rotation_choice"]]
277
  if rotation_k:
278
  current_image = np.rot90(current_image, k=rotation_k)
@@ -317,11 +319,19 @@ def main() -> None:
317
  else:
318
  uploaded = st.file_uploader(
319
  "Upload a file",
320
- type=["png", "jpg", "jpeg", "dcm", "nii", "gz"],
321
- help="Examples: single-channel PNG, 3-channel grayscale PNG, DICOM (.dcm), or NIfTI (.nii/.nii.gz).",
 
 
 
 
 
 
 
 
 
 
322
  )
323
- st.caption("Accepted examples: single-channel PNG, 3-channel grayscale PNG, DICOM (.dcm), NIfTI (.nii or .nii.gz).")
324
- control_note("Uploaded DICOM and NIfTI files can expose native scanner intensity values, while PNG/JPG files are usually already display-ready images.")
325
  if uploaded is not None:
326
  try:
327
  loaded = load_uploaded_file(uploaded)
 
265
  current_slice_index = int(st.session_state["slice_index"])
266
  current_image = loaded.volume[current_slice_index]
267
 
268
+ if source_mode == "Upload a file" and loaded.source_kind in {"dicom"}:
269
  st.radio(
270
  "Rotate uploaded image",
271
  options=list(ROTATION_OPTIONS.keys()),
272
  key="rotation_choice",
273
  horizontal=False,
274
  )
275
+ control_note(
276
+ "If an uploaded DICOM slice looks sideways, use rotation to fix its viewing orientation before comparing outputs."
277
+ )
278
  rotation_k = ROTATION_OPTIONS[st.session_state["rotation_choice"]]
279
  if rotation_k:
280
  current_image = np.rot90(current_image, k=rotation_k)
 
319
  else:
320
  uploaded = st.file_uploader(
321
  "Upload a file",
322
+ type=["png", "jpg", "jpeg", "dcm"],
323
+ help=(
324
+ "Upload a 2D PNG/JPG image or a single-slice DICOM (.dcm) file. "
325
+ "Large 3D NIfTI volumes are intentionally not supported in this online demo."
326
+ ),
327
+ )
328
+ st.caption(
329
+ "Accepted examples: PNG/JPG display images or single-slice DICOM (.dcm). "
330
+ "NIfTI (.nii/.nii.gz) volumes are not supported for reliability on Hugging Face Spaces."
331
+ )
332
+ control_note(
333
+ "Uploaded DICOM files can expose scanner-like intensity values, while PNG/JPG files are usually already display-ready 8-bit images."
334
  )
 
 
335
  if uploaded is not None:
336
  try:
337
  loaded = load_uploaded_file(uploaded)
utils.py CHANGED
@@ -231,24 +231,43 @@ def load_local_path(path: Path) -> LoadedImage:
231
  )
232
 
233
 
234
- def pil_to_grayscale_array(pil_image: Image.Image) -> tuple[np.ndarray, str]:
235
  array = np.asarray(pil_image)
 
236
  if array.size == 0:
237
  raise ValueError("The uploaded image contains no pixel data.")
238
 
 
239
  if array.ndim == 2:
240
- return _ensure_non_empty_2d_image(array, "Uploaded image"), "Loaded a single-channel grayscale image."
241
 
 
242
  if array.ndim == 3 and array.shape[2] >= 3:
243
  rgb = array[..., :3]
244
- if np.all(rgb[..., 0] == rgb[..., 1]) and np.all(rgb[..., 1] == rgb[..., 2]):
245
- return _ensure_non_empty_2d_image(rgb[..., 0], "Uploaded image"), "Detected a 3-channel grayscale image and collapsed it to one channel."
246
 
247
- gray = 0.299 * rgb[..., 0] + 0.587 * rgb[..., 1] + 0.114 * rgb[..., 2]
248
- return _ensure_non_empty_2d_image(gray, "Uploaded image"), "Converted an RGB image to grayscale for this grayscale-only demo."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
 
250
- raise ValueError("Unsupported image shape. Please upload a standard grayscale or RGB image.")
 
 
251
 
 
252
 
253
  def infer_bit_depth_from_array(array: np.ndarray) -> int:
254
  dtype = np.asarray(array).dtype
 
231
  )
232
 
233
 
234
+ def pil_to_grayscale_array(pil_image):
235
  array = np.asarray(pil_image)
236
+
237
  if array.size == 0:
238
  raise ValueError("The uploaded image contains no pixel data.")
239
 
240
+ # Already single-channel
241
  if array.ndim == 2:
242
+ return array, f"Loaded a single-channel grayscale image with dtype {array.dtype}."
243
 
244
+ # RGB or RGBA
245
  if array.ndim == 3 and array.shape[2] >= 3:
246
  rgb = array[..., :3]
 
 
247
 
248
+ # Case 1: 3-channel grayscale image
249
+ if np.array_equal(rgb[..., 0], rgb[..., 1]) and np.array_equal(rgb[..., 1], rgb[..., 2]):
250
+ gray = rgb[..., 0]
251
+ return gray, "Detected a 3-channel grayscale image and collapsed it to one channel."
252
+
253
+ # Case 2: true RGB image
254
+ rgb_float = rgb.astype(np.float32)
255
+ gray_float = (
256
+ 0.299 * rgb_float[..., 0]
257
+ + 0.587 * rgb_float[..., 1]
258
+ + 0.114 * rgb_float[..., 2]
259
+ )
260
+
261
+ # For ordinary 8-bit PNG/JPG, return 8-bit grayscale
262
+ if rgb.dtype == np.uint8:
263
+ gray = np.clip(np.rint(gray_float), 0, 255).astype(np.uint8)
264
+ return gray, "Converted an 8-bit RGB image to one 8-bit luminance grayscale channel."
265
 
266
+ # For unusual higher-bit RGB images, preserve numeric range but avoid fake precision if possible
267
+ gray = gray_float.astype(np.float32)
268
+ return gray, f"Converted an RGB image with dtype {rgb.dtype} to one grayscale channel."
269
 
270
+ raise ValueError("Unsupported image shape. Please upload a grayscale or RGB image.")
271
 
272
  def infer_bit_depth_from_array(array: np.ndarray) -> int:
273
  dtype = np.asarray(array).dtype