Skip to content

Commit

Permalink
feat: ✨ implementation ocr module for blank guns
Browse files Browse the repository at this point in the history
  • Loading branch information
nutfdt committed Aug 30, 2024
1 parent 3c27f86 commit 8d337ec
Show file tree
Hide file tree
Showing 14 changed files with 339 additions and 207 deletions.
1 change: 1 addition & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RUN apt update && apt install -y \
libglib2.0-0 \
curl \
gcc \
patch \
&& rm -rf /var/lib/apt/lists/*

# install python libraries
Expand Down
2 changes: 1 addition & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ autodynatrace==2.0.0
PyJWT==2.8.0
cryptography==42.0.8
# ML
basegun-ml==1.0.1
basegun-ml==2.0.1
# Dev
pytest==7.4.3
coverage==7.3.2
Expand Down
36 changes: 34 additions & 2 deletions backend/src/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from basegun_ml.classification import get_typology
from basegun_ml.measure import get_lengths
from basegun_ml.ocr import is_alarm_weapon, LowQuality, MissingText
from fastapi import (
APIRouter,
BackgroundTasks,
Expand Down Expand Up @@ -37,7 +38,6 @@ def home():
def version():
return APP_VERSION


@router.post("/upload")
async def imageupload(
request: Request,
Expand Down Expand Up @@ -74,8 +74,12 @@ async def imageupload(

gun_length, gun_barrel_length, conf_card = None, None, None
if label in TYPOLOGIES_MEASURED and confidence_level != "low":
gun_length, gun_barrel_length, conf_card = get_lengths(img_bytes)
try:
gun_length, gun_barrel_length, conf_card = get_lengths(img_bytes)

except Exception as e:
extras_logging["bg_error_type"] = e.__class__.__name__
logging.exception(e, extra=extras_logging)
# Temporary fix while ML package send 0 instead of None
# https://github.com/dnum-mi/basegun-ml/issues/14
gun_length = None if gun_length == 0 else gun_length
Expand Down Expand Up @@ -106,6 +110,34 @@ async def imageupload(
logging.exception(e, extra=extras_logging)
raise HTTPException(status_code=500, detail=str(e))

@router.post("/identification-blank-gun")
async def imageblankgun(
image: UploadFile = File(...),
):
try:
img_bytes = image.file.read()
# Process image with ML models
alarm_model = is_alarm_weapon(img_bytes)
return {
"alarm_model": alarm_model,
"missing_text": False,
"low_quality": False,
}

except LowQuality:
return {
"alarm_model": None,
"low_quality": True,
"missing_text": False,
}
except MissingText:
return {
"alarm_model": None,
"low_quality": False,
"missing_text": True,
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))

@router.post("/identification-feedback")
async def log_feedback(request: Request, user_id: Union[str, None] = Cookie(None)):
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/api/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
IDENTIFICATION_FEEDBACK_ROUTE,
TUTORIAL_FEEDBACK_ROUTE,
UPLOAD_PHOTO_FOR_DETECTION_ROUTE,
UPLOAD_PHOTO_FOR_BLANK_GUN_DETECTION_ROUTE,
} from "./api-routes";

export const uploadPhotoForDetection = async (file: File) => {
Expand Down Expand Up @@ -50,3 +51,13 @@ export const sendExpertiseForm = async (
console.error("Erreur lors de l'envoi du formulaire :", error);
}
};

export const uploadPhotoForBlankGunDetection = async (file: File) => {
const fd = new FormData();
fd.append("image", file, file.name);
const { data } = await axios.post(
UPLOAD_PHOTO_FOR_BLANK_GUN_DETECTION_ROUTE,
fd,
);
return data;
};
2 changes: 2 additions & 0 deletions frontend/src/api/api-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export const TUTORIAL_FEEDBACK_ROUTE = "/tutorial-feedback";
export const IDENTIFICATION_FEEDBACK_ROUTE = "/identification-feedback";
export const IDENTIFICATION_DUMMY_ROUTE = "/identification-dummy";
export const ASK_FOR_OPINION_ROUTE = "/expert-contact";
export const UPLOAD_PHOTO_FOR_BLANK_GUN_DETECTION_ROUTE =
"/identification-blank-gun";
Binary file added frontend/src/assets/missing_marking.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion frontend/src/components/ResultPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SnackbarAlert from "@/components/SnackbarAlert.vue";
import {
TYPOLOGIES,
MEASURED_GUNS_TYPOLOGIES,
isAlarmGun,
} from "@/utils/firearms-utils/index";
import { isUserUsingCrosscall } from "@/utils/isUserUsingCrosscall";
import { useSnackbarStore } from "@/stores/snackbar";
Expand Down Expand Up @@ -129,7 +130,7 @@ function sendFeedback(isCorrect: boolean) {
<h2 v-if="isDummy" class="fr-alert__title">
Arme factice de type {{ label }}
</h2>
<h2 v-else-if="store.selectedAlarmGun" class="fr-alert__title">
<h2 v-else-if="isAlarmGun()" class="fr-alert__title">
Arme d'alarme de type {{ label }}
</h2>
<h2 v-else class="fr-alert__title">
Expand Down
11 changes: 5 additions & 6 deletions frontend/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { clearLocalStorage } from "@/utils/storage-utils.js";
import { mgr } from "@/utils/authentication";

import MissingCardPage from "@/views/MissingCardPage.vue";
import IdentificationQualityImage from "@/views/GuideIdentificationFirearm/IdentificationQualityImage.vue";
import ExpertiseForm from "@/views/GuideAskingExpertise/ExpertiseForm.vue";

const HomePage = () => import("@/views/HomePage.vue");
Expand Down Expand Up @@ -41,8 +42,6 @@ const IdentificationFurtherInformations = () =>
);
const IdentificationSelectAmmo = () =>
import("@/views/GuideIdentificationFirearm/IdentificationSelectAmmo.vue");
const IdentificationBlankGun = () =>
import("@/views/GuideIdentificationFirearm/IdentificationBlankGun.vue");
const ExpertSituation = () =>
import("@/views/GuideContactExpert/ExpertSituation.vue");

Expand Down Expand Up @@ -129,9 +128,9 @@ const routes: RouteRecordRaw[] = [
component: IdentificationSelectAmmo,
},
{
path: "armes-alarme",
name: "IdentificationBlankGun",
component: IdentificationBlankGun,
path: "qualite-image",
name: "IdentificationQualityImage",
component: IdentificationQualityImage,
},
{
path: "resultat-final",
Expand Down Expand Up @@ -247,7 +246,7 @@ const routes: RouteRecordRaw[] = [
}
} catch (error) {
console.error("Erreur signin callback:", error);
next({ name: "AuthRedirect" });
next({ name: "ErrorPage" });
}
},
},
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/stores/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ export const useStore = defineStore("result", {
const gunBarrelLength = ref(null);
const img = ref(null);
const imgUrl = ref(null);
const unresizeImage = ref(null);
const securingTutorial = ref(false);

const selectedOptions = ref([]);
const selectedAmmo = ref(undefined);
const selectedAlarmGun = ref(undefined);
const alarmModel = ref(null);
const isAlarmGunMissingText = ref(null);
const isAlarmGunLowQuality = ref(null);
const isDummy = computed(() => !!(selectedAmmo.value === "billes"));
const isModalTransparentAmmoOpened = ref(null);

Expand All @@ -26,11 +30,15 @@ export const useStore = defineStore("result", {
gunBarrelLength.value = null;
img.value = null;
imgUrl.value = null;
unresizeImage.value = null;
securingTutorial.value = false;

selectedOptions.value = [];
selectedAmmo.value = undefined;
selectedAlarmGun.value = undefined;
alarmModel.value = null;
isAlarmGunMissingText.value = null;
isAlarmGunLowQuality.value = null;
isModalTransparentAmmoOpened.value = null;
}

Expand All @@ -42,10 +50,14 @@ export const useStore = defineStore("result", {
gunBarrelLength,
img,
imgUrl,
unresizeImage,
securingTutorial,
selectedOptions,
selectedAmmo,
selectedAlarmGun,
alarmModel,
isAlarmGunMissingText,
isAlarmGunLowQuality,
isDummy,
isModalTransparentAmmoOpened,
$reset,
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/utils/firearms-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const TYPOLOGIES = {
const IdentificationTypologyResult = "IdentificationTypologyResult";
const IdentificationFurtherInformations = "IdentificationFurtherInformations";
const IdentificationSelectAmmo = "IdentificationSelectAmmo";
const IdentificationBlankGun = "IdentificationBlankGun";
const IdentificationQualityImage = "IdentificationQualityImage";
const IdentificationFinalResult = "IdentificationFinalResult";

export const identificationGuideSteps = [
Expand All @@ -48,7 +48,7 @@ export const identificationGuideStepsWithArmeAlarme = [
IdentificationTypologyResult,
IdentificationFurtherInformations,
IdentificationSelectAmmo,
IdentificationBlankGun,
IdentificationQualityImage,
IdentificationFinalResult,
] as const;

Expand All @@ -69,7 +69,7 @@ export const identificationRoutePathsWithArmeAlarme = [
"resultat-typologie",
"informations-complementaires",
"munition-type",
"armes-alarme",
"qualite-image",
"resultat-final",
] as const;

Expand All @@ -81,7 +81,7 @@ export function isAlarmGun() {
) {
return false;
}
return store.selectedAlarmGun ? true : undefined;
return store.alarmModel === "Alarm_model" ? true : undefined;
}

export const MEASURED_GUNS_TYPOLOGIES = [
Expand Down
Loading

0 comments on commit 8d337ec

Please sign in to comment.