Skier8402 commited on
Commit
e58811e
·
verified ·
1 Parent(s): 5ce3bb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -58
app.py CHANGED
@@ -4,63 +4,82 @@ import pandas as pd
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.title("Cell Explorer")
16
-
17
- uploaded_files = st.file_uploader("Upload Images", accept_multiple_files=True, type="jpg")
18
-
19
- if uploaded_files:
20
- img_index = st.selectbox("Select Image", range(len(uploaded_files)))
21
- img_data = uploaded_files[img_index].read()
22
- img = Image.open(io.BytesIO(img_data)).resize((500, 500))
23
- x = st.slider("X Coordinate", 0, 500, 205)
24
- y = st.slider("Y Coordinate", 0, 500, 250)
25
- zoom = st.slider("Zoom", 1, 10, 5)
26
- contrast = st.slider("Contrast", 0.0, 5.0, 1.0)
27
- brightness = st.slider("Brightness", 0.0, 5.0, 1.0)
28
- sharpness = st.slider("Sharpness", 0.0, 2.0, 1.0)
29
-
30
- img_zoomed = zoom_at(img, x, y, zoom)
31
- img_contrast = ImageEnhance.Contrast(img_zoomed).enhance(contrast)
32
- img_bright = ImageEnhance.Brightness(img_contrast).enhance(brightness)
33
- img_sharp = ImageEnhance.Sharpness(img_bright).enhance(sharpness)
34
-
35
- st.image(img_sharp, caption="Processed Image", use_column_width=True)
36
-
37
- save_image = st.checkbox("Save Image")
38
- if save_image:
39
- img_sharp.save("image-processed.jpg")
40
- st.success("Image saved as image-processed.jpg")
41
-
42
- description = st.text_area("Describe the image", "")
43
- if st.button("Save Description"):
44
- with open("saved_image_description.txt", "w") as f:
45
- f.write(description)
46
- st.success("Description saved as saved_image_description.txt")
47
-
48
- if st.button("Save Image Parameters"):
49
- params = {
50
- "coordinates_x": x,
51
- "coordinates_y": y,
52
- "zoom": zoom,
53
- "contrast": contrast,
54
- "brightness": brightness,
55
- "sharpness": sharpness
56
- }
57
- with open("saved_image_parameters.json", "w") as f:
58
- f.write(pd.DataFrame([params]).to_json(orient="records"))
59
- st.success("Image parameters saved as saved_image_parameters.json")
60
-
61
- if st.button("Rename Files"):
62
- file_ext = str(np.random.randint(100))
63
- os.rename("image-processed.jpg", f"img_processed{file_ext}.jpg")
64
- os.rename("saved_image_parameters.json", f"saved_image_parameters{file_ext}.json")
65
- os.rename("saved_image_description.txt", f"saved_image_description{file_ext}.txt")
66
- st.success("Files renamed successfully")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)