-
-
Notifications
You must be signed in to change notification settings - Fork 978
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1589 from pierotofy/objdec
AI Object Detection
- Loading branch information
Showing
23 changed files
with
579 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .plugin import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import os | ||
import json | ||
from rest_framework import status | ||
from rest_framework.response import Response | ||
from app.plugins.views import TaskView, GetTaskResult, TaskResultOutputError | ||
from app.plugins.worker import run_function_async | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
def detect(orthophoto, model, progress_callback=None): | ||
import os | ||
from webodm import settings | ||
|
||
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', progress_callback=progress_callback)} | ||
except Exception as e: | ||
return {'error': str(e)} | ||
|
||
class TaskObjDetect(TaskView): | ||
def post(self, request, pk=None): | ||
task = self.get_and_check_task(request, pk) | ||
|
||
if task.orthophoto_extent is None: | ||
return Response({'error': _('No orthophoto is available.')}) | ||
|
||
orthophoto = os.path.abspath(task.get_asset_download_path("orthophoto.tif")) | ||
model = request.data.get('model', 'cars') | ||
|
||
if not model in ['cars', 'trees']: | ||
return Response({'error': 'Invalid model'}, status=status.HTTP_200_OK) | ||
|
||
celery_task_id = run_function_async(detect, orthophoto, model, with_progress=True).task_id | ||
|
||
return Response({'celery_task_id': celery_task_id}, status=status.HTTP_200_OK) | ||
|
||
class TaskObjDownload(GetTaskResult): | ||
def handle_output(self, output, result, **kwargs): | ||
try: | ||
return json.loads(output) | ||
except: | ||
raise TaskResultOutputError("Invalid GeoJSON") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "Object Detect", | ||
"webodmMinVersion": "2.6.0", | ||
"description": "Detect objects using AI in orthophotos", | ||
"version": "1.0.0", | ||
"author": "Piero Toffanin", | ||
"email": "[email protected]", | ||
"repository": "https://github.com/OpenDroneMap/WebODM", | ||
"tags": ["object", "detect", "ai"], | ||
"homepage": "https://github.com/OpenDroneMap/WebODM", | ||
"experimental": false, | ||
"deprecated": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from app.plugins import PluginBase | ||
from app.plugins import MountPoint | ||
from .api import TaskObjDetect | ||
from .api import TaskObjDownload | ||
|
||
|
||
class Plugin(PluginBase): | ||
def include_js_files(self): | ||
return ['main.js'] | ||
|
||
def build_jsx_components(self): | ||
return ['ObjDetect.jsx'] | ||
|
||
def api_mount_points(self): | ||
return [ | ||
MountPoint('task/(?P<pk>[^/.]+)/detect', TaskObjDetect.as_view()), | ||
MountPoint('task/[^/.]+/download/(?P<celery_task_id>.+)', TaskObjDownload.as_view()), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import L from 'leaflet'; | ||
import ReactDOM from 'ReactDOM'; | ||
import React from 'React'; | ||
import PropTypes from 'prop-types'; | ||
import './ObjDetect.scss'; | ||
import ObjDetectPanel from './ObjDetectPanel'; | ||
|
||
class ObjDetectButton extends React.Component { | ||
static propTypes = { | ||
tasks: PropTypes.object.isRequired, | ||
map: PropTypes.object.isRequired | ||
} | ||
|
||
constructor(props){ | ||
super(props); | ||
|
||
this.state = { | ||
showPanel: false | ||
}; | ||
} | ||
|
||
handleOpen = () => { | ||
this.setState({showPanel: true}); | ||
} | ||
|
||
handleClose = () => { | ||
this.setState({showPanel: false}); | ||
} | ||
|
||
render(){ | ||
const { showPanel } = this.state; | ||
|
||
return (<div className={showPanel ? "open" : ""}> | ||
<a href="javascript:void(0);" | ||
onClick={this.handleOpen} | ||
className="leaflet-control-objdetect-button leaflet-bar-part theme-secondary"></a> | ||
<ObjDetectPanel map={this.props.map} isShowed={showPanel} tasks={this.props.tasks} onClose={this.handleClose} /> | ||
</div>); | ||
} | ||
} | ||
|
||
export default L.Control.extend({ | ||
options: { | ||
position: 'topright' | ||
}, | ||
|
||
onAdd: function (map) { | ||
var container = L.DomUtil.create('div', 'leaflet-control-objdetect leaflet-bar leaflet-control'); | ||
L.DomEvent.disableClickPropagation(container); | ||
ReactDOM.render(<ObjDetectButton map={this.options.map} tasks={this.options.tasks} />, container); | ||
|
||
return container; | ||
} | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
.leaflet-control-objdetect{ | ||
z-index: 999 !important; | ||
|
||
a.leaflet-control-objdetect-button{ | ||
background: url(icon.svg) no-repeat 0 0; | ||
background-size: 26px 26px; | ||
border-radius: 2px; | ||
} | ||
|
||
div.objdetect-panel{ display: none; } | ||
|
||
.open{ | ||
a.leaflet-control-objdetect-button{ | ||
display: none; | ||
} | ||
|
||
div.objdetect-panel{ | ||
display: block; | ||
} | ||
} | ||
} | ||
.leaflet-touch .leaflet-control-objdetect a { | ||
background-position: 2px 2px; | ||
} |
Oops, something went wrong.