File size: 2,410 Bytes
23fd2ea
 
 
e5e522f
3a1a35b
68807ee
 
d5dab40
b5e3884
8326ba9
23fd2ea
aa9d0f7
23fd2ea
f242043
23fd2ea
 
 
 
 
 
 
 
 
 
 
47ed2bd
544f6e3
8bbf6f5
47ed2bd
04197a3
47ed2bd
 
 
 
 
 
 
 
 
 
544f6e3
 
 
 
23fd2ea
 
38462dc
544f6e3
23fd2ea
f67550e
23fd2ea
5affd3e
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
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
import io
import zipfile
import base64
#Загружаем модель
loaded_model = load_model('best_model', 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(zip_bytes):
    predictions_html = "<table><tr><th>Файл</th><th>Класс</th><th>Точность предсказания</th></tr>"
    with zipfile.ZipFile(zip_bytes, 'r') as zip_ref:
        for filename in zip_ref.namelist():
            if filename.lower().endswith(('.png', '.jpg', '.jpeg','.bmp')):
                with zip_ref.open(filename) as file:
                    img_bytes = file.read()
                    img_np = np.frombuffer(img_bytes, np.uint8)
                    img = cv2.imdecode(img_np, cv2.IMREAD_COLOR)
                    img_resized = cv2.resize(img, (200, 200))
                    img_array = np.expand_dims(img_resized, axis=0)
                    prediction = loaded_model.predict(img_array)
                    predicted_class = np.argmax(prediction)
                    decoded_class = label_encoder.inverse_transform([predicted_class])[0]
                    predicted_class_translation = translator(decoded_class)
                    accuracy = round(((np.max(prediction).item())*100),2)
                    predictions_html += f"<tr><th>{filename}</th><th>{predicted_class_translation}</th><th>{accuracy}%</th></tr>"
    predictions_html += "</table>"
    return predictions_html
interface = gr.Interface(
  fn=predict,
  inputs=gr.File(label="Выберите zip-файл (только с изображениями)"),
  outputs=gr.HTML(label="Предсказанные классы в таблице"),
  title="Нейронная сеть для классификации брака на металлопрокате"
) 

interface.launch(share=True)