Spaces:
Sleeping
Sleeping
Download app.py from Cheomjee/TeachableMachine: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/spaces/Cheomjee/TeachableMachine/resolve/main/app.py
- Command line
-
hf download hf://spaces/Cheomjee/TeachableMachine/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Cheomjee/TeachableMachine/resolve/main/app.py
1.28 kB
| import tensorflow as tf | |
| import gradio as gr | |
| import numpy as np | |
| from PIL import Image | |
| # .h5 파일을 이용한 모델 로드 | |
| model = tf.keras.models.load_model('keras_model.h5') | |
| # 클래스 레이블 | |
| class_labels = ['disposablecup', 'envmark', 'label', 'mug', 'nonlabel', 'reusablecup'] | |
| # 이미지 전처리 함수 | |
| def preprocess_image(image): | |
| image = Image.fromarray(image) | |
| image = image.resize((224, 224)) # 모델 입력 크기에 맞게 조정 | |
| image = np.array(image) / 255.0 # 정규화 | |
| image = np.expand_dims(image, axis=0) | |
| return image | |
| # 예측 함수 | |
| def predict(image): | |
| processed_image = preprocess_image(image) | |
| predictions = model.predict(processed_image) | |
| predicted_class_index = np.argmax(predictions, axis=-1)[0] | |
| predicted_class_label = class_labels[predicted_class_index] | |
| if predicted_class_label in ['disposablecup','label'] : missionresult = '인증에 실패했습니다.' | |
| else : missionresult = '인증에 성공했습니다!' | |
| return missionresult | |
| # Gradio 인터페이스 설정 | |
| interface = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(type="numpy"), | |
| outputs=gr.Textbox(label="미션 결과"), | |
| live=True | |
| ) | |
| # Gradio 웹 인터페이스 실행 | |
| if __name__ == "__main__": | |
| interface.launch() | |