Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# .h5 파일을 이용한 모델 로드
|
| 5 |
+
model = tf.keras.models.load_model('path_to_your_model.h5')
|
| 6 |
+
|
| 7 |
+
# 이미지 전처리 함수
|
| 8 |
+
def preprocess_image(image):
|
| 9 |
+
image = tf.image.resize(image, [224, 224]) # 모델에 맞게 이미지 크기 조정
|
| 10 |
+
image = tf.cast(image, tf.float32) / 255.0 # 정규화
|
| 11 |
+
image = tf.expand_dims(image, axis=0) # 배치 차원 추가
|
| 12 |
+
return image
|
| 13 |
+
|
| 14 |
+
# 예측 함수
|
| 15 |
+
def predict(image):
|
| 16 |
+
processed_image = preprocess_image(image)
|
| 17 |
+
predictions = model.predict(processed_image)
|
| 18 |
+
predicted_class = tf.argmax(predictions, axis=-1).numpy()[0]
|
| 19 |
+
return predicted_class
|
| 20 |
+
|
| 21 |
+
# Gradio 인터페이스 설정
|
| 22 |
+
interface = gr.Interface(
|
| 23 |
+
fn=predict,
|
| 24 |
+
inputs=gr.inputs.Image(source="upload", tool="editor"),
|
| 25 |
+
outputs="label",
|
| 26 |
+
live=True
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Gradio 웹 인터페이스 실행
|
| 30 |
+
if __name__ == "__main__":
|
| 31 |
+
interface.launch()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
import tensorflow as tf
|
| 35 |
+
import gradio as gr
|
| 36 |
+
|
| 37 |
+
# .h5 파일을 이용한 모델 로드
|
| 38 |
+
model = tf.keras.models.load_model('path_to_your_model.h5')
|
| 39 |
+
|
| 40 |
+
# 이미지 전처리 함수
|
| 41 |
+
def preprocess_image(image):
|
| 42 |
+
image = tf.image.resize(image, [224, 224]) # 모델에 맞게 이미지 크기 조정
|
| 43 |
+
image = tf.cast(image, tf.float32) / 255.0 # 정규화
|
| 44 |
+
image = tf.expand_dims(image, axis=0) # 배치 차원 추가
|
| 45 |
+
return image
|
| 46 |
+
|
| 47 |
+
# 예측 함수
|
| 48 |
+
def predict(image):
|
| 49 |
+
processed_image = preprocess_image(image)
|
| 50 |
+
predictions = model.predict(processed_image)
|
| 51 |
+
predicted_class = tf.argmax(predictions, axis=-1).numpy()[0]
|
| 52 |
+
return predicted_class
|
| 53 |
+
|
| 54 |
+
# Gradio 인터페이스 설정
|
| 55 |
+
interface = gr.Interface(
|
| 56 |
+
fn=predict,
|
| 57 |
+
inputs=gr.inputs.Image(source="webcam", tool="editor"),
|
| 58 |
+
outputs="label",
|
| 59 |
+
live=True
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Gradio 웹 인터페이스 실행
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
interface.launch()
|