Spaces:
Sleeping
Sleeping
KevanSoon
commited on
Commit
·
37decb3
1
Parent(s):
c4fa409
added fastapi facedetection endpoint
Browse files
src/main/java/com/cs102/attendance/controller/FastApiCallerController.java
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package com.cs102.attendance.controller;
|
| 2 |
+
|
| 3 |
+
import org.springframework.web.bind.annotation.GetMapping;
|
| 4 |
+
import org.springframework.web.bind.annotation.RestController;
|
| 5 |
+
|
| 6 |
+
import com.cs102.attendance.service.FastApiCallerService;
|
| 7 |
+
|
| 8 |
+
@RestController
|
| 9 |
+
public class FastApiCallerController {
|
| 10 |
+
|
| 11 |
+
private final FastApiCallerService fastApiCallerService = new FastApiCallerService();
|
| 12 |
+
|
| 13 |
+
@GetMapping("/call-face-recognition")
|
| 14 |
+
public String callFaceRecognition() {
|
| 15 |
+
try {
|
| 16 |
+
return fastApiCallerService.callFaceRecognition();
|
| 17 |
+
} catch (Exception e) {
|
| 18 |
+
e.printStackTrace();
|
| 19 |
+
return "Error calling face-recognition API";
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
}
|
src/main/java/com/cs102/attendance/service/FastApiCallerService.java
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package com.cs102.attendance.service;
|
| 2 |
+
|
| 3 |
+
import java.net.URI;
|
| 4 |
+
import java.net.http.HttpClient;
|
| 5 |
+
import java.net.http.HttpRequest;
|
| 6 |
+
import java.net.http.HttpResponse;
|
| 7 |
+
|
| 8 |
+
import com.fasterxml.jackson.databind.JsonNode;
|
| 9 |
+
import com.fasterxml.jackson.databind.ObjectMapper;
|
| 10 |
+
|
| 11 |
+
public class FastApiCallerService {
|
| 12 |
+
|
| 13 |
+
private static final String FASTAPI_URL = "https://kevansoon-java-facerecognition-endpoint.hf.space/face-recognition";
|
| 14 |
+
|
| 15 |
+
private final HttpClient httpClient;
|
| 16 |
+
private final ObjectMapper objectMapper;
|
| 17 |
+
|
| 18 |
+
public FastApiCallerService() {
|
| 19 |
+
this.httpClient = HttpClient.newHttpClient();
|
| 20 |
+
this.objectMapper = new ObjectMapper();
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
public String callFaceRecognition() throws Exception {
|
| 24 |
+
HttpRequest request = HttpRequest.newBuilder()
|
| 25 |
+
.GET()
|
| 26 |
+
.uri(URI.create(FASTAPI_URL))
|
| 27 |
+
.build();
|
| 28 |
+
|
| 29 |
+
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
| 30 |
+
|
| 31 |
+
if (response.statusCode() == 200) {
|
| 32 |
+
JsonNode jsonNode = objectMapper.readTree(response.body());
|
| 33 |
+
return jsonNode.path("result").asText();
|
| 34 |
+
} else {
|
| 35 |
+
throw new RuntimeException("Failed to call FastAPI: HTTP " + response.statusCode());
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
}
|