hysts commited on
Commit
572921a
1 Parent(s): 98c1dea
.gitattributes CHANGED
@@ -1,3 +1,4 @@
 
1
  *.7z filter=lfs diff=lfs merge=lfs -text
2
  *.arrow filter=lfs diff=lfs merge=lfs -text
3
  *.bin filter=lfs diff=lfs merge=lfs -text
 
1
+ *jpg filter=lfs diff=lfs merge=lfs -text
2
  *.7z filter=lfs diff=lfs merge=lfs -text
3
  *.arrow filter=lfs diff=lfs merge=lfs -text
4
  *.bin filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio_cached_examples/
2
+ yolo*.pt
3
+
4
+ # Byte-compiled / optimized / DLL files
5
+ __pycache__/
6
+ *.py[cod]
7
+ *$py.class
8
+
9
+ # C extensions
10
+ *.so
11
+
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/
.pre-commit-config.yaml ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.2.0
4
+ hooks:
5
+ - id: check-executables-have-shebangs
6
+ - id: check-json
7
+ - id: check-merge-conflict
8
+ - id: check-shebang-scripts-are-executable
9
+ - id: check-toml
10
+ - id: check-yaml
11
+ - id: double-quote-string-fixer
12
+ - id: end-of-file-fixer
13
+ - id: mixed-line-ending
14
+ args: ['--fix=lf']
15
+ - id: requirements-txt-fixer
16
+ - id: trailing-whitespace
17
+ - repo: https://github.com/myint/docformatter
18
+ rev: v1.4
19
+ hooks:
20
+ - id: docformatter
21
+ args: ['--in-place']
22
+ - repo: https://github.com/pycqa/isort
23
+ rev: 5.12.0
24
+ hooks:
25
+ - id: isort
26
+ - repo: https://github.com/pre-commit/mirrors-mypy
27
+ rev: v0.991
28
+ hooks:
29
+ - id: mypy
30
+ args: ['--ignore-missing-imports']
31
+ - repo: https://github.com/google/yapf
32
+ rev: v0.32.0
33
+ hooks:
34
+ - id: yapf
35
+ args: ['--parallel', '--in-place']
.style.yapf ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ [style]
2
+ based_on_style = pep8
3
+ blank_line_before_nested_class_or_def = false
4
+ spaces_before_comment = 2
5
+ split_before_logical_operator = true
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 💻
4
  colorFrom: indigo
5
  colorTo: purple
6
  sdk: gradio
7
- sdk_version: 3.17.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
4
  colorFrom: indigo
5
  colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 3.18.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ import pathlib
4
+
5
+ import gradio as gr
6
+ import numpy as np
7
+ import PIL.Image
8
+ import torch
9
+ from sahi.prediction import ObjectPrediction
10
+ from sahi.utils.cv import visualize_object_predictions
11
+ from transformers import AutoImageProcessor, DetaForObjectDetection
12
+ from ultralytics import YOLO
13
+
14
+ DESCRIPTION = '# Compare DETA and YOLOv8'
15
+
16
+ device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
17
+
18
+ MODEL_ID = 'jozhang97/deta-swin-large'
19
+ image_processor = AutoImageProcessor.from_pretrained(MODEL_ID)
20
+ model_deta = DetaForObjectDetection.from_pretrained(MODEL_ID)
21
+ model_deta.to(device)
22
+
23
+ model_yolo = YOLO('yolov8x.pt')
24
+
25
+
26
+ @torch.inference_mode()
27
+ def run_deta(image_path: str, threshold: float) -> np.ndarray:
28
+ image = PIL.Image.open(image_path)
29
+ inputs = image_processor(images=image, return_tensors='pt').to(device)
30
+ outputs = model_deta(**inputs)
31
+ target_sizes = torch.tensor([image.size[::-1]])
32
+ results = image_processor.post_process_object_detection(
33
+ outputs, threshold=threshold, target_sizes=target_sizes)[0]
34
+
35
+ boxes = results['boxes'].cpu().numpy()
36
+ scores = results['scores'].cpu().numpy()
37
+ cat_ids = results['labels'].cpu().numpy().tolist()
38
+ preds = []
39
+ for box, score, cat_id in zip(boxes, scores, cat_ids):
40
+ box = np.round(box).astype(int)
41
+ cat_label = model_deta.config.id2label[cat_id]
42
+ pred = ObjectPrediction(bbox=box,
43
+ category_id=cat_id,
44
+ category_name=cat_label,
45
+ score=score)
46
+ preds.append(pred)
47
+ res = visualize_object_predictions(np.asarray(image), preds)['image']
48
+ return res
49
+
50
+
51
+ def run_yolov8(image_path: str, threshold: float) -> np.ndarray:
52
+ image = PIL.Image.open(image_path)
53
+ results = model_yolo(image, imgsz=640, conf=threshold)
54
+ boxes = results[0].boxes.cpu().numpy().data
55
+ preds = []
56
+ for box in boxes:
57
+ score = box[4]
58
+ cat_id = int(box[5])
59
+ box = np.round(box[:4]).astype(int)
60
+ cat_label = model_yolo.model.names[cat_id]
61
+ pred = ObjectPrediction(bbox=box,
62
+ category_id=cat_id,
63
+ category_name=cat_label,
64
+ score=score)
65
+ preds.append(pred)
66
+ res = visualize_object_predictions(np.asarray(image), preds)['image']
67
+ return res
68
+
69
+
70
+ def run(image_path: str, threshold_deta: float,
71
+ threshold_yolo: float) -> tuple[np.ndarray, np.ndarray]:
72
+ return run_deta(image_path,
73
+ threshold_deta), run_yolov8(image_path, threshold_yolo)
74
+
75
+
76
+ with gr.Blocks(css='style.css') as demo:
77
+ gr.Markdown(DESCRIPTION)
78
+ with gr.Row():
79
+ with gr.Column():
80
+ image = gr.Image(label='Input image', type='filepath')
81
+ threshold_deta = gr.Slider(label='Score threshold for DETA',
82
+ minimum=0,
83
+ maximum=1,
84
+ value=0.1,
85
+ step=0.01)
86
+ threshold_yolo = gr.Slider(label='Score threshold for YOLOv8',
87
+ minimum=0,
88
+ maximum=1,
89
+ value=0.5,
90
+ step=0.01)
91
+ run_button = gr.Button('Run')
92
+ with gr.Column():
93
+ result_deta = gr.Image(label='Result (DETA)', type='numpy')
94
+ result_yolo = gr.Image(label='Result (YOLOv8)', type='numpy')
95
+
96
+ with gr.Row():
97
+ paths = sorted(pathlib.Path('images').glob('*.jpg'))
98
+ gr.Examples(examples=[[path.as_posix(), 0.1, 0.5] for path in paths],
99
+ inputs=[
100
+ image,
101
+ threshold_deta,
102
+ threshold_yolo,
103
+ ],
104
+ outputs=[
105
+ result_deta,
106
+ result_yolo,
107
+ ],
108
+ fn=run,
109
+ cache_examples=True)
110
+
111
+ run_button.click(fn=run,
112
+ inputs=[
113
+ image,
114
+ threshold_deta,
115
+ threshold_yolo,
116
+ ],
117
+ outputs=[
118
+ result_deta,
119
+ result_yolo,
120
+ ])
121
+
122
+ demo.queue().launch()
images/pexels-jean-van-der-meulen-15997919k9oucxp.jpg ADDED

Git LFS Details

  • SHA256: 0f34538e583afda98666158a40154b09316757d89b465b318e182a1758859a88
  • Pointer size: 131 Bytes
  • Size of remote file: 333 kB
images/pexels-oleksandr-pidvalnyi-1031698hnbmeibr.jpg ADDED

Git LFS Details

  • SHA256: 2de03892c6f6624a45fc67a283aeb33d4534fecc76e51d83e43fe40b58ce2351
  • Pointer size: 131 Bytes
  • Size of remote file: 670 kB
images/pexels-pixabay-45170x8uq00t8.jpg ADDED

Git LFS Details

  • SHA256: 114e83cc896e9df6476fd682c8d0dc8fafb76e8cf1da5f6396280040081ee1ca
  • Pointer size: 131 Bytes
  • Size of remote file: 681 kB
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ git+https://github.com/huggingface/transformers@59d5ede
2
+ numpy==1.24.1
3
+ Pillow==9.4.0
4
+ sahi==0.11.11
5
+ torch==1.13.1
6
+ torchvision==0.14.1
7
+ ultralytics==8.0.28
style.css ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ }