From 267aa2fb640163bca3b6299776c18e6ae5447137 Mon Sep 17 00:00:00 2001 From: Dhruv Ranajit Choudhury <111229637+dhrvrc@users.noreply.github.com> Date: Fri, 1 Mar 2024 16:15:03 +0000 Subject: [PATCH] Linked Images endpoint to the Model (#90) * 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 --------- Co-authored-by: Sam Co-authored-by: temi-oye <29523551+temi-oye@users.noreply.github.com> Co-authored-by: Peng Hui Liu <127897896+liuphui@users.noreply.github.com> * 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 Co-authored-by: temi-oye <29523551+temi-oye@users.noreply.github.com> Co-authored-by: Peng Hui Liu <127897896+liuphui@users.noreply.github.com> Co-authored-by: egaynor --- api/images/services/ImageProcessingService.py | 2 - api/images/services/LogoDetectionService.py | 2 - api/images/services/__init__.py | 2 + .../services/image_processing_service.py | 16 ++++++ api/images/services/logo_detection_service.py | 19 +++++++ api/images/views.py | 56 +++++++++++++++++-- 6 files changed, 87 insertions(+), 10 deletions(-) delete mode 100644 api/images/services/ImageProcessingService.py delete mode 100644 api/images/services/LogoDetectionService.py create mode 100644 api/images/services/image_processing_service.py create mode 100644 api/images/services/logo_detection_service.py diff --git a/api/images/services/ImageProcessingService.py b/api/images/services/ImageProcessingService.py deleted file mode 100644 index 6e70ce34..00000000 --- a/api/images/services/ImageProcessingService.py +++ /dev/null @@ -1,2 +0,0 @@ -class ImageProcessingService: - pass diff --git a/api/images/services/LogoDetectionService.py b/api/images/services/LogoDetectionService.py deleted file mode 100644 index 86b382a0..00000000 --- a/api/images/services/LogoDetectionService.py +++ /dev/null @@ -1,2 +0,0 @@ -class LogoDetectionService: - pass diff --git a/api/images/services/__init__.py b/api/images/services/__init__.py index e69de29b..2378deb8 100644 --- a/api/images/services/__init__.py +++ b/api/images/services/__init__.py @@ -0,0 +1,2 @@ +# from .ImageProcessingService import ImageProcessingService +# from .LogoDetectionService import LogoDetectionService diff --git a/api/images/services/image_processing_service.py b/api/images/services/image_processing_service.py new file mode 100644 index 00000000..8d296982 --- /dev/null +++ b/api/images/services/image_processing_service.py @@ -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 diff --git a/api/images/services/logo_detection_service.py b/api/images/services/logo_detection_service.py new file mode 100644 index 00000000..a7daf31f --- /dev/null +++ b/api/images/services/logo_detection_service.py @@ -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 diff --git a/api/images/views.py b/api/images/views.py index 7f43f732..98148f28 100644 --- a/api/images/views.py +++ b/api/images/views.py @@ -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, } ) + +"""