박선진

add fire detection script

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