Bharatmali999 commited on
Commit
0f44fe5
·
verified ·
1 Parent(s): 4778c0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -30
app.py CHANGED
@@ -1,45 +1,35 @@
1
  import gradio as gr
2
- import torch
 
3
  from PIL import Image
4
- import torchvision
5
- from torchvision import transforms
6
- from analyzer import analyze_image
7
 
8
- # Load the pre-trained model (adjust based on your model)
9
- model = torch.load('cell_detection_model.pth')
10
- model.eval()
11
 
12
  def analyze_image(uploaded_file):
13
  try:
14
- # Convert uploaded image to a format that the model can process
15
- image = Image.open(uploaded_file)
16
- transform = transforms.Compose([
17
- transforms.ToTensor(),
18
- ])
19
- image_tensor = transform(image).unsqueeze(0) # Add batch dimension
20
-
21
- # Run inference with the model
22
- with torch.no_grad():
23
- prediction = model(image_tensor)
24
-
25
- # Get the number of cells (boxes detected)
26
- cell_count = len(prediction[0]['masks']) # Mask prediction provides the number of cells
27
-
28
- # Annotate image with predictions
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."