Skier8402 commited on
Commit
485c4fe
·
verified ·
1 Parent(s): e58811e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -65
app.py CHANGED
@@ -4,82 +4,87 @@ import pandas as pd
4
  import numpy as np
5
  import io
6
  import os
7
- from pathlib import Path
8
 
9
  def zoom_at(img, x, y, zoom):
10
- # ...existing code...
 
 
 
 
11
 
12
- # App title and description
13
  st.set_page_config(page_title="Cell Explorer", layout="wide")
14
- st.title("CLL Explorer: Annotation Tool")
15
 
16
  st.markdown("""
17
- ### About this Application
18
- This tool helps researchers analyze microscope images of blood cells for malaria detection:
19
- - Upload microscope images
20
- - Adjust image view with zoom and enhancement controls
21
- - Detect and measure cells automatically
22
- - Save analysis results and annotations
23
-
24
- **Note**: Cell measurements are in micrometers (µm)
25
  """)
26
 
27
- # Create tabs for different functions
28
- tab1, tab2, tab3 = st.tabs(["Image Analysis", "Detection Results", "Settings"])
29
-
30
- with tab1:
31
- col1, col2 = st.columns([2,1])
32
 
33
- with col1:
34
- uploaded_files = st.file_uploader("Upload Images", accept_multiple_files=True, type=["jpg", "png"])
 
 
35
 
36
- if uploaded_files:
37
- img_index = st.selectbox("Select Image", range(len(uploaded_files)))
38
- img_data = uploaded_files[img_index].read()
39
- img = Image.open(io.BytesIO(img_data)).resize((800, 800))
40
 
41
- st.image(img, caption="Original Image", use_column_width=True)
 
42
 
43
  with col2:
44
- if uploaded_files:
45
- st.subheader("Image Controls")
46
- x = st.slider("X Position", 0, 800, 400)
47
- y = st.slider("Y Position", 0, 800, 400)
48
- zoom = st.slider("Zoom Level", 1, 10, 5)
49
-
50
- with st.expander("Enhancement Settings"):
51
- contrast = st.slider("Contrast", 0.0, 5.0, 1.0)
52
- brightness = st.slider("Brightness", 0.0, 5.0, 1.0)
53
- sharpness = st.slider("Sharpness", 0.0, 2.0, 1.0)
54
-
55
- img_zoomed = zoom_at(img, x, y, zoom)
56
- img_processed = ImageEnhance.Contrast(img_zoomed).enhance(contrast)
57
- img_processed = ImageEnhance.Brightness(img_processed).enhance(brightness)
58
- img_processed = ImageEnhance.Sharpness(img_processed).enhance(sharpness)
59
-
60
- st.image(img_processed, caption="Processed View", use_column_width=True)
61
-
62
- with tab2:
63
- if uploaded_files:
64
- st.subheader("Cell Detection Results")
65
- # Add your existing cell detection code here
66
-
67
- col1, col2 = st.columns(2)
68
- with col1:
69
- description = st.text_area("Analysis Notes", "")
70
- with col2:
71
- if st.button("Save Analysis"):
72
- timestamp = pd.Timestamp.now().strftime("%Y%m%d_%H%M%S")
73
- save_dir = Path("analysis_results") / timestamp
74
- save_dir.mkdir(parents=True, exist_ok=True)
75
-
76
- img_processed.save(save_dir / "processed_image.jpg")
77
- with open(save_dir / "analysis_notes.txt", "w") as f:
78
  f.write(description)
79
- st.success(f"Analysis saved to {save_dir}")
80
-
81
- with tab3:
82
- st.subheader("Application Settings")
83
- st.checkbox("Enable Auto-Detection", value=True)
84
- st.selectbox("Measurement Unit", ["Micrometers", "Pixels"])
85
- st.number_input("Detection Confidence Threshold", 0.0, 1.0, 0.5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import numpy as np
5
  import io
6
  import os
 
7
 
8
  def zoom_at(img, x, y, zoom):
9
+ w, h = img.size
10
+ zoom2 = zoom * 2
11
+ img = img.crop((x - w / zoom2, y - h / zoom2,
12
+ x + w / zoom2, y + h / zoom2))
13
+ return img.resize((w, h), Image.LANCZOS)
14
 
 
15
  st.set_page_config(page_title="Cell Explorer", layout="wide")
16
+ st.title("CLL Explorer - Annotation Tool")
17
 
18
  st.markdown("""
19
+ ### About This Application
20
+ This tool assists researchers in analyzing microscope images of blood cells for malaria detection:
21
+ - **Upload** microscope images.
22
+ - **Adjust** image view with zoom and enhancement controls.
23
+ - **Detect** and measure cells automatically.
24
+ - **Save** analysis results and annotations.
 
 
25
  """)
26
 
27
+ uploaded_files = st.file_uploader("Upload Images", accept_multiple_files=True, type=["jpg", "png"])
 
 
 
 
28
 
29
+ if uploaded_files:
30
+ img_index = st.selectbox("Select Image", range(len(uploaded_files)), format_func=lambda x: uploaded_files[x].name)
31
+ img_data = uploaded_files[img_index].read()
32
+ img = Image.open(io.BytesIO(img_data)).resize((500, 500))
33
 
34
+ col1, col2 = st.columns([3, 2])
 
 
 
35
 
36
+ with col1:
37
+ st.image(img, caption="Uploaded Image", use_column_width=True)
38
 
39
  with col2:
40
+ st.subheader("Image Controls")
41
+ x = st.slider("X Coordinate", 0, 500, 250)
42
+ y = st.slider("Y Coordinate", 0, 500, 250)
43
+ zoom = st.slider("Zoom", 1, 10, 5)
44
+
45
+ with st.expander("Enhancement Settings"):
46
+ contrast = st.slider("Contrast", 0.0, 5.0, 1.0)
47
+ brightness = st.slider("Brightness", 0.0, 5.0, 1.0)
48
+ sharpness = st.slider("Sharpness", 0.0, 2.0, 1.0)
49
+
50
+ processed_col, actions_col = st.columns([3, 2])
51
+
52
+ with processed_col:
53
+ img_zoomed = zoom_at(img, x, y, zoom)
54
+ img_contrast = ImageEnhance.Contrast(img_zoomed).enhance(contrast)
55
+ img_bright = ImageEnhance.Brightness(img_contrast).enhance(brightness)
56
+ img_sharp = ImageEnhance.Sharpness(img_bright).enhance(sharpness)
57
+ st.image(img_sharp, caption="Processed Image", use_column_width=True)
58
+
59
+ with actions_col:
60
+ save_image = st.checkbox("Save Image")
61
+ if save_image:
62
+ img_sharp.save("image-processed.jpg")
63
+ st.success("Image saved as `image-processed.jpg`")
64
+
65
+ with st.expander("Save Options"):
66
+ description = st.text_area("Describe the image", "")
67
+ if st.button("Save Description"):
68
+ with open("saved_image_description.txt", "w") as f:
 
 
 
 
 
69
  f.write(description)
70
+ st.success("Description saved as `saved_image_description.txt`")
71
+
72
+ if st.button("Save Image Parameters"):
73
+ params = {
74
+ "coordinates_x": x,
75
+ "coordinates_y": y,
76
+ "zoom": zoom,
77
+ "contrast": contrast,
78
+ "brightness": brightness,
79
+ "sharpness": sharpness
80
+ }
81
+ with open("saved_image_parameters.json", "w") as f:
82
+ f.write(pd.DataFrame([params]).to_json(orient="records"))
83
+ st.success("Image parameters saved as `saved_image_parameters.json`")
84
+
85
+ if st.button("Rename Files"):
86
+ file_ext = str(np.random.randint(100))
87
+ os.rename("image-processed.jpg", f"img_processed{file_ext}.jpg")
88
+ os.rename("saved_image_parameters.json", f"saved_image_parameters{file_ext}.json")
89
+ os.rename("saved_image_description.txt", f"saved_image_description{file_ext}.txt")
90
+ st.success("Files renamed successfully")