File size: 2,021 Bytes
37decb3
 
fc9a733
 
 
 
 
 
0048178
fc9a733
 
 
37decb3
0048178
37decb3
 
fc9a733
37decb3
 
 
fc9a733
37decb3
 
fc9a733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37decb3
fc9a733
37decb3
fc9a733
 
37decb3
fc9a733
37decb3
 
 
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
package com.cs102.attendance.service;

import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

@Service
public class FastApiCallerService {

    private final RestTemplate restTemplate;
    private static final String FASTAPI_URL = "https://kevansoon-java-facerecognition-endpoint.hf.space/face-recognition";

    public FastApiCallerService() {
        this.restTemplate = new RestTemplate(new SimpleClientHttpRequestFactory());
    }

    public String callFaceRecognitionWithImage(MultipartFile image) throws Exception {
        // Wrap MultipartFile bytes with filename
        ByteArrayResource imageAsResource = new ByteArrayResource(image.getBytes()) {
            @Override
            public String getFilename() {
                return image.getOriginalFilename();
            }
        };

        // Prepare multipart form data
        LinkedMultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("image", imageAsResource);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

        ResponseEntity<String> response = restTemplate.postForEntity(FASTAPI_URL, requestEntity, String.class);

        if (response.getStatusCode().is2xxSuccessful()) {
            return response.getBody();  // Or parse JSON if needed
        } else {
            throw new RuntimeException("Failed to call FastAPI: HTTP " + response.getStatusCodeValue());
        }
    }
}