欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java集成和使用dl4j實(shí)現(xiàn)通過掃描圖片識(shí)別快遞單信息

 更新時(shí)間:2024年12月26日 09:19:44   作者:慧香一格  
這篇文章主要為大家詳細(xì)介紹了Java如何使用DL4J搭建一個(gè)簡(jiǎn)單的圖像識(shí)別模型,并將其集成到Spring?Boot后端中從而實(shí)現(xiàn)通過掃描圖片識(shí)別快遞單信息,需要的可以參考下

使用DL4J(DeepLearning4J)搭建一個(gè)簡(jiǎn)單的圖像識(shí)別模型,并將其集成到Spring Boot后端中。我們將使用MNIST數(shù)據(jù)集來訓(xùn)練一個(gè)簡(jiǎn)單的卷積神經(jīng)網(wǎng)絡(luò)(CNN),然后將其部署到Spring Boot應(yīng)用中。

1. 設(shè)置Spring Boot項(xiàng)目

首先,創(chuàng)建一個(gè)新的Spring Boot項(xiàng)目。你可以使用Spring Initializr(https://start.spring.io/)來快速生成項(xiàng)目結(jié)構(gòu)。選擇以下依賴:

Spring Web

Spring Boot DevTools

Lombok(可選,用于簡(jiǎn)化代碼)

2. 添加DL4J依賴

在你的pom.xml文件中添加DL4J和相關(guān)依賴:

<dependencies>
    <!-- Spring Boot Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
 
    <!-- DL4J -->
    <dependency>
        <groupId>org.deeplearning4j</groupId>
        <artifactId>deeplearning4j-core</artifactId>
        <version>1.0.0-beta7</version>
    </dependency>
    <dependency>
        <groupId>org.deeplearning4j</groupId>
        <artifactId>deeplearning4j-nn</artifactId>
        <version>1.0.0-beta7</version>
    </dependency>
    <dependency>
        <groupId>org.deeplearning4j</groupId>
        <artifactId>deeplearning4j-ui</artifactId>
        <version>1.0.0-beta7</version>
    </dependency>
    <dependency>
        <groupId>org.nd4j</groupId>
        <artifactId>nd4j-native-platform</artifactId>
        <version>1.0.0-beta7</version>
    </dependency>
 
    <!-- File Upload -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.11.0</version>
    </dependency>
 
    <!-- Lombok (optional) -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

3. 訓(xùn)練DL4J模型

我們將使用MNIST數(shù)據(jù)集來訓(xùn)練一個(gè)簡(jiǎn)單的卷積神經(jīng)網(wǎng)絡(luò)(CNN)。創(chuàng)建一個(gè)新的Java類MnistModelTrainer.java來訓(xùn)練模型:

package com.example.scanapp;
 
import org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.layers.ConvolutionLayer;
import org.deeplearning4j.nn.conf.layers.DenseLayer;
import org.deeplearning4j.nn.conf.layers.OutputLayer;
import org.deeplearning4j.nn.conf.layers.SubsamplingLayer;
import org.deeplearning4j.nn.conf.layers.objdetect.YoloOutputLayer;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.optimize.listeners.ScoreIterationListener;
import org.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler;
import org.nd4j.linalg.lossfunctions.LossFunctions;
import org.nd4j.linalg.learning.config.Adam;
 
import java.io.File;
import java.io.IOException;
 
public class MnistModelTrainer {
 
    public static void main(String[] args) throws IOException {
        int numEpochs = 10;
        int batchSize = 64;
        int numLabels = 10;
        int numRows = 28;
        int numColumns = 28;
        int numChannels = 1;
 
        // Load MNIST data
        DataSetIterator mnistTrain = new MnistDataSetIterator(batchSize, true, 12345);
        DataSetIterator mnistTest = new MnistDataSetIterator(batchSize, false, 12345);
 
        // Preprocess data
        ImagePreProcessingScaler scaler = new ImagePreProcessingScaler(0, 1);
        mnistTrain.setPreProcessor(scaler);
        mnistTest.setPreProcessor(scaler);
 
        // Define the network architecture
        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                .seed(12345)
                .updater(new Adam(0.001))
                .list()
                .layer(0, new ConvolutionLayer.Builder(5, 5)
                        .nIn(numChannels)
                        .nOut(20)
                        .stride(1, 1)
                        .activation(Activation.RELU)
                        .build())
                .layer(1, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX)
                        .kernelSize(2, 2)
                        .stride(2, 2)
                        .build())
                .layer(2, new ConvolutionLayer.Builder(5, 5)
                        .nOut(50)
                        .stride(1, 1)
                        .activation(Activation.RELU)
                        .build())
                .layer(3, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX)
                        .kernelSize(2, 2)
                        .stride(2, 2)
                        .build())
                .layer(4, new DenseLayer.Builder().activation(Activation.RELU)
                        .nOut(500).build())
                .layer(5, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
                        .nOut(numLabels)
                        .activation(Activation.SOFTMAX)
                        .build())
                .build();
 
        // Initialize the network
        MultiLayerNetwork model = new MultiLayerNetwork(conf);
        model.init();
        model.setListeners(new ScoreIterationListener(10));
 
        // Train the network
        for (int i = 0; i < numEpochs; i++) {
            model.fit(mnistTrain);
        }
 
        // Save the model
        File locationToSave = new File("mnist-model.zip");
        boolean saveUpdater = true; // Save the updater
        ModelSerializer.writeModel(model, locationToSave, saveUpdater);
    }
}

運(yùn)行MnistModelTrainer類來訓(xùn)練模型并保存到mnist-model.zip文件中。

4. 創(chuàng)建Spring Boot Controller

創(chuàng)建一個(gè)新的Controller來處理圖片上傳和圖像識(shí)別:

package com.example.scanapp.controller;
 
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.util.ModelSerializer;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler;
import org.nd4j.linalg.factory.Nd4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
 
@RestController
public class ImageController {
 
    private static final String MODEL_PATH = "mnist-model.zip"; // 替換為你的模型路徑
    private MultiLayerNetwork model;
    private ImagePreProcessingScaler scaler;
 
    public ImageController() throws IOException {
        this.model = ModelSerializer.restoreMultiLayerNetwork(new File(MODEL_PATH));
        this.scaler = new ImagePreProcessingScaler(0, 1);
    }
 
    @PostMapping("/recognize")
    public String recognize(@RequestParam("image") MultipartFile file) {
        try {
            BufferedImage image = ImageIO.read(file.getInputStream());
            INDArray imageArray = Nd4j.create(new int[]{1, 1, 28, 28});
            for (int i = 0; i < 28; i++) {
                for (int j = 0; j < 28; j++) {
                    int rgb = image.getRGB(j, i);
                    int gray = (rgb >> 16) & 0xFF; // Convert to grayscale
                    imageArray.putScalar(0, 0, i, j, gray / 255.0);
                }
            }
 
            scaler.transform(imageArray);
            INDArray output = model.output(imageArray);
            int predictedClass = output.argMax(1).getInt(0);
 
            return "Predicted class: " + predictedClass;
        } catch (IOException e) {
            e.printStackTrace();
            return "Error processing image";
        }
    }
}

5. 測(cè)試API

你可以使用Postman或其他工具來測(cè)試你的API。發(fā)送一個(gè)POST請(qǐng)求到/recognize端點(diǎn),并附帶一個(gè)MNIST格式的圖片文件(28x28像素的灰度圖像)。

6. 運(yùn)行Spring Boot應(yīng)用

確保你的Spring Boot應(yīng)用能夠正常啟動(dòng)。你可以通過以下命令運(yùn)行應(yīng)用:

mvn spring-boot:run

7. 前端集成(可選)

如果你有一個(gè)前端應(yīng)用(例如Vue.js),你可以創(chuàng)建一個(gè)簡(jiǎn)單的表單來上傳圖片并調(diào)用后端API。以下是一個(gè)簡(jiǎn)單的Vue.js組件示例:

<template>
  <div>
    <h1>Image Recognition</h1>
    <input type="file" @change="onFileChange" accept="image/*" />
    <button @click="uploadImage">Upload</button>
    <p v-if="result">{{ result }}</p>
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      file: null,
      result: ''
    };
  },
  methods: {
    onFileChange(e) {
      this.file = e.target.files[0];
    },
    async uploadImage() {
      const formData = new FormData();
      formData.append('image', this.file);
 
      try {
        const response = await fetch('http://localhost:8080/recognize', {
          method: 'POST',
          body: formData
        });
        const data = await response.text();
        this.result = data;
      } catch (error) {
        console.error('Error uploading image:', error);
      }
    }
  }
};
</script>

代碼解讀

將上述Vue.js組件添加到你的Vue項(xiàng)目中,然后運(yùn)行前端應(yīng)用來測(cè)試整個(gè)流程。

通過以上步驟,你應(yīng)該能夠成功搭建一個(gè)使用DL4J模型的Spring Boot后端服務(wù),并通過前端應(yīng)用進(jìn)行圖像識(shí)別。

以上就是Java集成和使用dl4j實(shí)現(xiàn)通過掃描圖片識(shí)別快遞單信息的詳細(xì)內(nèi)容,更多關(guān)于Java dl4j實(shí)現(xiàn)圖片識(shí)別的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Maven使用方法詳及方式詳細(xì)介紹

    Maven使用方法詳及方式詳細(xì)介紹

    使用maven倉庫的話需要從網(wǎng)上下載maven的包,比如“apache-maven-3.5.4-bin.tar”,下載完成之后解壓,在解壓的文件夾中的conf目錄下的settings.xml文件夾下就可以配置maven遠(yuǎn)程倉庫和本地倉庫的地址
    2022-11-11
  • Java設(shè)計(jì)模式之模板方法模式

    Java設(shè)計(jì)模式之模板方法模式

    這篇文章介紹了Java設(shè)計(jì)模式之模板方法模式,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-10-10
  • idea搭建ssh框架的超詳細(xì)教程

    idea搭建ssh框架的超詳細(xì)教程

    這篇文章主要介紹了idea搭建ssh框架的超詳細(xì)教程,本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • springboot整合netty-mqtt-client實(shí)現(xiàn)Mqtt消息的訂閱和發(fā)布示例

    springboot整合netty-mqtt-client實(shí)現(xiàn)Mqtt消息的訂閱和發(fā)布示例

    本文主要介紹了springboot整合netty-mqtt-client實(shí)現(xiàn)Mqtt消息的訂閱和發(fā)布示例,文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Java通過URL類下載圖片的實(shí)例代碼

    Java通過URL類下載圖片的實(shí)例代碼

    這篇文章主要介紹了Java通過URL類下載圖片,文中結(jié)合實(shí)例代碼補(bǔ)充介紹了java通過url獲取圖片文件的相關(guān)知識(shí),代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-02-02
  • springboot配置多個(gè)數(shù)據(jù)源兩種方式實(shí)現(xiàn)

    springboot配置多個(gè)數(shù)據(jù)源兩種方式實(shí)現(xiàn)

    在我們的實(shí)際業(yè)務(wù)中可能會(huì)遇到;在一個(gè)項(xiàng)目里面讀取多個(gè)數(shù)據(jù)庫的數(shù)據(jù)來進(jìn)行展示,spring對(duì)同時(shí)配置多個(gè)數(shù)據(jù)源是支持的,本文主要介紹了springboot配置多個(gè)數(shù)據(jù)源兩種方式實(shí)現(xiàn),感興趣的可以了解一下
    2022-03-03
  • Java數(shù)據(jù)結(jié)構(gòu)之LinkedList的用法詳解

    Java數(shù)據(jù)結(jié)構(gòu)之LinkedList的用法詳解

    鏈表(Linked?list)是一種常見的基礎(chǔ)數(shù)據(jù)結(jié)構(gòu),是一種線性表。Java的LinkedList(鏈表)?類似于?ArrayList,是一種常用的數(shù)據(jù)容器,本文就來簡(jiǎn)單講講它的使用吧
    2023-05-05
  • java實(shí)現(xiàn)字符串like和not?like的使用示例

    java實(shí)現(xiàn)字符串like和not?like的使用示例

    在Java中,我們經(jīng)常需要對(duì)字符串進(jìn)行模式匹配操作,字符串的模式匹配通常使用like和not?like這兩個(gè)運(yùn)算符進(jìn)行,本文就來介紹一下如何實(shí)現(xiàn),感興趣的可以了解一下
    2023-09-09
  • Spring MVC---數(shù)據(jù)綁定和表單標(biāo)簽詳解

    Spring MVC---數(shù)據(jù)綁定和表單標(biāo)簽詳解

    本篇文章主要介紹了Spring MVC---數(shù)據(jù)綁定和表單標(biāo)簽詳解,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-01-01
  • 在Java中如何避免創(chuàng)建不必要的對(duì)象

    在Java中如何避免創(chuàng)建不必要的對(duì)象

    作為Java開發(fā)者,我們每天創(chuàng)建很多對(duì)象,但如何才能避免創(chuàng)建不必要的對(duì)象呢?這需要我們好好學(xué)習(xí),這篇文章主要給大家介紹了關(guān)于在Java中如何避免創(chuàng)建不必要對(duì)象的相關(guān)資料,需要的朋友可以參考下
    2021-10-10

最新評(píng)論