Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ add api route and tests for alarm gun detection #617

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions backend/src/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from basegun_ml.classification import get_typology
from basegun_ml.exceptions import MissingCard, MissingGun
from basegun_ml.measure import get_lengths
from basegun_ml.ocr import is_alarm_weapon
from fastapi import (
APIRouter,
BackgroundTasks,
Expand Down Expand Up @@ -238,3 +239,16 @@ async def expert_contact(
""",
attachements=files,
)


@router.post("/identification-alarm-gun")
async def image_alarm_gun(
image: UploadFile = File(...),
):
try:
img_bytes = image.file.read()
# Process image with ML models
return {"is_alarm_model": is_alarm_weapon(img_bytes) == "Alarm_model"}

except Exception as e:
return {"is_alarm_model": False, "exception": e.__class__.__name__}
Binary file added backend/tests/images/alarm_gun.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/tests/images/low_quality.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/tests/images/no_text.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
44 changes: 40 additions & 4 deletions backend/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def test_version(self, client):
def test_upload(self, client):
"""Checks that the file upload works properly"""
create_bucket()
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "revolver.jpg")
path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "images/revolver.jpg"
)

with open(path, "rb") as f:
r = client.post(
Expand Down Expand Up @@ -99,7 +101,7 @@ def test_headers(self, client):

class TestUpload:
def test_revolver_without_card(self, client):
with open("./tests/revolver.jpg", "rb") as f:
with open("./tests/images/revolver.jpg", "rb") as f:
response = client.post(
"/api/upload",
files={"image": f},
Expand All @@ -115,7 +117,7 @@ def test_revolver_without_card(self, client):
assert response.data["conf_card"] is None

def test_semi_auto_without_card(self, client):
with open("./tests/epaule_a_levier_sous_garde.jpg", "rb") as f:
with open("./tests/images/epaule_a_levier_sous_garde.jpg", "rb") as f:
response = client.post(
"/api/upload",
files={"image": f},
Expand All @@ -134,7 +136,7 @@ def test_semi_auto_without_card(self, client):
class TestExpertContact:
def test_success(self, faker, client):
client.authenticate()
with open("./tests/revolver.jpg", "rb") as f:
with open("./tests/images/revolver.jpg", "rb") as f:
response = client.post(
"/api/expert-contact",
files=[
Expand Down Expand Up @@ -178,3 +180,37 @@ def test_403(self, client):
response = client.get("/api/contact-details")
response.data = response.json()
assert response.status_code == 403


class TestAlarmGunUpload:
def test_alarm_gun(self, client):
with open("./tests/images/alarm_gun.jpg", "rb") as f:
response = client.post(
"/api/identification-alarm-gun",
files={"image": f},
)
response.data = response.json()
assert response.status_code == 200
assert response.data["is_alarm_model"] is True

def test_bad_quality(self, client):
with open("./tests/images/low_quality.jpg", "rb") as f:
response = client.post(
"/api/identification-alarm-gun",
files={"image": f},
)
response.data = response.json()
assert response.status_code == 200
assert response.data["is_alarm_model"] is False
assert response.data["exception"] == "LowQuality"

def test_missing_text(self, client):
with open("./tests/images/no_text.jpg", "rb") as f:
response = client.post(
"/api/identification-alarm-gun",
files={"image": f},
)
response.data = response.json()
assert response.status_code == 200
assert response.data["is_alarm_model"] is False
assert response.data["exception"] == "MissingText"
Loading