Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
-
import torchvision
|
| 5 |
-
from torchvision import transforms
|
| 6 |
-
from analyzer import analyze_image
|
| 7 |
|
| 8 |
-
|
| 9 |
-
model = torch.load('cell_detection_model.pth')
|
| 10 |
-
model.eval()
|
| 11 |
|
| 12 |
def analyze_image(uploaded_file):
|
| 13 |
try:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
annotated_image = image.copy()
|
| 30 |
-
draw = ImageDraw.Draw(annotated_image)
|
| 31 |
-
for box in prediction[0]['boxes']:
|
| 32 |
-
draw.rectangle([box[0], box[1], box[2], box[3]], outline="red")
|
| 33 |
-
|
| 34 |
-
# Return the annotated image and the cell count
|
| 35 |
-
return annotated_image, f"Total Cells Counted: {cell_count}"
|
| 36 |
|
| 37 |
except Exception as e:
|
|
|
|
| 38 |
return None, f"Error analyzing image: {e}"
|
| 39 |
|
| 40 |
iface = gr.Interface(
|
| 41 |
fn=analyze_image,
|
| 42 |
-
inputs=gr.File(file_types=[".png", ".jpg", ".jpeg"]),
|
| 43 |
outputs=[gr.Image(type="pil"), gr.Textbox()],
|
| 44 |
title="Microscope Image Analyzer",
|
| 45 |
description="Upload a microscope image (PNG or JPG) to detect and count cells."
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from analyzer import analyze_cells
|
| 3 |
+
import logging
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def analyze_image(uploaded_file):
|
| 9 |
try:
|
| 10 |
+
if uploaded_file is None:
|
| 11 |
+
return None, "No file uploaded."
|
| 12 |
+
|
| 13 |
+
# uploaded_file is a file path string
|
| 14 |
+
image_path = uploaded_file
|
| 15 |
+
|
| 16 |
+
logging.debug(f"Received image path: {image_path}")
|
| 17 |
+
|
| 18 |
+
# Analyze the image using your analyzer function
|
| 19 |
+
count, annotated_path = analyze_cells(image_path)
|
| 20 |
+
|
| 21 |
+
# Open the annotated image for display
|
| 22 |
+
annotated_image = Image.open(annotated_path)
|
| 23 |
+
|
| 24 |
+
return annotated_image, f"Total Cells Counted: {count}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
except Exception as e:
|
| 27 |
+
logging.error(f"Error during image analysis: {e}")
|
| 28 |
return None, f"Error analyzing image: {e}"
|
| 29 |
|
| 30 |
iface = gr.Interface(
|
| 31 |
fn=analyze_image,
|
| 32 |
+
inputs=gr.File(file_types=[".png", ".jpg", ".jpeg"], type="filepath"),
|
| 33 |
outputs=[gr.Image(type="pil"), gr.Textbox()],
|
| 34 |
title="Microscope Image Analyzer",
|
| 35 |
description="Upload a microscope image (PNG or JPG) to detect and count cells."
|