-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_eme.py
78 lines (70 loc) · 2.97 KB
/
app_eme.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import base64
import tensorflow as tf
from PIL import Image
import numpy as np
from flask import Flask, request, jsonify
from flask_cors import CORS
import io
from tensorflow import keras
from PIL import ImageOps
model = tf.keras.models.load_model('keras_model.h5')
model_eme = keras.models.load_model('keras_model_eme.h5')
app = Flask(__name__)
CORS(app)
def preprocess_image(base64_string):
image_data = base64.b64decode(base64_string)
image = Image.open(io.BytesIO(image_data))
image = image.resize((224, 224))
image_array = np.array(image) / 255.0
return np.expand_dims(image_array, axis=0)
def classify_image(image_data):
preprocessed_image = preprocess_image(image_data)
prediction = model.predict(preprocessed_image)
category_index = tf.argmax(prediction, axis=-1).numpy()[0]
threshold = 0.5
if prediction.max() < threshold:
category_index = 6
return category_index
def classify_image_eme(image_data):
preprocessed_image = preprocess_image(image_data)
prediction = model_eme.predict(preprocessed_image)
category_index = tf.argmax(prediction, axis=-1).numpy()[0]
threshold = 0.5
if prediction.max() < threshold:
category_index = 3
return category_index
@app.route('/api/eme', methods=['POST'])
def process_image_eme():
image_data = request.get_json()['image']
category_index = classify_image_eme(image_data)
if category_index == 0:
result_e = {'title': '차량 화재 긴급 신고', 'category_index': int(category_index)}
elif category_index == 1:
result_e = {'title': '건물 화재 긴급 신고', 'category_index': int(category_index)}
elif category_index == 2:
result_e = {'title': '산불 화재 긴급 신고', 'category_index': int(category_index)}
elif category_index == 3:
result_e = {'title': '교통 사고 긴급 신고', 'category_index': int(category_index)}
else:
result_e = {'title': '분류 결과 없음', 'category_index': int(category_index)}
return jsonify(result_e)
@app.route('/api/dif', methods=['POST'])
def analyze_image():
image_data = request.get_json()['image']
category_index = classify_image(image_data)
if category_index == 0:
result = {'title': '도로, 시설물 파손 신고', 'category_index': int(category_index)}
elif category_index == 1:
result = {'title': '차량 및 교통 위험 신고', 'category_index': int(category_index)}
elif category_index == 2:
result = {'title': '대기오염 신고', 'category_index': int(category_index)}
elif category_index == 3:
result = {'title': '수질 오염 신고', 'category_index': int(category_index)}
elif category_index == 4:
result = {'title': '소방 안전 신고', 'category_index': int(category_index)}
else:
result = {'title': '분류 결과 없음', 'category_index': int(category_index)}
return jsonify(result)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=1234)