phiaaa commited on
Commit
ffc503e
·
verified ·
1 Parent(s): af3260e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -0
app.py CHANGED
@@ -21,6 +21,17 @@ def preprocess_image(img: Image.Image):
21
 
22
  def classify_image(image):
23
  try:
 
 
 
 
 
 
 
 
 
 
 
24
  input_data = preprocess_image(image)
25
  interpreter.set_tensor(input_details[0]['index'], input_data)
26
  interpreter.invoke()
@@ -28,9 +39,11 @@ def classify_image(image):
28
  results = {labels[i]: float(output_data[i]) for i in range(len(labels))}
29
  sorted_results = dict(sorted(results.items(), key=lambda x: x[1], reverse=True))
30
  return sorted_results
 
31
  except Exception as e:
32
  return {"error": str(e)}
33
 
 
34
  demo = gr.Interface(
35
  fn=classify_image,
36
  inputs=gr.Image(type="pil", label="Upload stool image"),
 
21
 
22
  def classify_image(image):
23
  try:
24
+ img = image.convert("RGB")
25
+ arr = np.asarray(img).astype(np.float32)
26
+ brightness = arr.mean()
27
+ contrast = arr.std()
28
+
29
+ # 🧠 Simple sanity check for stool-like features (brownish tone + moderate contrast)
30
+ # You can adjust these thresholds depending on your dataset
31
+ if brightness > 220 or brightness < 20 or contrast < 25:
32
+ return {"Not stool image": 1.0}
33
+
34
+ # Normal prediction process
35
  input_data = preprocess_image(image)
36
  interpreter.set_tensor(input_details[0]['index'], input_data)
37
  interpreter.invoke()
 
39
  results = {labels[i]: float(output_data[i]) for i in range(len(labels))}
40
  sorted_results = dict(sorted(results.items(), key=lambda x: x[1], reverse=True))
41
  return sorted_results
42
+
43
  except Exception as e:
44
  return {"error": str(e)}
45
 
46
+
47
  demo = gr.Interface(
48
  fn=classify_image,
49
  inputs=gr.Image(type="pil", label="Upload stool image"),