# 当前为语义分割的脚本,从现场minio读取图片,进而做语义分割,最终将结果存储到本地minio import datetime import json import os from cropland_module.change_detection_module import ChangeDetectionModule from middleware.minio_util import downFile, upload_file, upload_file_from_buffer from io import BytesIO from mqtt_pub import MQTTClient 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 detection_func(task_id, s3_id, early_pic_url, later_pic_url, func_id): print("\n=== 变化检测系统 ===") # args = parse_args() gpu_id = "0" img_size = 256 lr = 0.0001 model_path = r'cropland_module/netCD_epoch_34.pth' # # image1_path = r'D:\project\dlfg\LEVIR-CD\test\A1\00201.png' # # image1_path = r'C:\Users\14867\Desktop\0613-building\Axz\xz\270\00201.jpg' # # # # 平移 5%-10% # # image1_path = r'C:\Users\14867\Desktop\0613-building\Axz\pingyi\00201.png' # # image2_path = r'D:\project\dlfg\LEVIR-CD\test\B1\00201.png' # image1_path = r'C:\Users\14867\Desktop\0613-building\Axz\pingyi\00205.png' # image2_path = r'C:\Users\14867\Desktop\0613-building\B\00205.png' # # image1_path=r'C:\Users\14867\Desktop\0613-building\B\00201.png' # # image2_path=r'C:\Users\14867\Desktop\0613-building\A\00201.png' cd = ChangeDetectionModule(gpu_id, img_size, lr) cd.load_model(model_path) 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') output, binary_result = cd.predict_without_label(image1, image2) pic_name = os.path.basename(later_pic) # # # 保存结果(可选) # result = Image.fromarray(binary_result) # result.save("output.png") # result = Image.fromarray(binary_result) # 将图像保存到 BytesIO 对象中 buffer = BytesIO() result.save(buffer, format="PNG") # 保存为 PNG 格式 buffer.seek(0) # # 现在 image_data 是图片的二进制数据 # 你可以将其写入文件、发送到网络等 # with open("output.png", "wb") as f: # f.write(image_data) date_str = datetime.datetime.now().strftime("%Y%m%d") time_s = datetime.datetime.now().timestamp() mqtt_client = MQTTClient(broker, port, topic) 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_from_buffer(buffer,pic_name) message = { "flight_task_id": task_id, "minio": { "minio_path_1": minio_path_1, "minio_path_2": minio_path_2, "minio_path_result": minio_path_result, "file_type": file_type_result } } json_message = json.dumps(message, indent=4, ensure_ascii=False) mqtt_client.publish_message(json_message)