-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarDetection.java
More file actions
83 lines (68 loc) · 3.81 KB
/
CarDetection.java
File metadata and controls
83 lines (68 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package cmc.app;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.rekognition.RekognitionClient;
import software.amazon.awssdk.services.rekognition.model.*;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.SendMessageRequest;
import java.util.List;
public class CarDetection {
public static void main(String[] args) {
// Setting up AWS credentials
String profileName = "default";
ProfileCredentialsProvider credentialsProvider;
credentialsProvider = ProfileCredentialsProvider.create(profileName);
// Setting up region for AWS services
Region region = Region.US_EAST_1;
// Creating clients for S3, Rekognition, and SQS
S3Client s3 = S3Client.builder().region(region).credentialsProvider(credentialsProvider).build();
RekognitionClient rekognition = RekognitionClient.builder().region(region).credentialsProvider(credentialsProvider).build();
SqsClient sqs = SqsClient.builder().region(region).credentialsProvider(credentialsProvider).build();
// Setting up bucket name and SQS URL
String bucketName = "njit-cs-643";
String sqsQueueUrl = "https://sqs.us-east-1.amazonaws.com/261847612621/CMCImageQueue";
// Loop to process 10 images
for (int i = 1; i <= 10; i++) {
String imageName = i + ".jpg";
// Getting image from S3
GetObjectRequest.Builder getObjectRequestBuilder = GetObjectRequest.builder();
GetObjectRequest getObjectRequest = getObjectRequestBuilder.bucket(bucketName).key(imageName).build();
byte[] imageBytes = s3.getObjectAsBytes(getObjectRequest).asByteArray();
// Preparing request for Rekognition
Image.Builder imageBuilder = Image.builder();
Image img = imageBuilder.bytes(SdkBytes.fromByteArray(imageBytes)).build();
DetectLabelsRequest.Builder detectLabelsRequestBuilder = DetectLabelsRequest.builder();
DetectLabelsRequest detectLabelsRequest = detectLabelsRequestBuilder.image(img).maxLabels(10).build();
// Detecting labels using Rekognition
DetectLabelsResponse detectLabelsResponse = rekognition.detectLabels(detectLabelsRequest);
List<Label> detectedLabels = detectLabelsResponse.labels();
// Checking if 'Car' label exists and has a confidence over 90%
for (int j = 0; j < detectedLabels.size(); j++) {
Label label = detectedLabels.get(j);
if ("Car".equalsIgnoreCase(label.name()) && label.confidence() > 90.0) {
// Sending the image name to SQS if the above condition is true
SendMessageRequest sendMessageRequest = SendMessageRequest.builder()
.queueUrl(sqsQueueUrl)
.messageBody(imageName)
.build();
sqs.sendMessage(sendMessageRequest);
System.out.println("Sent image name " + imageName + " to SQS.");
}
}
}
// Sending termination message
SendMessageRequest terminationMsg = SendMessageRequest.builder()
.queueUrl(sqsQueueUrl)
.messageBody("-1")
.build();
sqs.sendMessage(terminationMsg);
System.out.println("Sent termination message to SQS.");
// Closing all AWS clients
rekognition.close();
s3.close();
sqs.close();
}
}