YOLOv5部署,并实现Java客户端向Python服务端发送请求进行图片火苗检测

马上要参加计算机设计大赛,Web系统中开发了火苗实时检测功能。
因为之前一直没有去做,所以今晚女娲补天式急速开发(三个小时),因此业务上难免有逻辑错误,代码也难免有问题。
该文章仅记录一下该功能的核心代码,有错或者如何优化请大佬指出。

模型检测效果展示:

在这里插入图片描述
在这里插入图片描述

业务流程图:

在这里插入图片描述

Python服务端核心代码:

from flask import Flask, request, send_file
from detect import parse_opt, main
import shutil
import urllib.request

app = Flask(__name__)


@app.route('/detect', methods=['POST'])
def detect():
    image_url = request.data.decode('utf-8')
    output_path = 'data/images/fire/image.jpg'
    download_image(image_url, output_path)

    delete_folder('runs/detect')
    opt = parse_opt()
    main(opt)

    return send_file('runs/detect/exp/image.jpg', mimetype='image/jpg')


def delete_folder(folder_path):
    try:
        shutil.rmtree(folder_path)
        print(f"文件夹 {folder_path} 删除成功")
    except Exception as e:
        print(f"删除文件夹 {folder_path} 失败: {str(e)}")


def download_image(url, output_path):
    try:
        urllib.request.urlretrieve(url, output_path)
        print("图片保存成功!")
    except Exception as e:
        print(f"图片保存失败:{str(e)}")


if __name__ == '__main__':
    app.run()

Java客户端核心代码:

package com.ruoyi.ai.fire;

import com.ruoyi.sc.domain.QiniuFile;
import com.ruoyi.sc.utils.QiniuUtils;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.mock.web.MockMultipartFile;

/**
 * @Author: xinglibao
 * @Date: 2023/7/19 23:05
 */
public class FireDetectService {

    public String detect(String url) {
        // 创建RestTemplate实例
        RestTemplate restTemplate = new RestTemplate();

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.TEXT_PLAIN);

        // 构建请求体
        HttpEntity<String> requestEntity = new HttpEntity<>(url, headers);

        // 发送请求
        ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity("http://127.0.0.1:5000/detect", requestEntity, byte[].class);

        // 获取返回的图片文件
        byte[] imageBytes = responseEntity.getBody();

        // 将图片上传至七牛云
        ByteArrayResource resource = new ByteArrayResource(imageBytes);
        MultipartFile file = null;
        try {
            file = new MockMultipartFile("image.jpg", resource.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        QiniuFile qiniuFile = QiniuUtils.upload(file);
        return qiniuFile.getUrl();

        /*AjaxResult ajax = AjaxResult.success();
        ajax.put("url", qiniuFile.getUrl());
        ajax.put("fileName", qiniuFile.getFileName());
        ajax.put("newFileName", qiniuFile.getNewFileName());
        ajax.put("originalFilename", qiniuFile.getOriginalFilename());*/
    }

    public static void main(String[] args) {
        System.out.println(new FireDetectService().detect("http://lygshengben.com/typora/nnnfsdfds.jpg"));
    }

}

系统前端界面展示:

在这里插入图片描述
在这里插入图片描述

附:SpringBoot整合七牛云实现文件上传——附Utils工具类(若依整合七牛云):

SpringBoot整合七牛云实现文件上传——附Utils工具类(若依整合七牛云)

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枯木何日可逢春

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值