박선진

add fire detection script

tested with local images of unknown person and fire
{
"labels" : [
"fire"
],
"anchors" : [
[
129,
208,
183,
155,
198,
259
],
[
73,
140,
109,
80,
119,
131
],
[
22,
31,
31,
57,
54,
80
]
]
}
\ No newline at end of file
import process
import base64
if __name__ == '__main__':
p = process.Process()
p.ProcessImage()
\ No newline at end of file
# image to base64 encoding
# you have to set 'encoded_image' to frame code
with open("./known_images/obama.jpg", "rb") as img_file:
encoded_obama_image = base64.b64encode(img_file.read())
with open("./test_images/Fire.jpg", "rb") as img_file:
encoded_fire_image = base64.b64encode(img_file.read())
with open("./test_images/unknown.jpg", "rb") as img_file:
encoded_unknown_image = base64.b64encode(img_file.read())
p.ProcessImage(encoded_obama_image)
p.ProcessImage(encoded_fire_image)
p.ProcessImage(encoded_unknown_image)
\ No newline at end of file
......
from imageai.Detection.Custom import CustomObjectDetection, CustomVideoObjectDetection
import base64
import face_recognition
import numpy as np
from cv2 import cv2
import os
import json
import sys
class Process:
def __init__(self):
known_path = './images/' # directory path of known faces
known_path = './known_images/' # directory path of known faces
image_format = 'jpg'
known_list = os.listdir(known_path)
......@@ -18,11 +21,17 @@ class Process:
known_img = face_recognition.load_image_file(known_path+f)
known_img_encoding = face_recognition.face_encodings(known_img)[0]
self.known_faces.append(known_img_encoding)
def ProcessImage(self):
# image to base64 encoding
# you have to set 'encoded_image' to frame code
with open("./images/obama.jpg", "rb") as img_file:
encoded_image = base64.b64encode(img_file.read())
self.execution_path = os.getcwd()
self.detector = CustomObjectDetection()
self.detector.setModelTypeAsYOLOv3()
self.detector.setModelPath(detection_model_path=os.path.join(self.execution_path, "detection_model-ex-33--loss-4.97.h5"))
self.detector.setJsonPath(configuration_json=os.path.join(self.execution_path, "detection_config.json"))
self.detector.loadModel()
def ProcessImage(self, data):
encoded_image = data
#base64 to image(uint8) decoding
img64_decode = base64.b64decode(encoded_image)
......@@ -30,8 +39,9 @@ class Process:
decoded_img = cv2.imdecode(im_arr, flags=cv2.IMREAD_COLOR)
face = self.FaceRecognition(decoded_img) #True면 침입자 발생
fire = self.FireDetection(decoded_img) #True면 화재발생
result= {"unknown_person" : face}
result= {"unknown_person" : face, "fire_broken" : fire}
print(result)
return result
def FaceRecognition(self, decoded_img):
......@@ -44,4 +54,18 @@ class Process:
return not True in results
except IndexError:
print("얼굴없음")
return False
\ No newline at end of file
return False
def FireDetection(self, decoded_img):
detections = self.detector.detectObjectsFromImage(input_image=decoded_img, input_type="array",
output_image_path=os.path.join(self.execution_path, "fire_detected.jpg"),
minimum_percentage_probability=40)
if len(detections) == 0 : fire_broken = False
else : fire_broken = True
return fire_broken
'''
for detection in detections:
print(detection["name"], " : ", detection["percentage_probability"], " : ", detection["box_points"])
'''
\ No newline at end of file
......