AmazonRekognition-Java對接
阿新 • • 發佈:2020-07-03
AmazonRekognition-Java對接
AmazonRekognition類似於百度EasyDL,是一款影象分析服務,檢測影象分類及影象內容等。
1、專案建立
專案建立的方式有一下三種方式:
1:AWS官網建立
2:CLI建立
3:編碼建立(Java)
AWS官網建立文件見:
連結:https://pan.baidu.com/s/1WMnUIjNug53YEzWMXENVKw
提取碼:flny
2、依賴
<!--AmazonRekognition依賴--> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-rekognition</artifactId> <version>1.11.759</version> </dependency>
3、功能程式碼
1、客戶端驗證連線
客戶端連線方式及問題見:https://www.cnblogs.com/StefanieYang/p/13212508.html
2、功能
public class RekognitionClient{private final static String regoin = "us-east-1"; //區域設定 private final static String projectArn = ""; //專案Arn private final static String versionName = ""; //模型 private final static String projectVersionArn = ""; //專案模型Arn private final static int minInferenceUnits = 1; //最小推理數 private final static String projectName = ""; //專案名稱 // Rekognition客戶端生成/連線 public static AmazonRekognition getRekognition(){ AWSCredentials credentials = new ProfileCredentialsProvider().getCredentials(); AmazonRekognition amazonRekognition = AmazonRekognitionClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(credentials)) .build(); return amazonRekognition; } // Create public static void createProject(AmazonRekognition amazonRekognition){ try{ CreateProjectRequest request = new CreateProjectRequest().withProjectName(projectName);//建立請求 CreateProjectResult result = amazonRekognition.createProject(request); log.info("Project ARN:" + result.getProjectArn()); }catch(Exception e){ log.error(e.getMessage()); } } // Delete public static void deleteProject(AmazonRekognition amazonRekognition){ try{ DeleteProjectRequest request = new DeleteProjectRequest().withPojectArn(projectArn); DeleteProjectResult result = amazonRekognition.deleteProject(request); log.info("Project Status:" + result.getStatus()); }catch(Exception e){ log.error(e.getMessage()); } } // Start public static void startProject(AmazonRekognition amazonRekognition){ try{ StartProjectVersionRequest request = new StartProjectVersionRequest().withProjectVersionArn(projectVersionArn).withMinInferenceUnits(minInferenceUnits); StartProjectVersionResult result = amazonRekognition.startProjectVersion(request); log.info("Project Status:" + result.getStatus()); }catch(Exception e){ log.error(e.getMessage()); } } // Stop public static void stopProject(AmazonRekognition amazonRekognition){ try{ StopProjectVersionRequest request = new StopProjectVersionRequest().withProjectVersionArn(projectVersionArn); StopProjectVersionResult result = amazonRekognition.stopProjectVersion(request); log.info("Project Status:" + result.getStatus()); }catch(Exception e){ log.error(e.getMessage()); } } // LocalImage Upload Detect(非自定義專案) public static void localImageDetect(String imagePath,AmazonRekognition amazonRekognition){ ByteBuffer imageBytes = null; InputStream in = null; DetectLabelsRequest request = null; Image image; try{ in = new FileInputStream(new File(imagePath)); try{ imageBytes = ByteBuffer.wrap(IOUtils.toByteArraay(in)); request = new DetectLabelsRequest().withImage(new Image().withBytes(imageBytes)) .withMaxLabels(10).withMinconfidence(77F); }catch(IOException e){ log.error("輸入流轉ByteBuffer失敗!") } }catch(FileNotFoundException e){ log.error("檔案不存在!"); } DetectLabelsResult result = amazonRekognition.detectLabels(request); List<Label> list = result.getLabels(); for(Label label : list){ log.info("標籤名:"+label.getName+"標籤Confidence:"+label.getConfidence()); } if(in!=null){ try{ in.close(); }catch(IOException e){ log.error(e.getMessage()); } } } // Bucket Image Detect(非自定義專案) public static void bucketImageDetect(String key,String bucketName,AmazonRekognition amazonRekognition){ DetectLabelsRequest request = new DetectLabelsRequest(); request.withImage(new Image().withS3Object(new S3Object().withName(key).withBucket(bucketName))).withMaxLabels(10).withConfidence(75F); try{ DetectLabelsResult result = amazonRekognition.detectLabes(request); List<Label> list = result.getLabels(); for(Label label : list){ log.info("標籤名:"+label.getName+"標籤Confidence"+label.getConfidence()); } }catch(IOException e){ log.error(e.getMessage()); } } }