from keras.models import load_model import numpy as np from PIL import Image import cv2 import os import gradio as gr weight_path = r'basic.h5' #权重文件 model = load_model(weight_path) labels=['天气:小雨, 降雨强度(mm/min):0.8, 能见度(m):500, 道路摩擦系数:0.64, 限速(km/h):80', '天气:中雨, 降雨强度(mm/min):1.2, 能见度(m):250, 道路摩擦系数:0.58, 限速(km/h):50', '天气:大雨, 降雨强度(mm/min):1.6, 能见度(m):50-150, 道路摩擦系数:0.45, 限速(km/h):30', '天气:薄雾, 降雨强度(mm/min):0, 能见度(m):500:, 道路摩擦系数:0.8, 限速(km/h):110', '天气:大雾, 降雨强度(mm/min):0, 能见度(m):200, 道路摩擦系数:0.8, 限速(km/h):70', '天气:浓雾, 降雨强度(mm/min):0:, 能见度(m):50-100, 道路摩擦系数:0.8, 限速(km/h):40', '天气:晴, 降雨强度(mm/min):0, 能见度(m):>1000:, 道路摩擦系数:0.8, 限速(km/h):120'] def classify_image(inp): inp = inp.resize((256,256), Image.ANTIALIAS) #缩放到事先指定的大小 inp = np.expand_dims(inp, axis=0) prediction = model.predict(inp) confidences = {labels[i]: float(prediction[0][i]) for i in range(7)} return confidences gr.Interface( fn=classify_image, inputs=gr.Image(type="pil",shape=(256,256)), outputs=gr.Label(num_top_classes=1), examples=["sunny.jpg"], interpretation="default",cache_examples=True,title="恶劣天气图像识别与预警" ).launch(enable_queue=True)