Skip to content

Commit 02672f3

Browse files
Add fire_classifier package.
1 parent 45106b3 commit 02672f3

File tree

8 files changed

+112
-5
lines changed

8 files changed

+112
-5
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ WORKDIR /deploy/
55
RUN apt update
66
RUN apt install -y git
77
RUN apt-get install -y libglib2.0-0
8-
RUN pip install git+https://github.com/CVxTz/FastImageClassification
8+
RUN pip install git+https://github.com/UPstartDeveloper/Fire-Detection-API
99
EXPOSE 8080
1010

1111
ENTRYPOINT uvicorn main:app --host 0.0.0.0 --port 8080 --workers 1

app/config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
resize_shape: [256, 256]
22
targets: ['Fire_Images', 'Normal_Images']
3-
model_path: "TODO"
4-
model_url: "TODO"
5-
model_sha256: "TODO"
3+
model_path: "fire_classifier.h5"
4+
model_url: "https://github.com/UPstartDeveloper/Fire-Detection-API/releases/download/v0.0.1/fire_classifier.h5"
5+
model_sha256: "b527ecea0f4786e45eb0242f263752c45fb55754"

app/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from fastapi import FastAPI, File, UploadFile
22

3-
from fast_image_classification.predictor import ImagePredictor
3+
from fire_classifier.predictor import ImagePredictor
44

55
app = FastAPI()
66

fire_classifier/__init__.py

Whitespace-only changes.

fire_classifier/predictor.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import argparse
2+
3+
import numpy as np
4+
import yaml
5+
from tensorflow.keras.models import load_model
6+
7+
from fire_classifier.preprocessing_utilities import (
8+
read_img_from_path,
9+
read_from_file,
10+
)
11+
from fire_classifier.utils import download_model
12+
13+
14+
class ImagePredictor:
15+
def __init__(self, model_path, resize_size, targets):
16+
self.model_path = model_path
17+
self.model = load_model(self.model_path)
18+
self.resize_size = resize_size
19+
self.targets = targets
20+
21+
@classmethod
22+
def init_from_config_path(cls, config_path):
23+
with open(config_path, "r") as f:
24+
config = yaml.load(f, yaml.SafeLoader)
25+
predictor = cls(
26+
model_path=config["model_path"],
27+
resize_size=config["resize_shape"],
28+
targets=config["targets"],
29+
)
30+
return predictor
31+
32+
@classmethod
33+
def init_from_config_url(cls, config_path):
34+
with open(config_path, "r") as f:
35+
config = yaml.load(f, yaml.SafeLoader)
36+
37+
download_model(
38+
config["model_url"], config["model_path"], config["model_sha256"]
39+
)
40+
41+
return cls.init_from_config_path(config_path)
42+
43+
def predict_from_array(self, arr):
44+
pred = self.model.predict(arr[np.newaxis, ...]).ravel().tolist()
45+
pred = [round(x, 3) for x in pred]
46+
return {k: v for k, v in zip(self.targets, pred)}
47+
48+
def predict_from_path(self, path):
49+
arr = read_img_from_path(path)
50+
return self.predict_from_array(arr)
51+
52+
def predict_from_file(self, file_object):
53+
arr = read_from_file(file_object)
54+
return self.predict_from_array(arr)
55+
56+
57+
if __name__ == "__main__":
58+
"""
59+
python predictor.py --predictor_config "../example/predictor_config.yaml"
60+
61+
"""
62+
parser = argparse.ArgumentParser()
63+
parser.add_argument(
64+
"--predictor_config_path",
65+
help="predictor_config_path",
66+
default="../example/predictor_config.yaml",
67+
)
68+
69+
args = parser.parse_args()
70+
71+
predictor_config_path = args.predictor_config_path
72+
73+
predictor = ImagePredictor.init_from_config_path(predictor_config_path)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import cv2
2+
import numpy as np
3+
4+
5+
def read_img_from_path(path):
6+
img = cv2.imread(path, cv2.IMREAD_COLOR)
7+
return img
8+
9+
10+
def read_from_file(file_object):
11+
arr = np.fromstring(file_object.read(), np.uint8)
12+
img_np = cv2.imdecode(arr, cv2.IMREAD_COLOR)
13+
14+
return img_np

fire_classifier/utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import hashlib
2+
import os
3+
from tensorflow.keras.utils import get_file
4+
5+
6+
def get_hash(filename):
7+
sha256_hash = hashlib.sha256()
8+
with open(filename, "rb") as f:
9+
for byte_block in iter(lambda: f.read(4096), b""):
10+
sha256_hash.update(byte_block)
11+
12+
return sha256_hash.hexdigest()
13+
14+
15+
def download_model(url, file_path, file_sha256):
16+
if os.path.exists(file_path) and get_hash(file_path) == file_sha256:
17+
print("File already exists")
18+
else:
19+
get_file(origin=url, fname=file_path, cache_dir=".", cache_subdir="")

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
numpy
22
tensorflow
33
fastapi==0.49.0
4+
opencv-python==4.5.3
45
PyYAML==5.3
56
uvicorn
67
python-multipart

0 commit comments

Comments
 (0)