Skip to content

Commit 000a9fc

Browse files
authored
tolldemo: queue-reader starting position (#885)
* convert to spring boot 2.7.x application * add call out to vision ai service --------- Signed-off-by: Mark Nelson <mark.x.nelson@oracle.com>
1 parent 180dddb commit 000a9fc

File tree

11 files changed

+199
-12
lines changed

11 files changed

+199
-12
lines changed

tolldemo/journal-app/src/main/java/com/example/journalapp/model/Journal.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ public class Journal {
3636
private String tollDate;
3737
@Column(name = "TOLL_COST")
3838
private Integer tollCost;
39+
@Column(name = "DETECTED_VEHICLE_TYPE")
40+
private String detectedVehicleType;
3941

4042
}

tolldemo/journal-app/src/main/resources/schema.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ create table if not exists journal (
77
license_plate varchar2(10),
88
vehicle_type varchar2(10),
99
toll_date varchar2(25),
10-
toll_cost number
10+
toll_cost number,
11+
detected_vehicle_type varchar2(10)
1112
);

tolldemo/queue-reader/pom.xml

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>3.2.5</version>
8+
<version>2.7.18</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.example</groupId>
@@ -14,15 +14,19 @@
1414
<name>queue-reader</name>
1515
<description>queue-reader</description>
1616
<properties>
17-
<java.version>21</java.version>
18-
<spring-cloud.version>2023.0.1</spring-cloud.version>
19-
<oracle-springboot-starter.version>23.4.0</oracle-springboot-starter.version>
17+
<java.version>11</java.version>
18+
<spring-cloud.version>2021.0.8</spring-cloud.version>
19+
<oracle-springboot-starter.version>2.7.12</oracle-springboot-starter.version>
2020
</properties>
2121
<dependencies>
2222
<dependency>
2323
<groupId>org.springframework.boot</groupId>
2424
<artifactId>spring-boot-starter-actuator</artifactId>
2525
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
2630
<dependency>
2731
<groupId>org.springframework.cloud</groupId>
2832
<artifactId>spring-cloud-starter-openfeign</artifactId>
@@ -42,6 +46,20 @@
4246
<artifactId>spring-boot-starter-test</artifactId>
4347
<scope>test</scope>
4448
</dependency>
49+
50+
<!-- oci sdk -->
51+
<dependency>
52+
<groupId>com.oracle.oci.sdk</groupId>
53+
<artifactId>oci-java-sdk-common-httpclient-jersey</artifactId>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.oracle.oci.sdk</groupId>
57+
<artifactId>oci-java-sdk-core</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.oracle.oci.sdk</groupId>
61+
<artifactId>oci-java-sdk-aivision</artifactId>
62+
</dependency>
4563
</dependencies>
4664
<dependencyManagement>
4765
<dependencies>
@@ -52,6 +70,13 @@
5270
<type>pom</type>
5371
<scope>import</scope>
5472
</dependency>
73+
<dependency>
74+
<groupId>com.oracle.oci.sdk</groupId>
75+
<artifactId>oci-java-sdk-bom</artifactId>
76+
<version>3.41.0</version>
77+
<type>pom</type>
78+
<scope>import</scope>
79+
</dependency>
5580
</dependencies>
5681
</dependencyManagement>
5782

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.example.queuereader.client;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import org.springframework.stereotype.Component;
9+
10+
import com.oracle.bmc.ConfigFileReader;
11+
import com.oracle.bmc.aivision.AIServiceVisionClient;
12+
import com.oracle.bmc.aivision.model.AnalyzeImageDetails;
13+
import com.oracle.bmc.aivision.model.ImageClassificationFeature;
14+
import com.oracle.bmc.aivision.model.Label;
15+
import com.oracle.bmc.aivision.model.ObjectStorageImageDetails;
16+
import com.oracle.bmc.aivision.requests.AnalyzeImageRequest;
17+
import com.oracle.bmc.aivision.responses.AnalyzeImageResponse;
18+
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
19+
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
20+
21+
import lombok.extern.slf4j.Slf4j;
22+
23+
@Slf4j
24+
@Component
25+
public class AIVisionClient {
26+
27+
public String analyzeImage(String imageUrl) {
28+
ConfigFileReader.ConfigFile configFile = null;
29+
try {
30+
configFile = ConfigFileReader.parseDefault();
31+
} catch (IOException ioe) {
32+
return null;
33+
}
34+
AuthenticationDetailsProvider provider = new ConfigFileAuthenticationDetailsProvider(configFile);
35+
AIServiceVisionClient client = AIServiceVisionClient.builder().build(provider);
36+
37+
String objectName = "cars-images/htsngg9tpc-2/" + imageUrl;
38+
log.info("object name: " + objectName);
39+
40+
AnalyzeImageDetails analyzeImageDetails = AnalyzeImageDetails.builder()
41+
.compartmentId("ocid1.compartment.oc1..aaaaaaaaulnuhmwvxfhcj46mtomr64ublwg6unsocdsmj5yvkujach6zewla")
42+
.features(new ArrayList<>(Arrays.asList(ImageClassificationFeature.builder()
43+
.modelId("ocid1.aivisionmodel.oc1.iad.amaaaaaaq33dybyan4fwde3syguv3dpp3alpryjs4z7phur4gfhhdr7l3c3q").build())))
44+
.image(ObjectStorageImageDetails.builder()
45+
.bucketName("tolldemo")
46+
.namespaceName("maacloud")
47+
.objectName(objectName).build())
48+
.build();
49+
50+
AnalyzeImageRequest analyzeImageRequest = AnalyzeImageRequest.builder()
51+
.analyzeImageDetails(analyzeImageDetails)
52+
.opcRequestId("buggaluggs").build();
53+
54+
log.info("request: " + analyzeImageRequest);
55+
AnalyzeImageResponse response = client.analyzeImage(analyzeImageRequest);
56+
57+
List<Label> labels = response.getAnalyzeImageResult().getLabels();
58+
if (labels.isEmpty()) {
59+
return null;
60+
}
61+
return labels.get(0).getName() + "," + labels.get(0).getConfidence();
62+
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.example.queuereader.controller;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.PostMapping;
6+
import org.springframework.web.bind.annotation.RequestBody;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import com.example.queuereader.service.AIVisionService;
11+
12+
import lombok.extern.slf4j.Slf4j;
13+
14+
@Slf4j
15+
@RestController
16+
@RequestMapping("/image")
17+
public class TollImageValidationController {
18+
19+
private AIVisionService service;
20+
21+
TollImageValidationController(AIVisionService service) {
22+
this.service = service;
23+
}
24+
25+
@PostMapping("/analyze")
26+
public ResponseEntity<String> analyzeImage(@RequestBody String imageUrl) {
27+
log.info("imageUrl = " + imageUrl);
28+
String response = service.analyzeImage(imageUrl);
29+
if (response == null) {
30+
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
31+
}
32+
return new ResponseEntity<>(response, HttpStatus.OK);
33+
}
34+
35+
36+
}

tolldemo/queue-reader/src/main/java/com/example/queuereader/controller/TollReaderReceiver.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33

44
package com.example.queuereader.controller;
55

6+
import com.example.queuereader.service.AIVisionService;
67
import com.example.queuereader.service.JournalService;
78
import com.fasterxml.jackson.core.JsonProcessingException;
89
import com.fasterxml.jackson.databind.JsonNode;
910
import com.fasterxml.jackson.databind.ObjectMapper;
11+
import com.fasterxml.jackson.databind.node.ObjectNode;
12+
1013
import lombok.extern.slf4j.Slf4j;
1114
import org.springframework.jms.annotation.JmsListener;
1215
import org.springframework.stereotype.Component;
@@ -18,9 +21,11 @@ public class TollReaderReceiver {
1821
ObjectMapper objectMapper = new ObjectMapper();
1922

2023
private JournalService journalService;
24+
private AIVisionService aiVisionService;
2125

22-
public TollReaderReceiver(JournalService journalService) {
26+
public TollReaderReceiver(JournalService journalService, AIVisionService aiVisionService) {
2327
this.journalService = journalService;
28+
this.aiVisionService = aiVisionService;
2429
}
2530

2631
@JmsListener(destination = "TollGate")
@@ -29,7 +34,14 @@ public void receiveTollData(String tollData) {
2934
try {
3035
JsonNode tollDataJson = objectMapper.readTree(tollData);
3136
log.info(String.valueOf(tollDataJson));
32-
journalService.journal(tollDataJson);
37+
// call ai vision model to detect vehicle type
38+
String aiResult = aiVisionService.analyzeImage(tollDataJson.get("image").asText());
39+
log.info("result from ai (type,confidence): " + aiResult);
40+
String detectedVehicleType = aiResult.split(",")[0];
41+
// add the detected vehicle type
42+
JsonNode updatedTollDataJson = (JsonNode) ((ObjectNode)tollDataJson).put("detectedVehicleType", detectedVehicleType);
43+
44+
journalService.journal(updatedTollDataJson);
3345
} catch (JsonProcessingException e) {
3446
e.printStackTrace();
3547
}

tolldemo/queue-reader/src/main/java/com/example/queuereader/model/TollData.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ public class TollData {
1515
private String licensePlate;
1616
private String carType;
1717
private String timeStamp;
18+
private String detectedCarType;
1819
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.queuereader.service;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
import com.example.queuereader.client.AIVisionClient;
6+
7+
@Service
8+
public class AIVisionService {
9+
10+
private AIVisionClient client;
11+
12+
AIVisionService(AIVisionClient client) {
13+
this.client = client;
14+
}
15+
16+
public String analyzeImage(String imageUrl) {
17+
return client.analyzeImage(imageUrl);
18+
}
19+
20+
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
# mvn clean spring-boot:run -Dspring-boot.run.profiles=local
22
spring:
33
application:
4-
name: queue-reader
5-
threads:
6-
virtual:
7-
enabled: true
4+
name: queue-reader
85

96
datasource:
10-
url: jdbc:oracle:thin:@//localhost:1521/orclpdb1
7+
url: jdbc:oracle:thin:@//172.17.0.2:1521/freepdb1
118
username: tolldemo
129
password: Welcome12345
1310
driver-class-name: oracle.jdbc.OracleDriver
@@ -20,3 +17,9 @@ spring:
2017
max-pool-size: 30
2118
server:
2219
port: 9090
20+
21+
oracle:
22+
aq:
23+
url: jdbc:oracle:thin:@//172.17.0.2:1521/freepdb1
24+
username: tolldemo
25+
password: Welcome12345
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.queuereader;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import com.example.queuereader.client.AIVisionClient;
6+
7+
import lombok.extern.slf4j.Slf4j;
8+
9+
@Slf4j
10+
public class TestAIVisionClient {
11+
12+
@Test
13+
public void testImageAnalyze() {
14+
AIVisionClient client = new AIVisionClient();
15+
16+
String response = client.analyzeImage("suv/PHOTO_96.jpg");
17+
log.info(response.toString());
18+
}
19+
20+
}

0 commit comments

Comments
 (0)