2025-01-23 20:09:58 +00:00
|
|
|
import os
|
2025-01-24 16:43:19 +00:00
|
|
|
import json
|
2025-01-23 20:09:58 +00:00
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.response import Response
|
2025-01-24 16:43:19 +00:00
|
|
|
from app.plugins.views import TaskView, CheckTask, GetTaskResult, TaskResultOutputError
|
2025-01-23 20:09:58 +00:00
|
|
|
from app.plugins.worker import run_function_async
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
2025-01-24 16:43:19 +00:00
|
|
|
def detect(orthophoto, model):
|
2025-01-23 20:09:58 +00:00
|
|
|
import os
|
|
|
|
from webodm import settings
|
|
|
|
|
2025-01-24 16:43:19 +00:00
|
|
|
try:
|
|
|
|
from geodeep import detect as gdetect, models
|
|
|
|
models.cache_dir = os.path.join(settings.MEDIA_ROOT, "CACHE", "detection_models")
|
|
|
|
except ImportError:
|
|
|
|
return {'error': "GeoDeep library is missing"}
|
|
|
|
|
|
|
|
try:
|
|
|
|
return {'output': gdetect(orthophoto, model, output_type='geojson')}
|
|
|
|
except Exception as e:
|
|
|
|
return {'error': str(e)}
|
|
|
|
|
|
|
|
class TaskObjDetect(TaskView):
|
2025-01-23 20:09:58 +00:00
|
|
|
def post(self, request, pk=None):
|
|
|
|
task = self.get_and_check_task(request, pk)
|
|
|
|
|
2025-01-24 16:43:19 +00:00
|
|
|
if task.orthophoto_extent is None:
|
|
|
|
return Response({'error': _('No orthophoto is available.')})
|
2025-01-23 20:09:58 +00:00
|
|
|
|
2025-01-24 16:43:19 +00:00
|
|
|
orthophoto = os.path.abspath(task.get_asset_download_path("orthophoto.tif"))
|
|
|
|
model = request.data.get('model', 'cars')
|
2025-01-23 20:09:58 +00:00
|
|
|
|
2025-01-24 16:43:19 +00:00
|
|
|
if not model in ['cars', 'trees']:
|
|
|
|
return Response({'error': 'Invalid model'}, status=status.HTTP_200_OK)
|
2025-01-23 20:09:58 +00:00
|
|
|
|
2025-01-24 16:43:19 +00:00
|
|
|
celery_task_id = run_function_async(detect, orthophoto, model).task_id
|
|
|
|
return Response({'celery_task_id': celery_task_id}, status=status.HTTP_200_OK)
|
2025-01-23 20:09:58 +00:00
|
|
|
|
2025-01-24 16:43:19 +00:00
|
|
|
class TaskObjCheck(CheckTask):
|
2025-01-23 20:09:58 +00:00
|
|
|
pass
|
2025-01-24 16:43:19 +00:00
|
|
|
|
|
|
|
class TaskObjDownload(GetTaskResult):
|
|
|
|
def handle_output(self, output, result, **kwargs):
|
|
|
|
try:
|
|
|
|
return json.loads(output)
|
|
|
|
except:
|
|
|
|
raise TaskResultOutputError("Invalid GeoJSON")
|