achouffe commited on
Commit
c58ca54
·
verified ·
1 Parent(s): c77fef7

feat: implement interface for forest fire detector ML model

Browse files
Files changed (1) hide show
  1. app.py +91 -3
app.py CHANGED
@@ -1,9 +1,97 @@
 
 
 
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
 
 
3
 
4
- def greet(name):
5
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
 
6
 
 
 
 
 
 
 
 
 
 
7
 
8
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
9
  demo.launch()
 
1
+ """
2
+ Gradio app to showcase the pyronear model for early forest fire detection.
3
+ """
4
+
5
+ from pathlib import Path
6
+ from typing import Tuple
7
+
8
  import gradio as gr
9
+ import numpy as np
10
+ from PIL import Image
11
+ from ultralytics import YOLO
12
+
13
+
14
+ def bgr_to_rgb(a: np.ndarray) -> np.ndarray:
15
+ """
16
+ Turn a BGR numpy array into a RGB numpy array when the array `a` represents
17
+ an image.
18
+ """
19
+ return a[:, :, ::-1]
20
+
21
+
22
+ def prediction_to_str(yolo_prediction) -> str:
23
+ """
24
+ Turn the yolo_prediction into a human friendly string.
25
+ """
26
+ boxes = yolo_prediction.boxes
27
+ return f"""{len(boxes.conf)} fire smoke(s) detected!\nwith bounding boxes (xyxy): {boxes.xyxy.cpu().numpy()}\nand confidence scores: {boxes.conf.cpu().numpy()}"""
28
+
29
+
30
+ def predict(model: YOLO, pil_image: Image.Image) -> Tuple[Image.Image, str]:
31
+ """
32
+ Main interface function that runs the model on the provided pil_image and
33
+ returns the exepected tuple to populate the gradio interface.
34
+
35
+ Args:
36
+ model (YOLO): Loaded ultralytics YOLO model.
37
+ pil_image (PIL): image to run inference on.
38
+
39
+ Returns:
40
+ pil_image_with_prediction (PIL): image with prediction from the model.
41
+ raw_prediction_str (str): string representing the raw prediction from the
42
+ model.
43
+ """
44
+ predictions = model(pil_image)
45
+ prediction = predictions[0]
46
+ pil_image_with_prediction = Image.fromarray(bgr_to_rgb(prediction.plot()))
47
+ raw_prediction_str = prediction_to_str(prediction)
48
+
49
+ return (pil_image_with_prediction, raw_prediction_str)
50
+
51
+
52
+ def examples(dir_examples: Path) -> list[Path]:
53
+ """
54
+ List the images from the dir_examples directory.
55
+
56
+ Returns:
57
+ filepaths (list[Path]): list of image filepaths.
58
+ """
59
+ return list(dir_examples.glob("*.jpg"))
60
+
61
+
62
+ def load_model(filepath_weights: Path) -> YOLO:
63
+ """
64
+ Load the YOLO model given the filepath_weights.
65
+ """
66
+ return YOLO(filepath_weights)
67
+
68
+
69
+ # Main Gradio interface
70
 
71
+ MODEL_FILEPATH_WEIGHTS = Path("data/model/best.pt")
72
+ DIR_EXAMPLES = Path("data/images/")
73
 
74
+ with gr.Blocks() as demo:
75
+ model = load_model(MODEL_FILEPATH_WEIGHTS)
76
+ image_filepaths = examples(dir_examples=DIR_EXAMPLES)
77
+ default_value_input = Image.open(image_filepaths[1])
78
+ input = gr.Image(
79
+ value=default_value_input,
80
+ type="pil",
81
+ label="input image",
82
+ sources=["upload", "clipboard"],
83
+ )
84
+ output_image = gr.Image(type="pil", label="model prediction")
85
+ output_raw = gr.Text(label="raw prediction")
86
 
87
+ fn = lambda pil_image: predict(model=model, pil_image=pil_image)
88
+ gr.Interface(
89
+ title="ML model for early forest fire detection 🔥",
90
+ fn=fn,
91
+ inputs=input,
92
+ outputs=[output_image, output_raw],
93
+ examples=image_filepaths,
94
+ allow_flagging="never",
95
+ )
96
 
 
97
  demo.launch()