Commit 948ec935 authored by Chamod Ishankha's avatar Chamod Ishankha

accept live video from web cam -> backend -> python model to process and return prediction

parent 8e3182b4
......@@ -126,6 +126,28 @@
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<!--handle live feeds-->
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.tyrus.bundles</groupId>
<artifactId>tyrus-standalone-client</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>opencv</artifactId>
<version>4.9.0-1.5.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies>
<build>
......
......@@ -2,8 +2,12 @@ package com.kaluwa.enterprises.babycarebackendservice.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
@Configuration
public class AppConfig {
......@@ -14,4 +18,15 @@ public class AppConfig {
objectMapper.registerModule(new JavaTimeModule());
return objectMapper;
}
@Bean
public WebSocketClient webSocketClient() {
return new StandardWebSocketClient();
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
}
......@@ -45,7 +45,8 @@ public class SecurityConfig {
"/configuration/security",
"/swagger-ui/**",
"/webjars/**",
"/swagger-ui.html"
"/swagger-ui.html",
"/emotional/video-process"
};
@Bean
......
package com.kaluwa.enterprises.babycarebackendservice.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kaluwa.enterprises.babycarebackendservice.socketHandlers.EmotionPrediction;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import java.sql.Blob;
class WebSocketClient {
private WebSocketSession session;
public WebSocketClient() {
try {
this.session = new StandardWebSocketClient()
.doHandshake(new MyWebSocketHandler(), "ws://localhost:8000/ws/emotion")
.get();
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendBytesToWebSocket(byte[] bytes) {
try {
session.sendMessage(new BinaryMessage(bytes));
} catch (Exception e) {
e.printStackTrace();
}
}
static ObjectMapper objectMapper = new ObjectMapper();
static class MyWebSocketHandler extends TextWebSocketHandler {
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
// This method will be called when the server sends a text message
// Deserialize JSON array of objects into an array of Java objects
EmotionPrediction[] predictions = objectMapper.readValue(message.getPayload(), EmotionPrediction[].class);
// Print each element of the array
for (EmotionPrediction prediction : predictions) {
System.out.println("Emotion: " + prediction.getEmotion());
System.out.println("Bounding Box:");
System.out.println(" X: " + prediction.getBounding_box().getX());
System.out.println(" Y: " + prediction.getBounding_box().getY());
System.out.println(" Width: " + prediction.getBounding_box().getWidth());
System.out.println(" Height: " + prediction.getBounding_box().getHeight());
}
}
}
}
\ No newline at end of file
package com.kaluwa.enterprises.babycarebackendservice.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.BinaryWebSocketHandler;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
import javax.sql.rowset.serial.SerialBlob;
import java.sql.Blob;
import java.util.Map;
@Configuration
@EnableWebSocket
@Slf4j
public class WebSocketConfig implements WebSocketConfigurer {
WebSocketClient webSocketClient = new WebSocketClient();
@Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
container.setMaxTextMessageBufferSize(500000);
container.setMaxBinaryMessageBufferSize(500000);
return container;
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new VideoFrameHandler(), "/emotional/video-process")
.setAllowedOrigins("*");
}
@Component
class VideoFrameHandler extends BinaryWebSocketHandler {
@Override
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
byte[] binaryData = message.getPayload().array();
// Process the binary data representing the video frame
// log.info("Received binary data from client: {}", binaryData);
// Here you can process the binary data as needed, such as saving it to a file, performing image processing, etc.
webSocketClient.sendBytesToWebSocket(binaryData);
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) {
super.handleTextMessage(session, message);
}
}
}
\ No newline at end of file
......@@ -3,5 +3,6 @@ package com.kaluwa.enterprises.babycarebackendservice.constants;
public class TableNames {
public final static String USER_TABLE = "users";
public final static String BABY_TABLE = "babies";
}
package com.kaluwa.enterprises.babycarebackendservice.dao;
import com.kaluwa.enterprises.babycarebackendservice.model.Baby;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface BabyDao extends JpaRepository<Baby, Long> {
}
package com.kaluwa.enterprises.babycarebackendservice.dto;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BabyDto {
private Long babyId;
@NotNull(message = "Baby name is required")
@NotEmpty(message = "Baby name is required")
private String babyName;
private String babyGender;
private LocalDate babyDob;
private String babyWeight;
private UserDto user;
@NotNull(message = "Logged user id is required")
@NotEmpty(message = "Logged user id is required")
private Long userId;
}
package com.kaluwa.enterprises.babycarebackendservice.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EmotionDetectionResponse {
private String emotion;
private String feeds;
}
package com.kaluwa.enterprises.babycarebackendservice.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseDto {
private Long id;
private String message;
}
package com.kaluwa.enterprises.babycarebackendservice.dto;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
......@@ -13,6 +14,8 @@ public class UserDto {
private String lastName;
private String email;
private String phone;
@JsonIgnore
private String role;
@JsonIgnore
private String status;
}
package com.kaluwa.enterprises.babycarebackendservice.mappers;
import com.kaluwa.enterprises.babycarebackendservice.dto.BabyDto;
import com.kaluwa.enterprises.babycarebackendservice.model.Baby;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import java.util.List;
@Mapper(componentModel = "spring")
public interface BabyMapper {
@Mappings({
@Mapping(source = "user.userId", target = "userId")
})
BabyDto toDto(Baby baby);
@Mappings({
@Mapping(target = "user.userId", source = "userId")
})
Baby toEntity(BabyDto babyDto);
@Mappings({
@Mapping(source = "user.userId", target = "userId")
})
List<BabyDto> toDtoList(List<Baby> babyList);
}
package com.kaluwa.enterprises.babycarebackendservice.model;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import static com.kaluwa.enterprises.babycarebackendservice.constants.TableNames.BABY_TABLE;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = BABY_TABLE)
public class Baby {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long babyId;
private String babyName;
private String babyGender;
private LocalDate babyDob;
private String babyWeight;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId", referencedColumnName = "userId", nullable = false)
private User user;
}
......@@ -44,7 +44,5 @@ public class AuthenticationController {
// change password
// logout
}
package com.kaluwa.enterprises.babycarebackendservice.rest;
import com.kaluwa.enterprises.babycarebackendservice.dto.BabyDto;
import com.kaluwa.enterprises.babycarebackendservice.dto.ResponseDto;
import com.kaluwa.enterprises.babycarebackendservice.service.BabyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/baby")
@Slf4j
public class BabyController {
private final BabyService babyService;
public BabyController(BabyService babyService) {
this.babyService = babyService;
}
@PostMapping()
public ResponseEntity<BabyDto> createBaby(@RequestBody BabyDto babyDto) {
log.info("Inside baby controller createBaby method");
return new ResponseEntity<>(babyService.createBaby(babyDto), HttpStatus.CREATED);
}
@GetMapping()
public ResponseEntity<List<BabyDto>> getAllbabies() {
log.info("Inside baby controller getAllbabies method");
return ResponseEntity.ok(babyService.getAllBabies());
}
@GetMapping("/{id}")
public ResponseEntity<BabyDto> getBabyById(@PathVariable Long babyId) {
log.info("Inside baby controller getBabyById method");
return ResponseEntity.ok(babyService.getBabyById(babyId));
}
@PutMapping("/{id}")
public ResponseEntity<BabyDto> updateBaby(@PathVariable Long babyId, @RequestBody BabyDto babyDto) {
log.info("Inside baby controller updateBaby method");
return ResponseEntity.ok(babyService.updateBaby(babyId, babyDto));
}
@DeleteMapping("/{id}")
public ResponseEntity<ResponseDto> deleteBaby(@PathVariable Long babyId) {
log.info("Inside baby controller deleteBaby method");
return ResponseEntity.ok(babyService.deleteBaby(babyId));
}
}
//package com.kaluwa.enterprises.babycarebackendservice.rest;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.web.bind.annotation.*;
//
//import java.util.Map;
//
//@RestController
//@RequestMapping("/emotional")
//@CrossOrigin(origins = "*")
//@Slf4j
//public class EmotionalDetectionController {
//
// @PostMapping("/process-video")
// public Map<String, String> processVideo(@RequestBody String imageData) {
// // Send the image data to the Python FastAPI server for processing
// // Implement WebSocket or HTTP client to send data to FastAPI server
// log.info("Received video frame for processing");
// return Map.of("status", "Received video frame for processing");
// }
//
//}
package com.kaluwa.enterprises.babycarebackendservice.rest;
import com.kaluwa.enterprises.babycarebackendservice.dto.ResponseDto;
import com.kaluwa.enterprises.babycarebackendservice.dto.UserDto;
import com.kaluwa.enterprises.babycarebackendservice.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
// get user by id
@GetMapping("/{userId}")
public ResponseEntity<UserDto> getUserById(@PathVariable Long userId) {
log.info("Inside user controller get user by id method");
return new ResponseEntity<>(userService.getUserById(userId), HttpStatus.OK);
}
// get all users
@GetMapping("/all")
public ResponseEntity<List<UserDto>> getAllUsers() {
log.info("Inside user controller get all users method");
return new ResponseEntity<>(userService.getAllUsers(), HttpStatus.OK);
}
// update user
@PutMapping("/{userId}")
public ResponseEntity<UserDto> updateUser(@PathVariable Long userId, @RequestBody UserDto userDto) {
log.info("Inside user controller update user method");
return new ResponseEntity<>(userService.updateUser(userId, userDto), HttpStatus.OK);
}
// delete user
@DeleteMapping("/{userId}")
public ResponseEntity<ResponseDto> deleteUser(@PathVariable Long userId) {
log.info("Inside user controller delete user method");
return new ResponseEntity<>(userService.deleteUser(userId), HttpStatus.OK);
}
}
package com.kaluwa.enterprises.babycarebackendservice.service;
import com.kaluwa.enterprises.babycarebackendservice.dto.BabyDto;
import com.kaluwa.enterprises.babycarebackendservice.dto.ResponseDto;
import java.util.List;
public interface BabyService {
BabyDto createBaby(BabyDto babyDto);
List<BabyDto> getAllBabies();
BabyDto getBabyById(Long babyId);
BabyDto updateBaby(Long babyId, BabyDto babyDto);
ResponseDto deleteBaby(Long babyId);
}
package com.kaluwa.enterprises.babycarebackendservice.service;
import com.kaluwa.enterprises.babycarebackendservice.dto.AuthenticationDto;
import com.kaluwa.enterprises.babycarebackendservice.dto.LoginRequest;
import com.kaluwa.enterprises.babycarebackendservice.dto.RegisterRequest;
import com.kaluwa.enterprises.babycarebackendservice.dto.UserDto;
import com.kaluwa.enterprises.babycarebackendservice.dto.*;
import java.util.List;
public interface UserService {
AuthenticationDto login(LoginRequest loginRequest);
UserDto register(RegisterRequest registerRequest);
UserDto getUserById(Long userId);
List<UserDto> getAllUsers();
UserDto updateUser(Long userId, UserDto userDto);
ResponseDto deleteUser(Long userId);
}
package com.kaluwa.enterprises.babycarebackendservice.service.impl;
import com.kaluwa.enterprises.babycarebackendservice.dao.BabyDao;
import com.kaluwa.enterprises.babycarebackendservice.dto.BabyDto;
import com.kaluwa.enterprises.babycarebackendservice.dto.ResponseDto;
import com.kaluwa.enterprises.babycarebackendservice.error.BadRequestAlertException;
import com.kaluwa.enterprises.babycarebackendservice.mappers.BabyMapper;
import com.kaluwa.enterprises.babycarebackendservice.model.Baby;
import com.kaluwa.enterprises.babycarebackendservice.service.BabyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class BabyServiceImpl implements BabyService {
private static final Logger log = LoggerFactory.getLogger(BabyServiceImpl.class);
private final BabyDao babyDao;
private final BabyMapper babyMapper;
public BabyServiceImpl(BabyDao babyDao, BabyMapper babyMapper) {
this.babyDao = babyDao;
this.babyMapper = babyMapper;
}
@Override
public BabyDto createBaby(BabyDto babyDto) {
log.info("Inside baby service createBaby method");
try {
return babyMapper.toDto(babyDao.save(babyMapper.toEntity(babyDto)));
} catch (Exception e) {
e.printStackTrace();
throw new BadRequestAlertException(e.getMessage(), "baby", "baby.error");
}
}
@Override
public List<BabyDto> getAllBabies() {
log.info("Inside baby service getAllBabies method");
try {
return babyMapper.toDtoList(babyDao.findAll());
} catch (Exception e) {
e.printStackTrace();
throw new BadRequestAlertException(e.getMessage(), "baby", "baby.error");
}
}
@Override
public BabyDto getBabyById(Long babyId) {
log.info("Inside baby service getBabyById method");
try {
Optional<Baby> babyOp = babyDao.findById(babyId);
if (babyOp.isEmpty()) {
throw new BadRequestAlertException("Baby not found", "baby", "baby.error");
} else {
return babyMapper.toDto(babyOp.get());
}
} catch (Exception e) {
e.printStackTrace();
throw new BadRequestAlertException(e.getMessage(), "baby", "baby.error");
}
}
@Override
public BabyDto updateBaby(Long babyId, BabyDto babyDto) {
log.info("Inside baby service updateBaby method");
try {
Optional<Baby> babyOp = babyDao.findById(babyId);
if (babyDto.getBabyId() == null) {
throw new BadRequestAlertException("Baby id is required", "baby", "baby.error");
} else if (!babyId.equals(babyDto.getBabyId())) {
throw new BadRequestAlertException("Baby id mismatch", "baby", "baby.error");
} else if (babyOp.isEmpty()) {
throw new BadRequestAlertException("Baby not found", "baby", "baby.error");
} else {
return babyMapper.toDto(babyDao.save(babyMapper.toEntity(babyDto)));
}
} catch (Exception e) {
e.printStackTrace();
throw new BadRequestAlertException(e.getMessage(), "baby", "baby.error");
}
}
@Override
public ResponseDto deleteBaby(Long babyId) {
log.info("Inside baby service deleteBaby method");
try {
Optional<Baby> babyOp = babyDao.findById(babyId);
if (babyOp.isEmpty()) {
throw new BadRequestAlertException("Baby not found", "baby", "baby.error");
} else {
babyDao.deleteById(babyId);
ResponseDto responseDto = new ResponseDto();
responseDto.setId(babyId);
responseDto.setMessage("Baby deleted successfully");
return responseDto;
}
} catch (Exception e) {
e.printStackTrace();
throw new BadRequestAlertException(e.getMessage(), "baby", "baby.error");
}
}
}
......@@ -14,6 +14,8 @@ import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import static com.kaluwa.enterprises.babycarebackendservice.constants.Common.TOKEN_TYPE;
import static com.kaluwa.enterprises.babycarebackendservice.constants.Status.STATUS_NEW;
......@@ -69,6 +71,7 @@ public class UserServiceImpl implements UserService {
return authenticationDto;
} catch (Exception e) {
e.printStackTrace();
throw new BadRequestAlertException(e.getMessage(), "User", "User login failed");
}
}
......@@ -96,6 +99,85 @@ public class UserServiceImpl implements UserService {
userDto = userMapper.toDto(userDao.save(user));
return userDto;
} catch (Exception e) {
e.printStackTrace();
throw new BadRequestAlertException(e.getMessage(), "User", "User registration failed");
}
}
@Override
public UserDto getUserById(Long userId) {
log.info("Inside user service get user by id method");
try {
Optional<User> userOp = userDao.findById(userId);
if (userOp.isEmpty()) {
throw new BadRequestAlertException("User not found", "User", "User not found");
} else {
return userMapper.toDto(userOp.get());
}
} catch (Exception e) {
e.printStackTrace();
throw new BadRequestAlertException(e.getMessage(), "User", "User registration failed");
}
}
@Override
public List<UserDto> getAllUsers() {
log.info("Inside user service get all users method");
try {
List<User> users = userDao.findAll();
return userMapper.listToDto(users);
} catch (Exception e) {
e.printStackTrace();
throw new BadRequestAlertException(e.getMessage(), "User", "User registration failed");
}
}
@Override
public UserDto updateUser(Long userId, UserDto userDto) {
log.info("Inside user service update user method");
try {
Optional<User> userOp = userDao.findById(userId);
if (userDto.getUserId() == null) {
throw new BadRequestAlertException("User id is required", "User", "User update failed");
} else if (!userId.equals(userDto.getUserId())) {
throw new BadRequestAlertException("User id mismatch", "User", "User update failed");
} else if (userOp.isEmpty()) {
throw new BadRequestAlertException("User not found", "User", "User not found");
} else if (userDto.getPhone() == null || userDto.getPhone().isEmpty()) {
throw new BadRequestAlertException("Phone number is required", "User", "User update failed");
} else if (userDto.getFirstName() == null || userDto.getFirstName().isEmpty()) {
throw new BadRequestAlertException("First name is required", "User", "User update failed");
} else if (userDto.getLastName() == null || userDto.getLastName().isEmpty()) {
throw new BadRequestAlertException("Last name is required", "User", "User update failed");
} else {
User user = userOp.get();
user.setFirstName(userDto.getFirstName());
user.setLastName(userDto.getLastName());
user.setPhone(userDto.getPhone());
return userMapper.toDto(userDao.save(user));
}
} catch (Exception e) {
e.printStackTrace();
throw new BadRequestAlertException(e.getMessage(), "User", "User registration failed");
}
}
@Override
public ResponseDto deleteUser(Long userId) {
log.info("Inside user service delete user method");
try {
Optional<User> userOp = userDao.findById(userId);
if (userOp.isEmpty()) {
throw new BadRequestAlertException("User not found", "User", "User not found");
} else {
userDao.deleteById(userId);
ResponseDto responseDto = new ResponseDto();
responseDto.setId(userId);
responseDto.setMessage("User deleted successfully");
return responseDto;
}
} catch (Exception e) {
e.printStackTrace();
throw new BadRequestAlertException(e.getMessage(), "User", "User registration failed");
}
}
......
package com.kaluwa.enterprises.babycarebackendservice.socketHandlers;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BoundingBox {
private int x;
private int y;
private int width;
private int height;
}
package com.kaluwa.enterprises.babycarebackendservice.socketHandlers;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EmotionPrediction {
private String emotion;
private BoundingBox bounding_box;
}
\ No newline at end of file
//package com.kaluwa.enterprises.babycarebackendservice.socketHandlers;
//
//// Program to eastablish the socket connection
//
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//
//import com.kaluwa.enterprises.babycarebackendservice.config.WebSocketClient;
//import org.springframework.web.socket.*;
//import org.springframework.web.socket.handler.BinaryWebSocketHandler;
//import org.springframework.web.socket.handler.TextWebSocketHandler;
//
//// Socket-Connection Configuration class
//public class SocketConnectionHandlers {
//
//
//
//}
//
......@@ -13,7 +13,7 @@ spring:
jpa:
hibernate:
ddl-auto: update
show-sql: true
show-sql: false
properties:
hibernate:
format_sql: true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment