358 lines
13 KiB
Python
358 lines
13 KiB
Python
|
import sys
|
|||
|
import os
|
|||
|
from pydantic import BaseModel
|
|||
|
from typing import List
|
|||
|
from fastapi import FastAPI, HTTPException
|
|||
|
import asyncio
|
|||
|
import pandas as pd
|
|||
|
import numpy as np
|
|||
|
from PIL import Image
|
|||
|
# 获取当前脚本所在目录
|
|||
|
print("Current working directory:", os.getcwd())
|
|||
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|||
|
# 添加环境变量路径
|
|||
|
sys.path.append(os.path.join(current_dir))
|
|||
|
print("Current sys.path:", sys.path)
|
|||
|
import torch
|
|||
|
from dimaoshibie import segformer
|
|||
|
from wudingpv.taihuyuan_roof.manet.model.resunet import resUnetpamcarb as roof_resUnetpamcarb
|
|||
|
from wudingpv.predictandeval_util import segmentation
|
|||
|
from guangfufadian import model_base as guangfufadian_model_base
|
|||
|
from fenglifadian import model_base as fenglifadian_model_base
|
|||
|
from work_util import prepare_data,model_deal,params,data_util,post_model
|
|||
|
from work_util.logger import logger
|
|||
|
import joblib
|
|||
|
def get_roof_model():
|
|||
|
model_roof = roof_resUnetpamcarb()
|
|||
|
model_path_roof = os.path.join(current_dir,'wudingpv/models/roof_best.pth')
|
|||
|
model_dict_roof = torch.load(model_path_roof, map_location=torch.device('cpu'))
|
|||
|
model_roof.load_state_dict(model_dict_roof['net'])
|
|||
|
logger.info("屋顶识别权重加载成功")
|
|||
|
model_roof.eval()
|
|||
|
model_roof.cuda()
|
|||
|
return model_roof
|
|||
|
|
|||
|
def get_pv_model():
|
|||
|
model_roof = roof_resUnetpamcarb()
|
|||
|
model_path_roof = os.path.join(current_dir,'wudingpv/models/pv_best.pth')
|
|||
|
model_dict_roof = torch.load(model_path_roof, map_location=torch.device('cpu'))
|
|||
|
model_roof.load_state_dict(model_dict_roof['net'])
|
|||
|
logger.info("屋顶识别权重加载成功")
|
|||
|
model_roof.eval()
|
|||
|
model_roof.cuda()
|
|||
|
return model_roof
|
|||
|
|
|||
|
|
|||
|
# 初始化 FastAPI
|
|||
|
app = FastAPI()
|
|||
|
|
|||
|
# 初始化参数
|
|||
|
param = params.ModelParams()
|
|||
|
pvfd_param = guangfufadian_model_base.guangfufadian_Args()
|
|||
|
windfd_args = fenglifadian_model_base.fenglifadian_Args()
|
|||
|
|
|||
|
# 模型实例
|
|||
|
dimaoshibie_SegFormer = segformer.SegFormer_Segmentation()
|
|||
|
roof_model = get_roof_model()
|
|||
|
pv_model = get_pv_model()
|
|||
|
|
|||
|
pvfd_model_path = os.path.join(pvfd_param.checkpoints,'Crossformer_station08_il192_ol96_sl6_win2_fa10_dm256_nh4_el3_itr0/checkpoint.pth') # 修改为实际模型路径
|
|||
|
pvfd_model = guangfufadian_model_base.ModelInference(pvfd_model_path, pvfd_param)
|
|||
|
|
|||
|
windfd_model_path = os.path.join(windfd_args.checkpoints,'Crossformer_Wind_farm_il192_ol12_sl6_win2_fa10_dm256_nh4_el3_itr0/checkpoint.pth') # 修改为实际模型路径
|
|||
|
windfd_model = fenglifadian_model_base.ModelInference(windfd_model_path, windfd_args)
|
|||
|
|
|||
|
ch4_model_flow = joblib.load(os.path.join(current_dir,'jiawanyuce/liuliang_model/xgb_model_liuliang.pkl'))
|
|||
|
ch4_model_gas = joblib.load(os.path.join(current_dir,'jiawanyuce/qixiangnongdu_model/xgb_model_qixiangnongdu.pkl'))
|
|||
|
|
|||
|
|
|||
|
# 模型调用
|
|||
|
"""地貌识别"""
|
|||
|
# # 地貌识别 - 图形分割
|
|||
|
# # path 这里需要每次上传,出现一个uuid命名的文件夹,防止文件冲突 eg:D:\\project\\ai_station\\wudingpv\\tmp\\99f06853-4788-4608-8884-2a3b7bc33768
|
|||
|
# path = "/home/xiazj/ai-station/ai_station_merge/tmp/dimaoshibie/crop_9_14.png"
|
|||
|
# result = model_deal.dimaoshibie_pic(dimaoshibie_SegFormer,path,param.dmsb_count,param.dmsb_name_classes)
|
|||
|
# print(result)
|
|||
|
|
|||
|
# # 地貌识别 - 像素面积计算
|
|||
|
# path = "/home/xiazj/ai-station/ai_station_merge/tmp/dimaoshibie/crop_9_14.png"
|
|||
|
# scale_m = 0.92*0.92 # Z18代表的,像素大小
|
|||
|
# result = model_deal.dimaoshibie_area(path,scale_m,param.dmsb_colors)
|
|||
|
# print(result)
|
|||
|
|
|||
|
|
|||
|
"""屋顶识别"""
|
|||
|
# # 屋顶识别- 图形分割
|
|||
|
# path = "/home/xiazj/ai-station/ai_station_merge/tmp/wuding/99f06853-4788-4608-8884-2a3b7bc32131/taihuyuan_7-8.png"
|
|||
|
# result = model_deal.roof_pic(roof_model,path,param.wdpv_palette)
|
|||
|
# print(result)
|
|||
|
|
|||
|
|
|||
|
|
|||
|
# # 屋顶识别 - 像素面积计算
|
|||
|
# path = "/home/xiazj/ai-station/ai_station_merge/tmp/wuding/99f06853-4788-4608-8884-2a3b7bc32131/taihuyuan_7-8.png"
|
|||
|
# scale_m = 0.92*0.92 # Z18代表的,像素大小
|
|||
|
# result = model_deal.roof_area(path,scale_m,param.wdpv_colors)
|
|||
|
# print(result)
|
|||
|
|
|||
|
|
|||
|
""" 光伏识别 """ # 公用一个接口即可,url进行区别
|
|||
|
# # 光伏识别- 图形分割
|
|||
|
# path = "/home/xiazj/ai-station/ai_station_merge/tmp/pv/99f06853-4788-4608-8884-2a3b7bc32131/taihuyuan_7-8.png"
|
|||
|
# result = model_deal.roof_pic(pv_model,path,param.wdpv_palette)
|
|||
|
# print(result)
|
|||
|
|
|||
|
# 光伏识别 - 像素面积计算
|
|||
|
# path = "/home/xiazj/ai-station/ai_station_merge/tmp/pv/99f06853-4788-4608-8884-2a3b7bc32131/taihuyuan_7-8.png"
|
|||
|
# scale_m = 0.92*0.92 # Z18代表的,像素大小
|
|||
|
# result = model_deal.roof_area(path,scale_m,param.wdpv_colors)
|
|||
|
# print(result)
|
|||
|
|
|||
|
|
|||
|
|
|||
|
""" 屋顶光伏识别 """
|
|||
|
# 屋顶光伏识别- 图形分割
|
|||
|
# path = "/home/xiazj/ai-station/ai_station_merge/tmp/wudingpv/99f06853-4788-4608-8884-2a3b7bc32131/taihuyuan_7-8.png"
|
|||
|
# result = model_deal.roofpv_pic(roof_model,pv_model,path,param.wdpv_palette)
|
|||
|
# file_list = result['reason']
|
|||
|
# final_path = prepare_data.merge_binary(file_list)
|
|||
|
# # 屋顶光伏识别 - 像素面积计算
|
|||
|
# path = "/home/xiazj/ai-station/ai_station_merge/tmp/wudingpv/99f06853-4788-4608-8884-2a3b7bc32131/taihuyuan_7-8.png"
|
|||
|
# scale_m = 0.92*0.92 # Z18代表的,像素大小
|
|||
|
# result = model_deal.roof_area_roofpv(path,scale_m,param.wdpv_colors)
|
|||
|
# print(result)
|
|||
|
|
|||
|
|
|||
|
|
|||
|
"""光伏出力预测"""
|
|||
|
# 1. 读取训练文件,截取部分数据,并针对点击列进行数据前端展示
|
|||
|
# 展示某一列,并进行返回
|
|||
|
# tmp = prepare_data.show_data_pvfd('nwp_directirrad')
|
|||
|
# print(tmp)
|
|||
|
|
|||
|
# # 2. 返回测试数据给前端
|
|||
|
# test_data_path = "/home/xiazj/ai-station/ai_station_merge/tmp/guangfufadian/99f06853-4788-4608-8884-2a3b7bc32131/run_test.csv"
|
|||
|
# tmp = prepare_data.show_testdata_pvfd(test_data_path)
|
|||
|
# print(tmp)
|
|||
|
|
|||
|
|
|||
|
# 3. 上传数据的预测
|
|||
|
# 返回pred_data,true_data,通过时间戳为x轴,进行展示结果即可,没有的话就是没有,有的话就是有
|
|||
|
# test_data_path = "/home/xiazj/ai-station/ai_station_merge/tmp/guangfufadian/99f06853-4788-4608-8884-2a3b7bc32131/run_test.csv"
|
|||
|
# predictions = pvfd_model.run_inference(test_data_path)
|
|||
|
# predictions = np.array(predictions).flatten()
|
|||
|
# pred_data, true_data = prepare_data.result_merge_guangfufadian(test_data_path,predictions)
|
|||
|
# print(pred_data)
|
|||
|
# print(true_data)
|
|||
|
|
|||
|
|
|||
|
|
|||
|
"""风力发电预测 """
|
|||
|
# # 1. 读取训练文件,截取部分数据,并针对点击列进行数据前端展示
|
|||
|
# # 展示某一列,并进行返回
|
|||
|
# tmp = prepare_data.show_data_windfd("Wind speed at height of 10 meters (m/s)")
|
|||
|
# print(tmp)
|
|||
|
|
|||
|
# # 2. 返回测试数据给前端
|
|||
|
# test_data_path = "/home/xiazj/ai-station/ai_station_merge/tmp/fenglifadian/99f06853-4788-4608-8884-2a3b7bc32131/Wind_farm_test.csv"
|
|||
|
# tmp = prepare_data.show_testdata_windfd(test_data_path)
|
|||
|
# print(tmp)
|
|||
|
|
|||
|
|
|||
|
# # 3. 上传数据的预测
|
|||
|
# test_data_path = "/home/xiazj/ai-station/ai_station_merge/tmp/fenglifadian/99f06853-4788-4608-8884-2a3b7bc32131/Wind_farm_test.csv"
|
|||
|
# predictions = windfd_model.run_inference(test_data_path)
|
|||
|
# predictions = np.array(predictions).flatten()
|
|||
|
# pred_data, true_data = prepare_data.result_merge_fenglifadian(test_data_path,predictions)
|
|||
|
# print(pred_data)
|
|||
|
# print(true_data)
|
|||
|
|
|||
|
|
|||
|
"""甲烷产量预测"""
|
|||
|
#1、读取训练文件,截取部分数据,并针对点击列进行数据前端展示
|
|||
|
# test_data_path = "/home/xiazj/ai-station/ai_station_merge/tmp/jiawanyuce/jiawan_test.csv"
|
|||
|
# tmp = prepare_data.show_data_jiawanyuce("X_ch")
|
|||
|
# print(tmp)
|
|||
|
|
|||
|
# #2、进行预测,预测接口
|
|||
|
# test_data_path = "/home/xiazj/ai-station/ai_station_merge/tmp/jiawanyuce/jiawan_test.csv"
|
|||
|
# tmp = model_deal.start_predict_endpoint(ch4_model_flow,ch4_model_gas,test_data_path,"2023-01-01 02:30:00","2023-01-03 02:30:00",2,False)
|
|||
|
# print(tmp)
|
|||
|
|
|||
|
""" 煤热解 """
|
|||
|
meirejie_test_content = {
|
|||
|
'A': 11.92,
|
|||
|
'V' : 51.16,
|
|||
|
'FC': 48.84,
|
|||
|
'C': 83.22 ,
|
|||
|
'H': 3.89,
|
|||
|
'N': 2.72,
|
|||
|
'S': 0.45,
|
|||
|
'O':20.21,
|
|||
|
'H/C':0.56,
|
|||
|
'O/C':0.18,
|
|||
|
'N/C':0.03,
|
|||
|
'Rt':40.00,
|
|||
|
'Hr':5.00,
|
|||
|
'dp':0.20,
|
|||
|
'T':510.00
|
|||
|
}
|
|||
|
meirejie_test_content = pd.DataFrame([meirejie_test_content])
|
|||
|
meirejie_result_content = {
|
|||
|
'Tar':11.71,
|
|||
|
'Gas':10.81 ,
|
|||
|
'Char':75.31,
|
|||
|
'Water':2.17
|
|||
|
}
|
|||
|
#1、 tar 焦油
|
|||
|
#1.1 单独预测
|
|||
|
# tar_result = model_deal.pred_single_tar(meirejie_test_content)
|
|||
|
# print(tar_result)
|
|||
|
# #1.2 文件批量单独模型预测
|
|||
|
# model_name ='xgb_gas'
|
|||
|
# result = model_deal.get_excel_tar(model_name)
|
|||
|
# print(result)
|
|||
|
|
|||
|
|
|||
|
#2、 char 煤渣
|
|||
|
#2.1 单独预测
|
|||
|
# char_result = model_deal.pred_single_char(meirejie_test_content)
|
|||
|
# print(char_result)
|
|||
|
# #2.2 文件批量单独模型预测
|
|||
|
# model_name ='xgb_gas'
|
|||
|
# result = model_deal.get_excel_char(model_name)
|
|||
|
# print(result)
|
|||
|
|
|||
|
|
|||
|
#3、 water 蒸汽
|
|||
|
#3.1 单独预测
|
|||
|
# water_result = model_deal.pred_single_water(meirejie_test_content)
|
|||
|
# print(water_result)
|
|||
|
# #3.2 文件批量单独模型预测
|
|||
|
# model_name ='xgb_gas'
|
|||
|
# result = model_deal.get_excel_water(model_name)
|
|||
|
# print(result)
|
|||
|
|
|||
|
|
|||
|
#4、 gas 煤气
|
|||
|
#4.1 单独预测
|
|||
|
# gas_result = model_deal.pred_single_gas(meirejie_test_content)
|
|||
|
# print(gas_result)
|
|||
|
# #4.2 文件批量单独模型预测
|
|||
|
# model_name ='xgb_gas'
|
|||
|
# result = model_deal.get_excel_gas(model_name)
|
|||
|
# print(result)
|
|||
|
|
|||
|
|
|||
|
|
|||
|
"""煤基碳材料"""
|
|||
|
meijiegou_test_content = {
|
|||
|
'A': 10.43,
|
|||
|
'VM' : 35.88,
|
|||
|
'K/C': 4,
|
|||
|
'MM': 0 ,
|
|||
|
'AT': 800,
|
|||
|
'At': 1,
|
|||
|
'Rt': 5
|
|||
|
}
|
|||
|
# meijiegou_test_content = pd.DataFrame([meijiegou_test_content])
|
|||
|
|
|||
|
meicailiao_test_content = {
|
|||
|
"SSA":1141.8,
|
|||
|
"TPV":0.46,
|
|||
|
"N":1.74,
|
|||
|
"O":3.84,
|
|||
|
"ID/IG":1.102,
|
|||
|
"J":0.5
|
|||
|
}
|
|||
|
|
|||
|
meicailiao_test_content = pd.DataFrame([meicailiao_test_content])
|
|||
|
|
|||
|
# 活性炭总表面积预测分析 #
|
|||
|
# # # 1、 单独预测
|
|||
|
# def test(content: post_model.Zongbiaomianji):
|
|||
|
# meijiegou_test_content = pd.DataFrame([content])
|
|||
|
# ssa_result = model_deal.pred_single_ssa(meijiegou_test_content)
|
|||
|
# print(ssa_result)
|
|||
|
|
|||
|
# test(meijiegou_test_content)
|
|||
|
# import uuid
|
|||
|
|
|||
|
# print(str(uuid.uuid4()))
|
|||
|
# # 2、文件批量单独模型预测
|
|||
|
# model_name ='xgb_ssa'
|
|||
|
# file_path = "/home/xiazj/ai-station/tmp/meijitancailiao/ssa/866b43f9-9ec0-423c-a974-7f46232b8277/test_ssa.csv"
|
|||
|
# result = model_deal.get_excel_ssa(model_name,file_path)
|
|||
|
# print(result)
|
|||
|
|
|||
|
# # 活性炭孔总体积预测分析 #
|
|||
|
# # # 1、 单独预测
|
|||
|
# tpv_result = model_deal.pred_single_tpv(meijiegou_test_content)
|
|||
|
# print(tpv_result)
|
|||
|
|
|||
|
# # # 2、文件批量单独模型预测
|
|||
|
# model_name ='xgb_tpv'
|
|||
|
# result = model_deal.get_excel_tpv(model_name)
|
|||
|
# print(result)
|
|||
|
|
|||
|
|
|||
|
# # 煤炭电容预测分析 #
|
|||
|
# # 1、 单独预测
|
|||
|
# meitan_result = model_deal.pred_single_meitan(meicailiao_test_content)
|
|||
|
# print(meitan_result)
|
|||
|
|
|||
|
# # # 2、文件批量单独模型预测
|
|||
|
# model_name ='xgb_meitan'
|
|||
|
# result = model_deal.get_excel_meitan(model_name)
|
|||
|
# print(result)
|
|||
|
|
|||
|
|
|||
|
# # 煤炭电容预测分析 #
|
|||
|
# # # 1、 单独预测
|
|||
|
# meiliqing_result = model_deal.pred_single_meiliqing(meicailiao_test_content)
|
|||
|
# print(meiliqing_result)
|
|||
|
|
|||
|
# # # 2、文件批量单独模型预测
|
|||
|
# model_name ='xgb_meiliqing'
|
|||
|
# result = model_deal.get_excel_meiliqing(model_name)
|
|||
|
# print(result)
|
|||
|
|
|||
|
|
|||
|
# # 数据模拟返回 #
|
|||
|
|
|||
|
# params = {"A_min" : None, "A_max": None, "A_step": None, "VM_min" : None, "VM_max": None, "VM_step": None,
|
|||
|
# "KC_min" : None, "KC_max": None, "KC_step": None,"MM_min" : None, "MM_max": None, "MM_step": None,
|
|||
|
# "AT_min" : None, "AT_max": None, "AT_step": None,"At_min" : None, "At_max": None, "At_step": None,
|
|||
|
# "Rt_min" : None, "Rt_max": None, "Rt_step": None}
|
|||
|
params = {'A_min': 4.0, 'A_max': 48.0, 'A_step': 4.0, 'VM_min': 5.0, 'VM_max': 50.0, 'VM_step': 5.0, 'KC_min': 1.0, 'KC_max': 4.0, 'KC_step': 0.5, 'MM_min': 0.0, 'MM_max': 1.0, 'MM_step': 1.0, 'AT_min': 600.0, 'AT_max': 900.0, 'AT_step': 50.0, 'At_min': 0.5, 'At_max': 2.0, 'At_step': 0.5, 'Rt_min': 5.0, 'Rt_max': 10.0, 'Rt_step': 5.0}
|
|||
|
params = prepare_data.get_params(params)
|
|||
|
pred_data = prepare_data.create_pred_data(params)
|
|||
|
result = model_deal.pred_func("xgb",pred_data)
|
|||
|
sorted_result = result.sort_values(by=['SSA', 'TPV'], ascending=[False, False])
|
|||
|
# 保留条数
|
|||
|
num = 6
|
|||
|
if num is None:
|
|||
|
print(sorted_result.head()) # 返回全部
|
|||
|
else:
|
|||
|
print(sorted_result.head(6)) # 返回所需条数
|
|||
|
|
|||
|
|
|||
|
"""数据界面获取"""
|
|||
|
# sql = "SELECT application_name, task_type, sample_name, img_url, time, download_url FROM data_samples"
|
|||
|
# data = data_util.fetch_data(sql)
|
|||
|
# data = data_util.generate_json_data_source(data)
|
|||
|
# print(data)
|
|||
|
|
|||
|
# sql = "SELECT application_name, task_type, sample_name, img_url, time FROM app_samples"
|
|||
|
# data = data_util.fetch_data(sql)
|
|||
|
# data = data_util.generate_json_app_source(data)
|
|||
|
# print(data)
|
|||
|
|
|||
|
# sql = "SELECT application_name, task_type, sample_name, img_url, time FROM meijitancailiao_samples"
|
|||
|
# data = data_util.fetch_data(sql)
|
|||
|
# data = data_util.generate_json_meijitancailiao_source(data)
|
|||
|
# print(data)
|
|||
|
# type = "zongkongtiji"
|
|||
|
# sql = "SELECT type, chinese_name, col_name, data_type, unit, data_scale FROM meijitancailiao_features where use_type = %s;"
|
|||
|
# data = data_util.fetch_data_with_param(sql,(type,))
|
|||
|
# print(data)
|