dev-deg commited on
Commit
204dc88
·
1 Parent(s): d95ba35

Major re-write to significantly improve UI

Browse files
Files changed (2) hide show
  1. app.py +4 -3
  2. inferencing.py +17 -14
app.py CHANGED
@@ -1,10 +1,11 @@
 
1
  import gradio as gr
2
- from inferencing import process_image
3
 
4
  iface = gr.Interface(
5
- fn=process_image,
6
  inputs="image",
7
- outputs=["image", "dataframe", "dataframe","dataframe"],
8
  title="AMBULANT Benthic Identification System"
9
  )
10
  iface.launch()
 
1
+ import gradio
2
  import gradio as gr
3
+ from inferencing import run_inference
4
 
5
  iface = gr.Interface(
6
+ fn=run_inference,
7
  inputs="image",
8
+ outputs=[gradio.Annotatedimage(label="Annotated Image"),gradio.DataFrame(label="Predicted Benthic Habitat",headers=["Prediction", "Confidence"]),gradio.DataFrame(label="Habitat Probabilities",headers=["Classification", "Confidence"])],
9
  title="AMBULANT Benthic Identification System"
10
  )
11
  iface.launch()
inferencing.py CHANGED
@@ -9,36 +9,39 @@ import dotenv
9
  dotenv.load_dotenv()
10
  hf_tk = os.getenv('HF_AT')
11
 
12
- # Function to display detections and segmentation masks
13
  def display_detections(frame, results):
14
- detections = []
 
15
  for result in results:
16
  if result.boxes.data.nelement() != 0:
17
  for detection in result.boxes.data:
18
  x1, y1, x2, y2, conf, cls = detection.cpu().numpy()
19
  class_name = result.names[int(cls)]
20
- detections.append([class_name, conf, x1, y1, x2, y2])
 
 
21
  outframe = results[0].plot()
22
- return outframe, detections
23
 
24
  print("No detections")
25
- return frame, detections
26
-
27
 
28
  def process_image(input_image):
29
  results = model(input_image)
30
- processed_frame, detections = display_detections(input_image, results)
31
 
32
- if not detections: # Check if detections list is empty
 
33
  inconclusive_res = get_inconclusive()
34
- return processed_frame, pd.DataFrame({'Message': ['No classes detected']}), inconclusive_res[0], inconclusive_res[1]
35
  else:
36
- df = pd.DataFrame(detections, columns=["Class", "Confidence", "X1", "Y1", "X2", "Y2"])
37
- habitat_res = classify_habitat(df['Class'].tolist())
38
- return processed_frame, df[['Class', 'Confidence']], habitat_res[0], habitat_res[1]
39
-
40
-
41
 
 
 
 
 
42
  # Load YOLO model
43
  raw_model = hf_hub_download(repo_id="dev-deg/Ambulant_1.0", filename="latest.pt",use_auth_token=hf_tk)
44
  model = YOLO(raw_model)
 
9
  dotenv.load_dotenv()
10
  hf_tk = os.getenv('HF_AT')
11
 
 
12
  def display_detections(frame, results):
13
+ class_names = []
14
+ annotations = []
15
  for result in results:
16
  if result.boxes.data.nelement() != 0:
17
  for detection in result.boxes.data:
18
  x1, y1, x2, y2, conf, cls = detection.cpu().numpy()
19
  class_name = result.names[int(cls)]
20
+ if class_name not in class_names:
21
+ class_names.append(class_name)
22
+ annotations.append(((int(x1), int(y1), int(x2), int(y2)), class_name))
23
  outframe = results[0].plot()
24
+ return outframe, annotations, class_names
25
 
26
  print("No detections")
27
+ return frame, [], []
 
28
 
29
  def process_image(input_image):
30
  results = model(input_image)
31
+ return display_detections(input_image, results)
32
 
33
+ def get_habitat_data(class_names):
34
+ if not class_names:
35
  inconclusive_res = get_inconclusive()
36
+ return inconclusive_res[0], inconclusive_res[1]
37
  else:
38
+ habitat_res = classify_habitat(class_names)
39
+ return habitat_res[0], habitat_res[1]
 
 
 
40
 
41
+ def run_inference(input_image):
42
+ processed_frame, annotations, class_names = process_image(input_image)
43
+ habitat, probabilities = get_habitat_data(class_names)
44
+ return (processed_frame, annotations), habitat, probabilities
45
  # Load YOLO model
46
  raw_model = hf_hub_download(repo_id="dev-deg/Ambulant_1.0", filename="latest.pt",use_auth_token=hf_tk)
47
  model = YOLO(raw_model)