Skip to content

Commit

Permalink
Merge pull request #105 from Team-HealthC/feature/#102
Browse files Browse the repository at this point in the history
Feature/#102
  • Loading branch information
jeongjaino committed Nov 18, 2023
2 parents bb041b3 + faadaa4 commit faecfeb
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class TextDetectionFragment : Fragment() {
): View {
_binding = DataBindingUtil.inflate(inflater, R.layout.fragment_text_detection, container, false)
binding.viewModel = viewModel // xml viewModel init
binding.lifecycleOwner = viewLifecycleOwner
return binding.root
}

Expand Down Expand Up @@ -91,7 +92,7 @@ class TextDetectionFragment : Fragment() {
private fun showNegativeDialog(detectedList: List<Allergy>){
NegativeSignDialog(
context = requireContext(),
allergyList = detectedList
detectedAllergies = detectedList
).show()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

Expand All @@ -24,16 +25,20 @@ class TextDetectionViewModel @Inject constructor(
private val _imageUrl: MutableStateFlow<String> = MutableStateFlow("")
val imageUrl: StateFlow<String> get() = _imageUrl

private val _recognizedText: MutableStateFlow<String> = MutableStateFlow("")
val recognizedText: StateFlow<String> = _recognizedText.asStateFlow()

fun setImageUrl(imageUrl : String){
_imageUrl.value = imageUrl
}

fun checkAllergiesInKoreanText() {
viewModelScope.launch {
checkAllergiesInKoreanTextUseCase(_imageUrl.value)
.onSuccess { list ->
if(list.isNotEmpty())
_textDetectionUiEvent.emit(TextDetectionEvent.Detected(list))
.onSuccess { result ->
_recognizedText.value = result.recognizedText
if(result.allergies.isNotEmpty())
_textDetectionUiEvent.emit(TextDetectionEvent.Detected(result.allergies))
else
_textDetectionUiEvent.emit(TextDetectionEvent.NotDetected)
}
Expand All @@ -46,9 +51,10 @@ class TextDetectionViewModel @Inject constructor(
fun checkAllergiesInEnglishText() {
viewModelScope.launch {
checkAllergiesInEnglishTextUseCase(_imageUrl.value)
.onSuccess { list ->
if(list.isNotEmpty())
_textDetectionUiEvent.emit(TextDetectionEvent.Detected(list))
.onSuccess { result ->
_recognizedText.value = result.recognizedText
if(result.allergies.isNotEmpty())
_textDetectionUiEvent.emit(TextDetectionEvent.Detected(result.allergies))
else
_textDetectionUiEvent.emit(TextDetectionEvent.NotDetected)
}
Expand Down
Binary file modified client/app/src/main/res/drawable/gallery.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 16 additions & 15 deletions client/app/src/main/res/layout/fragment_camera.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@

<ImageButton
android:id="@+id/flash_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/baseline_flash_on_24"
android:background="@color/black"
app:tint="@color/white"
Expand Down Expand Up @@ -121,25 +121,27 @@

</com.google.android.material.chip.ChipGroup>

<androidx.appcompat.widget.AppCompatImageButton
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/goToGalleryButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="32dp"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="@drawable/gallery"
android:background="@color/black"
app:backgroundTint="@color/light_black"
app:fabCustomSize="48dp"
app:maxImageSize="24dp"
app:tint="@color/white"
app:layout_constraintBottom_toBottomOf="@+id/capture_image_button"
app:layout_constraintEnd_toStartOf="@+id/capture_image_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/capture_image_button"
app:tint="@color/white" />
app:layout_constraintTop_toTopOf="@id/capture_image_button" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/capture_image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="32dp"
app:fabCustomSize="64dp"
app:backgroundTint="@color/light_gray"
app:layout_constraintBottom_toBottomOf="parent"
Expand All @@ -151,17 +153,16 @@
android:id="@+id/switch_camera_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:clickable="true"
android:src="@drawable/switch_camera"
app:backgroundTint="@color/light_black"
app:fabCustomSize="48dp"
app:maxImageSize="32dp"
app:tint="@color/white"
app:layout_constraintBottom_toBottomOf="@+id/capture_image_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/capture_image_button"
app:layout_constraintTop_toTopOf="@+id/capture_image_button"
app:maxImageSize="28dp"
app:tint="@color/white" />
app:layout_constraintTop_toTopOf="@+id/capture_image_button" />

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Expand Down
139 changes: 124 additions & 15 deletions client/app/src/main/res/layout/fragment_text_detection.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,136 @@
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:background="@color/light_gray">

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/Capture_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:imageUrl="@{viewModel.imageUrl}"
<androidx.cardview.widget.CardView
android:id="@+id/cd_t_d_result"
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<TextView
android:id="@+id/tv_t_d_result_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/sc_dream_nor"
android:text="@string/recongnized_text"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_t_d_result_title" >

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light_gray">

<TextView
android:id="@+id/tv_t_d_recognized_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{viewModel.recognizedText}"
android:textSize="14sp"
android:textColor="@color/black"
android:fontFamily="@font/sc_dream_nor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

</androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

<androidx.cardview.widget.CardView
android:id="@+id/cd_t_d_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:placeholder="@{@drawable/health_c}" />
app:layout_constraintTop_toTopOf="parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">

<ImageButton
android:id="@+id/back_to_camera_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_arrow_back_ios_new_24"
app:backgroundTint="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tv_t_d_top_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:fontFamily="@font/sc_dream_nor"
android:text="@string/korea_ocr_camera"
android:textColor="@color/black"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/back_to_camera_button"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

<ImageButton
android:id="@+id/back_to_camera_button"
android:layout_width="wrap_content"
<androidx.cardview.widget.CardView
android:id="@+id/cd_t_d_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:src="@drawable/ic_baseline_arrow_back_ios_new_24"
app:backgroundTint="@color/white"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toTopOf="@+id/cd_t_d_result"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toBottomOf="@+id/cd_t_d_top">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_t_d_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:imageUrl="@{viewModel.imageUrl}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:placeholder="@{@drawable/health_c}"
tools:layout_height="300dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
2 changes: 1 addition & 1 deletion client/app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="light_black">#FF474747</color>
<color name="light_black">#FF242425</color>
<color name="white">#FFFFFFFF</color>
<color name="green">#FF2BAE66</color>
<color name="white_yellow">#FFFCF6F5</color>
Expand Down
1 change: 1 addition & 0 deletions client/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@
<string name="failed_object_detection_title">음식을 인식할 수 없습니다.\n더 멀리서 찍거나, 다른 사진을 사용해주세요 !</string>
<string name="object_detection_result">인식된 음식</string>
<string name="food_detection">음식 인식</string>
<string name="recongnized_text">인식된 텍스트</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,16 @@ data class Allergy(
fun toEnglish(): Allergy {
return when (this) {
Allergy("돼지고기") -> { Allergy("pork") }

Allergy("대두") -> { Allergy("soybean") }

Allergy("땅콩") -> { Allergy("peanut") }

Allergy("계란") -> { Allergy("egg") }

Allergy("우유") -> { Allergy("milk") }

Allergy("") -> { Allergy("flour") }

Allergy("복숭아") -> { Allergy("peach") }

Allergy("견과류") -> { Allergy("nuts") }

Allergy("갑각류") -> { Allergy("crab") }

Allergy("조개류") -> { Allergy("clam") }

Allergy("생선") -> { Allergy("fish") }

else -> { this }
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.healthc.domain.model.detection

import com.healthc.domain.model.auth.Allergy

data class AllergyTextDetection (
val allergies: List<Allergy>,
val recognizedText: String,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.healthc.domain.model.detection

// 오직 OCR, 문자 인식만을 위한 도메인 모델
data class TextDetection(
val recognizedText: String
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.healthc.domain.usecase.detection

import com.healthc.domain.model.auth.Allergy
import com.healthc.domain.model.detection.AllergyTextDetection
import com.healthc.domain.repository.DetectionRepository
import com.healthc.domain.repository.UserRepository
import javax.inject.Inject
Expand All @@ -9,23 +10,27 @@ class CheckAllergiesInEnglishTextUseCase @Inject constructor(
private val userRepository: UserRepository,
private val detectionRepository: DetectionRepository
){
suspend operator fun invoke(imageUri: String) : Result<List<Allergy>> {
suspend operator fun invoke(imageUri: String) : Result<AllergyTextDetection> {
return runCatching {
val response = userRepository.getUser().getOrThrow()
val userAllergies = response.allergies.map { allergy ->
allergy.toEnglish()
}

val recognizedText =
detectionRepository.getDetectedEnglishText(imageUri).getOrThrow().toString()
detectionRepository.getDetectedEnglishText(imageUri).getOrThrow().recognizedText

val detectedAllergies = mutableListOf<Allergy>()
userAllergies.forEach { allergy ->
if (recognizedText.contains(allergy.allergy)) {
detectedAllergies.add(allergy)
}
}
detectedAllergies

AllergyTextDetection(
allergies = detectedAllergies,
recognizedText = recognizedText,
)
}
}
}
Loading

0 comments on commit faecfeb

Please sign in to comment.