# 資料庫與影像辨識 (使用java) 原代碼:https://github.com/chyhhwen/image-recognition-java 平常看到的都是python\ 不如用java試試看吧~ ## 主要 ### 引入opencv ``` static { System.loadLibrary("opencv_java310"); } ``` ### 劃出GUI ``` private void initialize() { frame = new JFrame(); frame.setBounds(100,100,850,650); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); label = new JLabel(""); label.setBounds(50,50,640,480); frame.getContentPane().add(label); } ``` ### 資料庫引用 ``` private static void sql(int put1) { query query = new query(); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); String date = dtf.format(LocalDateTime.now()); String input = "INSERT INTO `focus` VALUES(NULL,"; String put = input + "\"" + put1 + "\","; put = put + "\"" + date +"\");"; query.query(3,put); System.out.println(put); } ``` ### 照片名稱取得 ``` private static String picture() { String path = "照片位置"; String name = null; file file = new file(); name = file.use(3,""); System.out.println(name); path += name; path += ".jpg"; return path; } ``` ### 導入模型 ``` String xmlFile = "模型位址"; CascadeClassifier cascadeClassifier = new CascadeClassifier(xmlFile); ``` ### 判斷相機是否開機 ``` VideoCapture camera = new VideoCapture(0); camera.open(0); if(!camera.isOpened()) { System.out.println("carma open error"); } ``` ### 辨識並繪圖 ``` Mat gray = new Mat(frame.rows(),frame.cols(),frame.type()); MatOfRect matOfRect = new MatOfRect(); Imgproc.cvtColor(frame,gray,Imgproc.COLOR_RGB2GRAY); cascadeClassifier.detectMultiScale(gray,matOfRect); Imgcodecs.imwrite( picture(),frame); if(!matOfRect.empty()) { Rect[] rects = matOfRect.toArray(); int people = 0; for(Rect r : rects) { Imgproc.rectangle(frame,new Point(r.x,r.y),new Point(r.x+r.width,r.y+r.height), new Scalar(0,255,0),3); people += 1; } label.setIcon(new ImageIcon(matImage.matToBufferedImage(frame))); sql(people); } ``` ## 資料庫設定 ``` public static Connection getConnection() throws SQLException { String serverName = "localhost"; String database = "recognition"; String url = "jdbc:mysql://" + serverName + "/" + database; String user = "root"; String password = ""; return DriverManager.getConnection(url, user, password); } ``` ## 資料庫使用 ### 判斷套件是否安裝 ``` try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("找不到驅動程式類別"); e.printStackTrace(); } ``` ### 資料輸入 ``` Connection connection = sqlset.getConnection(); Statement statement = connection.createStatement(); try { statement.executeUpdate(String.format(put)); } catch (SQLException e) { e.printStackTrace(); } System.out.println("input yes"); connection.close(); ``` ### 資料查詢 ``` Connection connection = sqlset.getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(put); while (resultSet.next()) { out = resultSet.getString("id"); System.out.printf("%d ",resultSet.getInt(1)); System.out.printf("%s",resultSet.getString(3)); } connection.close(); ``` ### 資料庫每秒存取 ``` Statement statement = connection.createStatement(); String put1 = "SELECT * FROM `focus`"; ResultSet resultSet = statement.executeQuery(put1); String time = null; String hour = null; String minute = null; String second = null; String now_hour = null; String now_minute = null; String now_second = null; while (resultSet.next()) { out = resultSet.getString("id"); time = resultSet.getString(3); } /*資料庫時間*/ hour = time.substring(11,13); minute = time.substring(14,16); second = time.substring(17,19); System.out.println(time); System.out.println(hour); System.out.println(minute); System.out.println(second); /*現在時間*/ DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); String date = dtf.format(LocalDateTime.now()); now_hour = date.substring(11,13); now_minute = date.substring(14,16); now_second = date.substring(17,19); System.out.println(date); System.out.println(now_hour); System.out.println(now_minute); System.out.println(now_second); /*判斷不一樣*/ int yes = 0; if(Integer.parseInt(now_hour) != Integer.parseInt(hour)) { yes = 1; } if(Integer.parseInt(now_minute) != Integer.parseInt(minute)) { yes = 1; } if(Integer.parseInt(now_second) != Integer.parseInt(second)) { yes = 1; } System.out.println(yes); if(yes == 1) { try { statement.executeUpdate(String.format(put)); } catch (SQLException e) { e.printStackTrace(); } System.out.println("input yes"); yes = 0; connection.close(); } else { System.out.println("ERROR SQLPUT"); } ``` ## 檔案處理 ### 當下時間 ``` String now_hour = null; String now_minute = null; String now_second = null; String now_year = null; String now_month = null; String now_day = null; String data_name = null; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); String date = dtf.format(LocalDateTime.now()); now_hour = date.substring(11,13); now_minute = date.substring(14,16); now_second = date.substring(17,19); now_year = date.substring(0,4); now_month = date.substring(5,7); now_day = date.substring(8,10); data_name = now_year + now_month + now_day + "-"; data_name += now_hour + now_minute + now_second; return data_name; ``` ### 資料清除 ``` try { FileWriter fileWriter = new FileWriter(file_name); fileWriter.write(""); fileWriter.flush(); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } ``` ### 資料寫入 ``` BufferedWriter writer = null; File file = new File(file_name); try { writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file_name,true),"UTF-8")); writer.newLine(); writer.append(date); writer.flush(); writer.close(); } catch (Exception e) { throw new RuntimeException(e); } ``` ### 資料讀取 ``` BufferedReader reader = null; String out = null; try { reader = new BufferedReader(new InputStreamReader(new FileInputStream(file_name), "UTF-8")); String put = null; while((put = reader.readLine()) != null) { out = put; } reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return out; ```