-
Notifications
You must be signed in to change notification settings - Fork 1
/
AWSTextRekognition(instance2).java
196 lines (155 loc) · 8.7 KB
/
AWSTextRekognition(instance2).java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.IOException;
import com.amazon.sqs.javamessaging.*;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.*;
import com.amazonaws.services.s3.model.ListObjectsV2Request;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.amazonaws.services.sqs.*;
import com.amazonaws.services.sqs.model.CreateQueueRequest;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
import java.util.*;
import com.amazonaws.services.rekognition.model.*;
import com.amazonaws.services.rekognition.*;
import javax.jms.*;
import javax.jms.Queue;
import com.amazonaws.services.rekognition.AmazonRekognition;
import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder;
import com.amazonaws.services.sqs.model.AmazonSQSException;
import com.amazonaws.services.sqs.model.SendMessageBatchRequest;
import java.io.File; // Import the File class
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
@SpringBootApplication
class MyListener implements MessageListener {
@Override
public void onMessage(Message message) {
try {
// The credentials added are as ## , and are active , simply execute the jar file.
BasicAWSCredentials awsCreds = new BasicAWSCredentials("#####################" , "########################" );
Regions clientRegion = Regions.(mention-regionname);
String bucketName = 'Bucket-Name';
ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(bucketName);
ListObjectsV2Result result;
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withRegion(clientRegion)
.build();
AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withRegion(clientRegion)
.build();
result = s3Client.listObjectsV2(req);
for (S3ObjectSummary objectSummary : result.getObjectSummaries())
{
String m = (String) ((TextMessage) message).getText().toString();
if(objectSummary.getKey().contains(m))
{
// System.out.println("Received: " + ((TextMessage) message).getText());
String photo = objectSummary.getKey();
//text rekognition of the image from the queue
DetectTextRequest request = new DetectTextRequest()
.withImage(new Image()
.withS3Object(new S3Object()
.withName(photo)
.withBucket(bucketName)));
try {
DetectTextResult result1 = rekognitionClient.detectText(request);
List<TextDetection> textDetections = result1.getTextDetections();
if (!textDetections.isEmpty()) {
System.out.print("Text Detected lines and words for: " + photo + " ==> ");
for (TextDetection text: textDetections) {
System.out.print(" Text Detected: " + text.getDetectedText() + " , Confidence: " + text.getConfidence().toString());
System.out.println();
}
}
} catch(AmazonRekognitionException e) {
System.out.print("oops in the catch");
e.printStackTrace();
}
}
}
} catch (JMSException e) {
System.out.println("Please run the instance 1 first..");
}
}
}
public class CloudComputing2Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(CloudComputing2Application.class, args);
/***
Please replace Accesskey and SecretAcessKey with your aws credentials.
*/
BasicAWSCredentials awsCreds = new BasicAWSCredentials("#################" , "##########################3" );
Regions clientRegion = Regions.(mention-regionname);
String bucketName = 'bucket-name';
try {
AmazonSQS sqsClient = AmazonSQSClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withRegion(clientRegion)
.build();
//creating sqs queue even if it is not created it will wait for instance 2 to start first.
try {
// Create a new connection factory with all defaults (credentials and region) set automatically
SQSConnectionFactory connectionFactory = new SQSConnectionFactory(new ProviderConfiguration(), AmazonSQSClientBuilder.defaultClient());
// Create the connection.
SQSConnection connection = connectionFactory.createConnection();
// Get the wrapped client
AmazonSQSMessagingClientWrapper client = connection.getWrappedAmazonSQSClient();
if (!client.queueExists("MyQueue.fifo")) {
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("FifoQueue", "true");
attributes.put("ContentBasedDeduplication", "true");
client.createQueue(new CreateQueueRequest().withQueueName("MyQueue.fifo").withAttributes(attributes));
}
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create a queue identity and specify the queue name to the session
Queue queue = session.createQueue("MyQueue.fifo");
// Create a consumer for the 'MyQueue'.
MessageConsumer consumer = session.createConsumer(queue);
// Instantiate and set the message listener for the consumer.
consumer.setMessageListener(new MyListener());
// Start receiving incoming messages.
connection.start();
Thread.sleep(10000);
}catch(Exception e) {
System.out.println("Please run the instance 1, the program will wait for the queue to have elements.");
SQSConnectionFactory connectionFactory = new SQSConnectionFactory(new ProviderConfiguration(), AmazonSQSClientBuilder.defaultClient());
// Create the connection.
SQSConnection connection = connectionFactory.createConnection();
// Get the wrapped client
AmazonSQSMessagingClientWrapper client = connection.getWrappedAmazonSQSClient();
if (!client.queueExists("MyQueue.fifo")) {
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("FifoQueue", "true");
attributes.put("ContentBasedDeduplication", "true");
client.createQueue(new CreateQueueRequest().withQueueName("MyQueue.fifo").withAttributes(attributes));
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create a queue identity and specify the queue name to the session
Queue queue = session.createQueue("MyQueue.fifo");
// Create a consumer for the 'MyQueue'.
MessageConsumer consumer = session.createConsumer(queue);
// Instantiate and set the message listener for the consumer.
consumer.setMessageListener(new MyListener());
// Start receiving incoming messages.
connection.start();
Thread.sleep(10000);
}
}
// Wait for 1 second. The listener onMessage() method is invoked when a message is received.
}catch (AmazonServiceException e) {
System.out.println("Please run the instance 1 first.This program will wait..");
}
}
}