SatyamSinghal commited on
Commit
f3bd758
·
verified ·
1 Parent(s): 1351a13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -29
app.py CHANGED
@@ -5,25 +5,20 @@ import gradio as gr
5
 
6
 
7
  def detect_baseline_and_solvent_front(image):
8
- """
9
- Automatically detect the baseline and solvent front in the TLC plate image.
10
- """
11
  try:
12
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
13
  blurred = cv2.GaussianBlur(gray, (5, 5), 0)
14
  edges = cv2.Canny(blurred, 50, 150)
15
 
16
- # Sum pixel intensities along each row to find horizontal lines
17
  row_sums = edges.sum(axis=1)
18
  baseline_y = None
19
  solvent_front_y = None
20
 
21
- # Detect lines based on intensity peaks
22
  for i, value in enumerate(row_sums):
23
- if value > 50: # Threshold for detecting a line
24
  if baseline_y is None:
25
- baseline_y = i # First line is the baseline
26
- solvent_front_y = i # Last detected line is the solvent front
27
 
28
  return baseline_y, solvent_front_y
29
  except Exception as e:
@@ -32,9 +27,6 @@ def detect_baseline_and_solvent_front(image):
32
 
33
 
34
  def detect_spots(image):
35
- """
36
- Detect spots (compounds) on the TLC plate.
37
- """
38
  try:
39
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
40
  blurred = cv2.GaussianBlur(gray, (5, 5), 0)
@@ -44,7 +36,7 @@ def detect_spots(image):
44
  spots = []
45
  for contour in contours:
46
  x, y, w, h = cv2.boundingRect(contour)
47
- if w > 5 and h > 5: # Filter small noise
48
  spots.append((x, y, w, h))
49
  return spots
50
  except Exception as e:
@@ -53,9 +45,6 @@ def detect_spots(image):
53
 
54
 
55
  def calculate_rf(spots, solvent_front_y, baseline_y):
56
- """
57
- Calculate the Rf values for detected spots.
58
- """
59
  rf_values = []
60
  for _, y, _, _ in spots:
61
  distance_compound = y - baseline_y
@@ -66,29 +55,25 @@ def calculate_rf(spots, solvent_front_y, baseline_y):
66
 
67
 
68
  def annotate_image(image, spots, rf_values):
69
- """
70
- Annotate the image with detected spots and their Rf values.
71
- """
72
- annotated_image = image.copy()
73
  try:
 
74
  for (x, y, w, h), rf in zip(spots, rf_values):
75
- cv2.putText(annotated_image, f"Rf={rf:.2f}", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
 
 
 
76
  cv2.rectangle(annotated_image, (x, y), (x + w, y + h), (0, 255, 0), 2)
77
  return annotated_image
78
  except Exception as e:
79
  print(f"Error in annotate_image: {e}")
80
- return image
81
 
82
 
83
  def process_tlc(image):
84
- """
85
- Process the TLC plate image to calculate Rf values and generate a report.
86
- """
87
  try:
88
- if image is None:
89
- return "Error: No image uploaded.", None
90
 
91
- # Detect baseline and solvent front
92
  baseline_y, solvent_front_y = detect_baseline_and_solvent_front(image)
93
  if baseline_y is None or solvent_front_y is None:
94
  return "Error: Unable to detect baseline or solvent front.", None
@@ -100,6 +85,9 @@ def process_tlc(image):
100
  rf_values = calculate_rf(spots, solvent_front_y, baseline_y)
101
  annotated_image = annotate_image(image, spots, rf_values)
102
 
 
 
 
103
  report_data = {"Spot": list(range(1, len(spots) + 1)), "Rf Value": rf_values}
104
  report = pd.DataFrame(report_data).to_csv(index=False)
105
 
@@ -109,12 +97,11 @@ def process_tlc(image):
109
  return "Error: Processing failed. Check inputs and try again.", None
110
 
111
 
112
- # Gradio Interface
113
  interface = gr.Interface(
114
  fn=process_tlc,
115
  inputs=gr.Image(type="numpy", label="Upload TLC Plate Image"),
116
  outputs=[
117
- gr.Image(label="Annotated TLC Plate"),
118
  gr.File(label="Download Rf Report (CSV)"),
119
  ],
120
  title="TLC Analyzer",
 
5
 
6
 
7
  def detect_baseline_and_solvent_front(image):
 
 
 
8
  try:
9
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
10
  blurred = cv2.GaussianBlur(gray, (5, 5), 0)
11
  edges = cv2.Canny(blurred, 50, 150)
12
 
 
13
  row_sums = edges.sum(axis=1)
14
  baseline_y = None
15
  solvent_front_y = None
16
 
 
17
  for i, value in enumerate(row_sums):
18
+ if value > 50:
19
  if baseline_y is None:
20
+ baseline_y = i
21
+ solvent_front_y = i
22
 
23
  return baseline_y, solvent_front_y
24
  except Exception as e:
 
27
 
28
 
29
  def detect_spots(image):
 
 
 
30
  try:
31
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
32
  blurred = cv2.GaussianBlur(gray, (5, 5), 0)
 
36
  spots = []
37
  for contour in contours:
38
  x, y, w, h = cv2.boundingRect(contour)
39
+ if w > 5 and h > 5:
40
  spots.append((x, y, w, h))
41
  return spots
42
  except Exception as e:
 
45
 
46
 
47
  def calculate_rf(spots, solvent_front_y, baseline_y):
 
 
 
48
  rf_values = []
49
  for _, y, _, _ in spots:
50
  distance_compound = y - baseline_y
 
55
 
56
 
57
  def annotate_image(image, spots, rf_values):
 
 
 
 
58
  try:
59
+ annotated_image = image.copy()
60
  for (x, y, w, h), rf in zip(spots, rf_values):
61
+ cv2.putText(
62
+ annotated_image, f"Rf={rf:.2f}", (x, y - 10),
63
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1
64
+ )
65
  cv2.rectangle(annotated_image, (x, y), (x + w, y + h), (0, 255, 0), 2)
66
  return annotated_image
67
  except Exception as e:
68
  print(f"Error in annotate_image: {e}")
69
+ return None
70
 
71
 
72
  def process_tlc(image):
 
 
 
73
  try:
74
+ if image is None or not isinstance(image, np.ndarray):
75
+ return "Error: Invalid image uploaded. Please try again with a valid image.", None
76
 
 
77
  baseline_y, solvent_front_y = detect_baseline_and_solvent_front(image)
78
  if baseline_y is None or solvent_front_y is None:
79
  return "Error: Unable to detect baseline or solvent front.", None
 
85
  rf_values = calculate_rf(spots, solvent_front_y, baseline_y)
86
  annotated_image = annotate_image(image, spots, rf_values)
87
 
88
+ if annotated_image is None:
89
+ return "Error: Could not annotate the TLC plate image.", None
90
+
91
  report_data = {"Spot": list(range(1, len(spots) + 1)), "Rf Value": rf_values}
92
  report = pd.DataFrame(report_data).to_csv(index=False)
93
 
 
97
  return "Error: Processing failed. Check inputs and try again.", None
98
 
99
 
 
100
  interface = gr.Interface(
101
  fn=process_tlc,
102
  inputs=gr.Image(type="numpy", label="Upload TLC Plate Image"),
103
  outputs=[
104
+ gr.Image(type="numpy", label="Annotated TLC Plate"),
105
  gr.File(label="Download Rf Report (CSV)"),
106
  ],
107
  title="TLC Analyzer",