139 lines
5.4 KiB
Python
139 lines
5.4 KiB
Python
|
|
import datetime
|
|||
|
|
import json
|
|||
|
|
import os.path
|
|||
|
|
import time
|
|||
|
|
from io import BytesIO
|
|||
|
|
from PIL import Image
|
|||
|
|
from torchvision import transforms
|
|||
|
|
|
|||
|
|
from CropLand_CD_module.change_detection_module import ChangeDetectionModule
|
|||
|
|
from CropLand_CD_module.visualize_pil_segmentation_mask import visualize_pil_segmentation_mask_opencv
|
|||
|
|
from middleware.conver_segementation_mask import apply_mask_to_image
|
|||
|
|
from middleware.minio_util import downFile, upload_file, upload_file_from_buffer
|
|||
|
|
from mqtt_pub import MQTTClient
|
|||
|
|
# from change_detection_module import ChangeDetectionModule
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
mqtt_client=None
|
|||
|
|
# MQTT 代理地址和端口
|
|||
|
|
# broker = "112.44.103.230" # 公共 MQTT 代理(免费)
|
|||
|
|
broker = "8.137.54.85" # 公共 MQTT 代理(免费)
|
|||
|
|
port = 1883 # MQTT 默认端口
|
|||
|
|
# 主题
|
|||
|
|
topic = "thing/product/ai/events"
|
|||
|
|
|
|||
|
|
|
|||
|
|
def corpland_detection_func(task_id, s3_id, early_pic_url, later_pic_url, model_func_id):
|
|||
|
|
print("\n=== 变化检测系统 ===")
|
|||
|
|
gpu_id = "0"
|
|||
|
|
img_size = 256 # 模型预期的输入尺寸
|
|||
|
|
lr = 0.0001
|
|||
|
|
|
|||
|
|
|
|||
|
|
early_pic = downFile(early_pic_url)
|
|||
|
|
later_pic = downFile(later_pic_url)
|
|||
|
|
|
|||
|
|
# from PIL import Image
|
|||
|
|
|
|||
|
|
# image1 = Image.open(image1_path).convert('RGB')
|
|||
|
|
# image2 = Image.open(image2_path).convert('RGB')
|
|||
|
|
# image1 = Image.open(early_pic).convert('RGB')
|
|||
|
|
# image2 = Image.open(later_pic).convert('RGB')
|
|||
|
|
early_pic_url=os.path.abspath(early_pic)
|
|||
|
|
later_pic_url=os.path.abspath(later_pic)
|
|||
|
|
|
|||
|
|
# windows路径
|
|||
|
|
# model_path = r'D:\project\2025-7-10_data+model\CropLand-CD-main_3\CropLand-CD-main\save_epoch\best_model.pth'
|
|||
|
|
# save_dir= r"D:\project\2025-7-10_data+model\CropLand-CD-main_3\CropLand-CD-main\save"
|
|||
|
|
# # linux路径
|
|||
|
|
model_path = r"CropLand_CD_module/save_epoch/best_model.pth"
|
|||
|
|
save_dir= r"CropLand_CD_module/save"
|
|||
|
|
|
|||
|
|
cd = ChangeDetectionModule(gpu_id, img_size, lr)
|
|||
|
|
cd.load_model(model_path)
|
|||
|
|
out_save_path = cd.predict_from_imgurl(early_pic_url,later_pic_url,save_dir)
|
|||
|
|
print(out_save_path)
|
|||
|
|
|
|||
|
|
pic_path = os.path.abspath(later_pic)
|
|||
|
|
# src_crs, instance_results_json_path, vis_png, vis_json=visualize_pil_segmentation_mask_opencv(out_save_path,pic_path)
|
|||
|
|
src_crs, instance_results_json_path, vis_png, _ = visualize_pil_segmentation_mask_opencv(out_save_path, pic_path)
|
|||
|
|
|
|||
|
|
mask_dir=os.path.dirname(out_save_path)
|
|||
|
|
mask_name=os.path.basename(out_save_path)
|
|||
|
|
convert_mask_with_pic_name="mask_"+mask_name
|
|||
|
|
convert_mask_path=os.path.join(mask_dir,convert_mask_with_pic_name)
|
|||
|
|
apply_mask_to_image(
|
|||
|
|
image_path=later_pic_url,
|
|||
|
|
mask_path=out_save_path,
|
|||
|
|
output_path=convert_mask_path,
|
|||
|
|
alpha=0.4
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
date_str = datetime.datetime.now().strftime("%Y%m%d")
|
|||
|
|
time_s = datetime.datetime.now().timestamp()
|
|||
|
|
mqtt_client = MQTTClient(broker, port, topic)
|
|||
|
|
|
|||
|
|
pic_name = os.path.basename(later_pic)
|
|||
|
|
|
|||
|
|
pic_name_1 = f"{date_str}/{time_s}-1-{pic_name}"
|
|||
|
|
pic_name_2 = f"{date_str}/{time_s}-2-{pic_name}"
|
|||
|
|
pic_name = f"{date_str}/{time_s}-result-{pic_name}"
|
|||
|
|
|
|||
|
|
minio_path_1, file_type_1 = upload_file(early_pic, None)
|
|||
|
|
minio_path_2, file_type_2 = upload_file(later_pic, None)
|
|||
|
|
minio_path_result, file_type_result = upload_file(convert_mask_path, None)
|
|||
|
|
minio_vis_png, file_type_result = upload_file(vis_png, None)
|
|||
|
|
# if instance_results_json_path and os.path.exists(instance_results_json_path):
|
|||
|
|
# # minio_vis_json, file_type_result = upload_file(json_result_path, "application/json")
|
|||
|
|
# minio_vis_json, file_type_result = upload_file(instance_results_json_path, None)
|
|||
|
|
# # minio_path_result, file_type_result = upload_file_from_buffer(buffer, pic_name)
|
|||
|
|
if instance_results_json_path is None or not os.path.exists(instance_results_json_path):
|
|||
|
|
print("警告: 无法生成有效的JSON结果文件,使用空文件替代")
|
|||
|
|
# 创建一个空的JSON文件作为替代
|
|||
|
|
empty_result = {"error": "无法生成有效的结果", "instances": []}
|
|||
|
|
instance_results_json_path = os.path.join(os.path.dirname(out_save_path), "empty_result.json")
|
|||
|
|
with open(instance_results_json_path, 'w') as f:
|
|||
|
|
json.dump(empty_result, f)
|
|||
|
|
minio_vis_json, file_type_result = upload_file(instance_results_json_path, None)
|
|||
|
|
|
|||
|
|
|
|||
|
|
message = {
|
|||
|
|
"task_id": task_id,
|
|||
|
|
"minio": {
|
|||
|
|
"minio_path_1": minio_path_1,
|
|||
|
|
"minio_path_2": minio_path_2,
|
|||
|
|
"minio_path_result": minio_path_result,
|
|||
|
|
"minio_path_boundary": minio_vis_png,
|
|||
|
|
"minio_path_json": minio_vis_json,
|
|||
|
|
"crs_info":str(src_crs),
|
|||
|
|
"file_type": file_type_result
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
json_message = json.dumps(message, indent=4, ensure_ascii=False)
|
|||
|
|
mqtt_client.publish_message(json_message)
|
|||
|
|
time.sleep(0.5)
|
|||
|
|
# 删除一期
|
|||
|
|
if os.path.exists(early_pic):
|
|||
|
|
os.remove(early_pic)
|
|||
|
|
# 删除二期
|
|||
|
|
if os.path.exists(later_pic):
|
|||
|
|
os.remove(later_pic)
|
|||
|
|
# 删除掩码
|
|||
|
|
if os.path.exists(out_save_path):
|
|||
|
|
os.remove(out_save_path)
|
|||
|
|
# 删除掩码覆盖原图
|
|||
|
|
if os.path.exists(convert_mask_path):
|
|||
|
|
os.remove(convert_mask_path)
|
|||
|
|
# 删除本地掩码图片
|
|||
|
|
if os.path.exists(minio_vis_png):
|
|||
|
|
os.remove(minio_vis_png)
|
|||
|
|
# 删除本地json文件
|
|||
|
|
if os.path.exists(minio_vis_json):
|
|||
|
|
os.remove(minio_vis_json)
|
|||
|
|
# 删除修改经纬度之后的json文件
|
|||
|
|
if os.path.exists(instance_results_json_path):
|
|||
|
|
os.remove(instance_results_json_path)
|
|||
|
|
|
|||
|
|
# if __name__ == '__main__':
|