首页 > 编程语言 >利用javaswing+百度云图像识别接口做一个简单的动植物图像识别

利用javaswing+百度云图像识别接口做一个简单的动植物图像识别

时间:2023-01-04 16:45:28浏览次数:46  
标签:statusLabel 图像识别 String private mainFrame import new javaswing 百度

import com.baidu.aip.imageclassify.AipImageClassify;
import com.sun.prism.PresentableState;
import org.json.JSONObject;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

public class SwingDemo1 {
    private JFrame mainFrame;
//以下是百度云图像ID密匙
    public static final String APP_ID = "****";
    public static final String API_KEY = "****";
    public static final String SECRET_KEY = "******";
    private JLabel headerLabel;
    private JLabel statusLabel;
    private JPanel controlPanel;
    private BufferedImage bufferedImage;
    private Image image;

    public SwingDemo1() {
        prepareGUI();
    }

    public static void main(String[] args) {
        SwingDemo1 swingControlDemo = new SwingDemo1();
        swingControlDemo.showEventDemo();
    }

    private void prepareGUI() {
        mainFrame = new JFrame("图片检测");
        mainFrame.setSize(400, 400);
        mainFrame.setLayout(new GridLayout(3, 1));
        headerLabel = new JLabel("", JLabel.CENTER);
        statusLabel = new JLabel("", JLabel.CENTER);
        statusLabel.setSize(350, 100);
        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });
        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());
        mainFrame.add(headerLabel);
        mainFrame.add(controlPanel);
        mainFrame.add(statusLabel);
        mainFrame.setVisible(true);
    }

    private void showEventDemo() {
        headerLabel.setText("选择识别");
        JButton okButton = new JButton("上传动物图片");
        JButton submitButton = new JButton("上传植物图片");
        okButton.setActionCommand("OK");
        submitButton.setActionCommand("Submit");
        okButton.addActionListener(new ButtonClickListener());
        submitButton.addActionListener(new ButtonClickListener());
        controlPanel.add(okButton);
        controlPanel.add(submitButton);
        mainFrame.setVisible(true);
    }

    private class ButtonClickListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();
            if (command.equals("OK")) {
                Pattern p = Pattern.compile("\"(.*?)\"");
                String address=out();
                AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);
                client.setConnectionTimeoutInMillis(2000);
                JSONObject res = client.animalDetect(address, new HashMap<String, String>());
                String a = findS(res);
                createT(address,a);

            } else if (command.equals("Submit")) {
                String address1=out();
                AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);
                client.setConnectionTimeoutInMillis(2000);
                JSONObject res1 = client.plantDetect(address1, new HashMap<String, String>());
                String a1 = findS1(res1);
                createT(address1,a1);
            }
        }

        private void createT(String address, String a) {
            statusLabel.setSize(400,300);
            statusLabel.setLocation(580,200);
            statusLabel.setLayout(null);
            ImageIcon imageIcon=new ImageIcon(address);
            statusLabel.setIcon(imageIcon);
            statusLabel.setBounds(50, 50, imageIcon.getIconWidth(), imageIcon.getIconHeight());
            mainFrame.add(statusLabel);
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            statusLabel.setText(a);
            JButton okButton=new JButton();
            okButton.setBounds(imageIcon.getIconWidth()/2, imageIcon.getIconHeight()+100, 100, 50);

        }

        private String findS(JSONObject res) {
            String result = res.getJSONArray("result").get(0).toString();
            ArrayList<String> arrayList = new ArrayList<>();
            Pattern p = Pattern.compile("\"(.*?)\"");
            Matcher m = p.matcher(result);
            while (m.find()) {
                arrayList.add(m.group().toString());
            }
            return arrayList.get(3);
        }

    private String findS1(JSONObject res) {
        String result = res.getJSONArray("result").get(0).toString();
        ArrayList<String> arrayList = new ArrayList<>();
        Pattern p = Pattern.compile("\"(.*?)\"");
        Matcher m = p.matcher(result);
        while (m.find()) {
            arrayList.add(m.group().toString());
        }
            return arrayList.get(2);
    }
}

    private String out() {
//文件选择地址 JFileChooser jfchooser = new JFileChooser("C:\\Users\\zber\\Desktop"); FileNameExtensionFilter filter = new FileNameExtensionFilter("png", "jpg"); jfchooser.setFileFilter(filter); int option = jfchooser.showOpenDialog(null); if (option == JFileChooser.APPROVE_OPTION) { //说明选定了一个文件 String adress = jfchooser.getSelectedFile().getPath();//把文件地址取出给adress变量 return adress; } return null; } }

  

标签:statusLabel,图像识别,String,private,mainFrame,import,new,javaswing,百度
From: https://www.cnblogs.com/Rebz/p/17025306.html

相关文章

  • 黄执中表达课 百度网盘下载分享
    黄执中亲授,轻松掌握说服底层逻辑+实操方法,针对职场和生活场景,全面提升,用”“说服”改变人生。黄执中说服课百度网盘下载分享,有需要的可以直接搜v信公众号:稀饭资料库......
  • 黄执中情绪课 百度网盘下载分享
    黄执中亲授,轻松掌握说服底层逻辑+实操方法,针对职场和生活场景,全面提升,用”“说服”改变人生。黄执中说服课百度网盘下载分享,有需要的可以直接搜v信公众号:稀饭资料库......
  • 黄执中情绪沟通课 百度网盘下载分享
    黄执中亲授,轻松掌握说服底层逻辑+实操方法,针对职场和生活场景,全面提升,用”“说服”改变人生。黄执中说服课百度网盘下载分享,有需要的可以直接搜v信公众号:稀饭资料库......
  • 表单元素,模拟百度搜索
    ​  form表单中可以放入的标签就是表单元素 <!DOCTYPEhtml><html><head><metacharset="UTF-8"><title></title>......
  • 表单元素,模拟百度搜索
    ​  form表单中可以放入的标签就是表单元素 <!DOCTYPEhtml><html><head><metacharset="UTF-8"><title></title>......
  • 使用Alist白嫖百度一刻相册无限空间图床
    注意文中所示软件功能均为免费(截至文章发布日期)请珍惜免费软件,来之不易:Replit提供免费Docker服务,因此,在上传图片的时候,尽量在百度一刻相册客户端内上传(如果是能重定向......
  • vue项目使用百度地图
    //b_map.jsexportfunctionMP(ak){  returnnewPromise(function(resolve,reject){   window.init=function(){    resolve(BMap); ......
  • 令电脑中的所有程序无法访问百度
    查看百度服务器的所有ip地址 nslookupwww.baidu.com防火墙新建出站规则: 协议类型也可以选择任何,因为我们使用http/https上网,它也是属于tcp将上面通过`nslooku......
  • 腾讯经纬度转百度,百度转GCJ02
    //腾讯坐标转百度坐标exportconsttxMapToBdMap=(lng,lat)=>{//eslint-disable-next-linecamelcaseconstx_pi=(3.14159265358979324*3000.0)/180.......
  • WordPress添加百度第三方登录功能
    OpenSocial操作简单适用范围广;可操作性强;无第三方平台、无接口文件冗余;功能特点社交登陆:腾讯QQ、微博、微信、豆瓣、谷歌、微软、Facebook、Twitter、Github等社交分享:QQ......