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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Byte-compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
*venv*
|
| 6 |
+
.env
|
| 7 |
+
# C extensions
|
| 8 |
+
*.so
|
| 9 |
+
env
|
| 10 |
+
env/
|
| 11 |
+
model/
|
| 12 |
+
# Distribution / packaging
|
| 13 |
+
.Python
|
| 14 |
+
build/
|
| 15 |
+
develop-eggs/
|
| 16 |
+
dist/
|
| 17 |
+
downloads/
|
| 18 |
+
eggs/
|
| 19 |
+
.eggs/
|
| 20 |
+
lib/
|
| 21 |
+
lib64/
|
| 22 |
+
parts/
|
| 23 |
+
sdist/
|
| 24 |
+
var/
|
| 25 |
+
wheels/
|
| 26 |
+
share/python-wheels/
|
| 27 |
+
*.egg-info/
|
| 28 |
+
.installed.cfg
|
| 29 |
+
*.egg
|
| 30 |
+
MANIFEST
|
| 31 |
+
|
| 32 |
+
# PyInstaller
|
| 33 |
+
# Usually these files are written by a python script from a template
|
| 34 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 35 |
+
*.manifest
|
| 36 |
+
*.spec
|
| 37 |
+
|
| 38 |
+
# Installer logs
|
| 39 |
+
pip-log.txt
|
| 40 |
+
pip-delete-this-directory.txt
|
| 41 |
+
|
| 42 |
+
# Unit test / coverage reports
|
| 43 |
+
htmlcov/
|
| 44 |
+
.tox/
|
| 45 |
+
.nox/
|
| 46 |
+
.coverage
|
| 47 |
+
.coverage.*
|
| 48 |
+
.cache
|
| 49 |
+
nosetests.xml
|
| 50 |
+
coverage.xml
|
| 51 |
+
*.cover
|
| 52 |
+
*.py,cover
|
| 53 |
+
.hypothesis/
|
| 54 |
+
.pytest_cache/
|
| 55 |
+
cover/
|
| 56 |
+
|
| 57 |
+
# Translations
|
| 58 |
+
*.mo
|
| 59 |
+
*.pot
|
| 60 |
+
|
| 61 |
+
# Django stuff:
|
| 62 |
+
*.log
|
| 63 |
+
local_settings.py
|
| 64 |
+
db.sqlite3
|
| 65 |
+
db.sqlite3-journal
|
| 66 |
+
|
| 67 |
+
# Flask stuff:
|
| 68 |
+
instance/
|
| 69 |
+
.webassets-cache
|
| 70 |
+
|
| 71 |
+
# Scrapy stuff:
|
| 72 |
+
.scrapy
|
| 73 |
+
|
| 74 |
+
# Sphinx documentation
|
| 75 |
+
docs/_build/
|
| 76 |
+
|
| 77 |
+
# PyBuilder
|
| 78 |
+
.pybuilder/
|
| 79 |
+
target/
|
| 80 |
+
|
| 81 |
+
# Jupyter Notebook
|
| 82 |
+
.ipynb_checkpoints
|
| 83 |
+
|
| 84 |
+
# IPython
|
| 85 |
+
profile_default/
|
| 86 |
+
ipython_config.py
|
| 87 |
+
|
| 88 |
+
# pyenv
|
| 89 |
+
# For a library or package, you might want to ignore these files since the code is
|
| 90 |
+
# intended to run in multiple environments; otherwise, check them in:
|
| 91 |
+
# .python-version
|
| 92 |
+
|
| 93 |
+
# pipenv
|
| 94 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 95 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 96 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 97 |
+
# install all needed dependencies.
|
| 98 |
+
#Pipfile.lock
|
| 99 |
+
|
| 100 |
+
# poetry
|
| 101 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
| 102 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 103 |
+
# commonly ignored for libraries.
|
| 104 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
| 105 |
+
#poetry.lock
|
| 106 |
+
|
| 107 |
+
# pdm
|
| 108 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
| 109 |
+
#pdm.lock
|
| 110 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
| 111 |
+
# in version control.
|
| 112 |
+
# https://pdm.fming.dev/#use-with-ide
|
| 113 |
+
.pdm.toml
|
| 114 |
+
|
| 115 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
| 116 |
+
__pypackages__/
|
| 117 |
+
|
| 118 |
+
# Celery stuff
|
| 119 |
+
celerybeat-schedule
|
| 120 |
+
celerybeat.pid
|
| 121 |
+
|
| 122 |
+
# SageMath parsed files
|
| 123 |
+
*.sage.py
|
| 124 |
+
|
| 125 |
+
# Environments
|
| 126 |
+
.env
|
| 127 |
+
.venv
|
| 128 |
+
env/
|
| 129 |
+
venv/
|
| 130 |
+
ENV/
|
| 131 |
+
env.bak/
|
| 132 |
+
venv.bak/
|
| 133 |
+
|
| 134 |
+
# Spyder project settings
|
| 135 |
+
.spyderproject
|
| 136 |
+
.spyproject
|
| 137 |
+
|
| 138 |
+
# Rope project settings
|
| 139 |
+
.ropeproject
|
| 140 |
+
|
| 141 |
+
# mkdocs documentation
|
| 142 |
+
/site
|
| 143 |
+
|
| 144 |
+
# mypy
|
| 145 |
+
.mypy_cache/
|
| 146 |
+
.dmypy.json
|
| 147 |
+
dmypy.json
|
| 148 |
+
|
| 149 |
+
# Pyre type checker
|
| 150 |
+
.pyre/
|
| 151 |
+
|
| 152 |
+
# pytype static type analyzer
|
| 153 |
+
.pytype/
|
| 154 |
+
|
| 155 |
+
# Cython debug symbols
|
| 156 |
+
cython_debug/
|
| 157 |
+
|
| 158 |
+
# PyCharm
|
| 159 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
| 160 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
| 161 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 162 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 163 |
+
#.idea/
|
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
from inference import inference_frame
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def analize_video(x):
|
| 14 |
+
cap = cv2.VideoCapture(x)
|
| 15 |
+
path = '/tmp/test/'
|
| 16 |
+
os.makedirs(path, exist_ok=True)
|
| 17 |
+
videos = len(os.listdir(path))
|
| 18 |
+
path = f'{path}{videos}'
|
| 19 |
+
os.makedirs(path, exist_ok=True)
|
| 20 |
+
outname = f'{path}_processed.mp4'
|
| 21 |
+
#out = cv2.VideoWriter(outname,cv2.VideoWriter_fourcc(*'h264'), 20.0, (640,480))
|
| 22 |
+
counter = 0
|
| 23 |
+
while(cap.isOpened()):
|
| 24 |
+
ret, frame = cap.read()
|
| 25 |
+
if ret==True:
|
| 26 |
+
name = os.path.join(path,f'{counter:05d}.png')
|
| 27 |
+
frame = inference_frame(frame)
|
| 28 |
+
# write the flipped frame
|
| 29 |
+
cv2.imwrite(name, frame)
|
| 30 |
+
counter +=1
|
| 31 |
+
else:
|
| 32 |
+
break
|
| 33 |
+
# Release everything if job is finished
|
| 34 |
+
print(path)
|
| 35 |
+
os.system(f'''ffmpeg -framerate 20 -pattern_type glob -i '{path}/*.png' -c:v libx264 -pix_fmt yuv420p {outname}''')
|
| 36 |
+
return outname
|
| 37 |
+
|
| 38 |
+
with gr.Blocks(title='Shark Patrol',theme=gr.themes.Soft(),live=True,) as demo:
|
| 39 |
+
gr.Markdown("Initial DEMO.")
|
| 40 |
+
with gr.Tab("Shark Detector"):
|
| 41 |
+
with gr.Row():
|
| 42 |
+
video_input = gr.Video(source='upload',include_audio=False)
|
| 43 |
+
#video_input.style(witdh='50%',height='50%')
|
| 44 |
+
video_output = gr.Video()
|
| 45 |
+
#video_output.style(witdh='50%',height='50%')
|
| 46 |
+
|
| 47 |
+
video_button = gr.Button("Analyze")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
with gr.Accordion("Open for More!"):
|
| 51 |
+
gr.Markdown("Place holder for detection")
|
| 52 |
+
|
| 53 |
+
video_button.click(analize_video, inputs=video_input, outputs=video_output)
|
| 54 |
+
|
| 55 |
+
demo.queue()
|
| 56 |
+
demo.launch(share=True,width='40%',auth=(os.environ.get('SHARK_USERNAME'), os.environ.get('SHARK_PASSWORD')))
|
inference.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Check Pytorch installation
|
| 2 |
+
import torch, torchvision
|
| 3 |
+
print("torch version:",torch.__version__, "cuda:",torch.cuda.is_available())
|
| 4 |
+
|
| 5 |
+
# Check MMDetection installation
|
| 6 |
+
import mmdet
|
| 7 |
+
import os
|
| 8 |
+
import mmcv
|
| 9 |
+
import mmengine
|
| 10 |
+
from mmdet.apis import init_detector, inference_detector
|
| 11 |
+
from mmdet.utils import register_all_modules
|
| 12 |
+
from mmdet.registry import VISUALIZERS
|
| 13 |
+
|
| 14 |
+
from huggingface_hub import hf_hub_download
|
| 15 |
+
from huggingface_hub import snapshot_download
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
classes= ['Beach',
|
| 19 |
+
'Sea',
|
| 20 |
+
'Wave',
|
| 21 |
+
'Rock',
|
| 22 |
+
'Breaking wave',
|
| 23 |
+
'Reflection of the sea',
|
| 24 |
+
'Foam',
|
| 25 |
+
'Algae',
|
| 26 |
+
'Vegetation',
|
| 27 |
+
'Watermark',
|
| 28 |
+
'Bird',
|
| 29 |
+
'Ship',
|
| 30 |
+
'Boat',
|
| 31 |
+
'Car',
|
| 32 |
+
'Kayak',
|
| 33 |
+
"Shark's line",
|
| 34 |
+
'Dock',
|
| 35 |
+
'Dog',
|
| 36 |
+
'Unidentifiable shade',
|
| 37 |
+
'Bird shadow',
|
| 38 |
+
'Boat shadow',
|
| 39 |
+
'Kayal shade',
|
| 40 |
+
'Surfer shadow',
|
| 41 |
+
'Shark shadow',
|
| 42 |
+
'Surfboard shadow',
|
| 43 |
+
'Crocodile',
|
| 44 |
+
'Sea cow',
|
| 45 |
+
'Stingray',
|
| 46 |
+
'Person',
|
| 47 |
+
'ocean',
|
| 48 |
+
'Surfer',
|
| 49 |
+
'Surfer',
|
| 50 |
+
'Fish',
|
| 51 |
+
'Killer whale',
|
| 52 |
+
'Whale',
|
| 53 |
+
'Dolphin',
|
| 54 |
+
'Miscellaneous',
|
| 55 |
+
'Unidentifiable shark',
|
| 56 |
+
'Carpet shark',
|
| 57 |
+
'Dusty shark',
|
| 58 |
+
'Blue shark',
|
| 59 |
+
'Great white shark',
|
| 60 |
+
'Copper shark',
|
| 61 |
+
'Nurse shark',
|
| 62 |
+
'Silky shark',
|
| 63 |
+
'Leopard shark',
|
| 64 |
+
'Shortfin mako shark',
|
| 65 |
+
'Hammerhead shark',
|
| 66 |
+
'Oceanic whitetip shark',
|
| 67 |
+
'Blacktip shark',
|
| 68 |
+
'Tiger shark',
|
| 69 |
+
'Bull shark']*3
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
REPO_ID = "piperod91/australiapatrol"
|
| 77 |
+
FILENAME = "mask2former"
|
| 78 |
+
|
| 79 |
+
snapshot_download(repo_id=REPO_ID, token= os.environ.get('SHARK_MODEL'),local_dir='model/')
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# Choose to use a config and initialize the detector
|
| 85 |
+
config_file ='model/mask2former_swin-t-p4-w7-224_8xb2-lsj-50e_coco-panoptic/mask2former_swin-t-p4-w7-224_8xb2-lsj-50e_coco-panoptic.py'
|
| 86 |
+
#'/content/mmdetection/configs/panoptic_fpn/panoptic-fpn_r50_fpn_ms-3x_coco.py'
|
| 87 |
+
# Setup a checkpoint file to load
|
| 88 |
+
checkpoint_file ='model/mask2former_swin-t-p4-w7-224_8xb2-lsj-50e_coco-panoptic/checkpoint.pth'
|
| 89 |
+
# '/content/drive/MyDrive/Algorithms/weights/shark_panoptic_weights_16_4_23/panoptic-fpn_r50_fpn_ms-3x_coco/epoch_36.pth'
|
| 90 |
+
|
| 91 |
+
# register all modules in mmdet into the registries
|
| 92 |
+
register_all_modules()
|
| 93 |
+
|
| 94 |
+
# build the model from a config file and a checkpoint file
|
| 95 |
+
model = init_detector(config_file, checkpoint_file, device='cuda:0') # or device='cuda:0'
|
| 96 |
+
model.dataset_meta['classes'] = classes
|
| 97 |
+
# init visualizer(run the block only once in jupyter notebook)
|
| 98 |
+
visualizer = VISUALIZERS.build(model.cfg.visualizer)
|
| 99 |
+
# the dataset_meta is loaded from the checkpoint and
|
| 100 |
+
# then pass to the model in init_detector
|
| 101 |
+
visualizer.dataset_meta = model.dataset_meta
|
| 102 |
+
def inference_frame(image):
|
| 103 |
+
result = inference_detector(model, image)
|
| 104 |
+
# show the results
|
| 105 |
+
visualizer.add_datasample(
|
| 106 |
+
'result',
|
| 107 |
+
image,
|
| 108 |
+
data_sample=result,
|
| 109 |
+
draw_gt = None,
|
| 110 |
+
show=False
|
| 111 |
+
)
|
| 112 |
+
frame = visualizer.get_image()
|
| 113 |
+
return frame
|