Skip to content

Commit

Permalink
Linked Images endpoint to the Model (#90)
Browse files Browse the repository at this point in the history
* Updating api branch to include model (#85)

* add nginx configuration and deployment script

* update deployment script and nginx configuration as well as adding deplyoment information to README

* add more urls and prevent overwriting images issue

* enable scraping for base64 encoded images

* move company urls to separate file + small refector

* refactor code

* collect adidas images

* add correct file extension to images

* restore original images

* move image bytes logic

* save image with file extension

* remove unuseful images

* rename some images

* comment out urls used

* Feat: Update docker building for model requirements (#69)

update docker building with new requirements and rename models directory to avoid conflicts

* add flake8 configuration to ignore unused imports from __init__.py

* add make_temp_directory helper for temporarily saving files

* Added 'images' app (#70)

* Chore: Bump Django and OpenCV versions (#75)

* bump django version to avoid vulnerabilities

* bump open cv version to avoid vulnerabilities

* collect under armour images (#72)

* Bugfix: Update deployment script for larger docker images (#78)

* force remove dangling docker resources and remove call to pull images before building

* add index url to install CPU version of torch by prebuilt wheel

* combine environment instructions and add all unnecessary files to docker ignore

* Update README with CI/CD status badges and database migration instructions (#79)

add ci status badges and update installation instructions to migrate

* Added model for detection and processing (#80)

* Added model for detection and processing

* Removed and added changes

* Commented out and removed code to pass make lint

* Uncomment __init__.py

---------

Co-authored-by: Sam <[email protected]>

---------

Co-authored-by: Sam <[email protected]>
Co-authored-by: temi-oye <[email protected]>
Co-authored-by: Peng Hui Liu <[email protected]>

* Initial attempt to link model to endpoint

* Trying to fix failures and changing variable and method names to match rest of the repository

* Linked endpoint to the model (Hopefully)

* Fixed the no 400 error code issue

* Fix spelling error

* fixed issue of writing image to temporary directory

* NoChangesAddedComment

* Removed redundant comments

* Removed redundant comments

* Formatting changes to adhere to snake_case convention and removing redundant printing

* More snake_case changes

* Naming convention changes

---------

Co-authored-by: Sam <[email protected]>
Co-authored-by: temi-oye <[email protected]>
Co-authored-by: Peng Hui Liu <[email protected]>
Co-authored-by: egaynor <[email protected]>
  • Loading branch information
5 people authored Mar 1, 2024
1 parent 453d246 commit 267aa2f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 10 deletions.
2 changes: 0 additions & 2 deletions api/images/services/ImageProcessingService.py

This file was deleted.

2 changes: 0 additions & 2 deletions api/images/services/LogoDetectionService.py

This file was deleted.

2 changes: 2 additions & 0 deletions api/images/services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# from .ImageProcessingService import ImageProcessingService
# from .LogoDetectionService import LogoDetectionService
16 changes: 16 additions & 0 deletions api/images/services/image_processing_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from api.utils.make_temp_directory import make_temp_directory

from .logo_detection_service import LogoDetectionService


class ImageProcessingService:
def process_images(self, images):
detections = {}

with make_temp_directory() as temp_directory:
for image in images:
file_path = temp_directory + "/" + image.name
with open(file_path, "wb") as file:
file.write(image.file.read())
detections.update({image.name: LogoDetectionService().detect_in_image(file_path)})
return detections
19 changes: 19 additions & 0 deletions api/images/services/logo_detection_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from ml.models.logo_detection.process import process as detect_logos

# from rest_framework import serializers


class LogoDetectionService:
def detect_in_image(self, image_path):
inference_results = []
inferences = detect_logos(image_path) # List LogoDetectionInference objects (ml.models.logo_detection.data)
for inference in inferences:
bbox = {
"x": inference.bounding_box.x,
"y": inference.bounding_box.y,
"w": inference.bounding_box.w,
"h": inference.bounding_box.h,
}
detection = {"bbox": bbox, "confidence": inference.confidence}
inference_results.append(detection)
return inference_results
56 changes: 50 additions & 6 deletions api/images/views.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,60 @@
from rest_framework import generics
from rest_framework import generics, status
from rest_framework.response import Response

from .services.image_processing_service import ImageProcessingService


# Create your views here.
class PredictionsViewSet(generics.GenericAPIView):
def post(self, request):
images = request.FILES
if not images:
return Response(status=status.HTTP_400_BAD_REQUEST)
images = images.values()
return Response(data=ImageProcessingService().process_images(images))


"""
EVA PREVIOUS CODE - does not work lol
import os
import tempfile
from django.http import FileResponse
from rest_framework import generics, status
from rest_framework.response import Response
from .services.ImageProcessingService import ImageProcessingService
# Create your views here.
class PredictionsViewSet(generics.GenericAPIView):
def post(self, request):
print(request.data)
images = request.FILES.values()
if not images:
return Response(status=status.HTTP_400_BAD_REQUEST)
temp_dir = tempfile.mkdtemp()
responses = []
output = []
for image in images:
filename = image.name
filepath = os.path.join(temp_dir, filename)
with open(filepath, "wb") as f:
for c in image.chunks():
f.write(c)
output_path = ImageProcessingService(filepath)
with open(output_path, "rb") as f:
output_image = f.read()
responses.append(output_path)
output.append(output_image)
print(responses)
return Response(
data={
"x1": 30,
"x2": 78,
"y1": 79,
"y2": 89,
"Confidence": 97.96,
"images": responses,
}
)
"""

0 comments on commit 267aa2f

Please sign in to comment.