Spaces:
Build error
Build error
Download app.py from Pashtetuum/MetallClassification: direct link, hf CLI and curl.
- Browser
- Download file 1.69 kB
-
https://huggingface.co/spaces/Pashtetuum/MetallClassification/resolve/main/app.py
- Command line
-
hf download hf://spaces/Pashtetuum/MetallClassification/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Pashtetuum/MetallClassification/resolve/main/app.py
1.69 kB
| import numpy as np | |
| from tensorflow.keras.models import load_model | |
| from tensorflow.keras.preprocessing.image import load_img, img_to_array | |
| import autokeras as ak | |
| import pickle | |
| import gradio as gr | |
| import cv2 | |
| #Загружаем модель | |
| loaded_model = load_model('best_model.h5', custom_objects=ak.CUSTOM_OBJECTS) | |
| # Загружаем label_encoder | |
| with open('label_encoder.pkl', 'rb') as le_file: | |
| label_encoder = pickle.load(le_file) | |
| def translator(Defect): | |
| translation_dict = { | |
| "Crazing": "Трещины", | |
| "Inclusion": "Вкрапления", | |
| "Patches": "Пятна", | |
| "Pitted": "Рябь", | |
| "Rolled": "Замятие", | |
| "Scratches": "Царапины" | |
| } | |
| return translation_dict.get(Defect) | |
| def predict(loaded_img): | |
| # Изменение размера изображения | |
| img = cv2.resize(loaded_img, (200, 200)) | |
| # Добавление измерения пакета | |
| img_array = np.expand_dims(img, axis=0) | |
| # Предсказание класса | |
| prediction = loaded_model.predict(img_array) | |
| predicted_class = np.argmax(prediction) | |
| decoded_class = label_encoder.inverse_transform([predicted_class])[0] | |
| accuracy = round(((np.max(prediction).item())*100),2) | |
| prediction=f"Класс: {translator(decoded_class)} - Точность: {accuracy}%" | |
| return prediction | |
| interface = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(label="Загрузите изображение"), | |
| outputs=gr.Textbox(label="Классификация"), | |
| title="Нейронная сеть для классификации брака на металлопрокате" | |
| ) | |
| interface.launch(share=True) |