Spaces:
Running
Running
ishworrsubedii
commited on
feat: add complete pipeline and Streamlit code This commit introduces a complete pipeline for both single and real-time inferences using cameras. It includes the implementation of Streamlit code to facilitate the process.
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitignore +163 -0
- Dockerfiles +0 -0
- README.md +126 -13
- app.py +56 -0
- config/config.ini +7 -0
- demos/__init__.py +4 -0
- demos/__pycache__/__init__.cpython-39.pyc +0 -0
- demos/__pycache__/single_image_inference.cpython-39.pyc +0 -0
- demos/alert_service_example.py +9 -0
- demos/cam_service_example/__pycache__/start_frame_capturing.cpython-39.pyc +0 -0
- demos/cam_service_example/__pycache__/stop_frame_capturing.cpython-39.pyc +0 -0
- demos/cam_service_example/start_frame_capturing.py +86 -0
- demos/cam_service_example/stop_frame_capturing.py +21 -0
- demos/image_load_service_example/__pycache__/start_image_load_example.cpython-39.pyc +0 -0
- demos/image_load_service_example/__pycache__/stop_image_load_example.cpython-39.pyc +0 -0
- demos/image_load_service_example/start_image_load_example.py +84 -0
- demos/image_load_service_example/stop_image_load_example.py +28 -0
- demos/single_image_inference.py +38 -0
- images/cam_images/PY-4856_Crosman-Bushmaster-MPW-Full_1558033480-458822316.jpg +0 -0
- images/cam_images/th-2208139001.jpg +0 -0
- images/cam_images/th-3711382641.jpg +0 -0
- logs/gun_det.log +0 -0
- logs/ipcam.log +1 -0
- main.py +10 -0
- resources/alert/alert.mp3 +0 -0
- resources/flag_load_image +1 -0
- resources/image_capturing +1 -0
- resources/models/best.pt +3 -0
- resources/models/v1/best.pt +3 -0
- resources/models/weapon_detector.pt +3 -0
- resources/models/yolov7.pt +3 -0
- resources/models/yolov7_2.pt +3 -0
- services/__init__.py +64 -0
- services/__pycache__/__init__.cpython-310.pyc +0 -0
- services/__pycache__/__init__.cpython-39.pyc +0 -0
- services/alert_service/__init__.py +4 -0
- services/alert_service/__pycache__/__init__.cpython-39.pyc +0 -0
- services/alert_service/__pycache__/alert_service.cpython-39.pyc +0 -0
- services/alert_service/alert_service.py +30 -0
- services/api/__init__.py +4 -0
- services/api/__pycache__/__init__.cpython-39.pyc +0 -0
- services/api/__pycache__/fast_api.cpython-39.pyc +0 -0
- services/api/fast_api.py +109 -0
- services/entity/__init__.py +0 -0
- services/entity/entity_config.py +7 -0
- services/image_capture_service/__init__.py +0 -0
- services/image_capture_service/__pycache__/__init__.cpython-39.pyc +0 -0
- services/image_capture_service/__pycache__/capture_main.cpython-39.pyc +0 -0
- services/image_capture_service/__pycache__/image_load_main.cpython-39.pyc +0 -0
- services/image_capture_service/capture_main.py +81 -0
.gitignore
ADDED
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# poetry
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
102 |
+
#poetry.lock
|
103 |
+
|
104 |
+
# pdm
|
105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
106 |
+
#pdm.lock
|
107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
108 |
+
# in version control.
|
109 |
+
# https://pdm.fming.dev/#use-with-ide
|
110 |
+
.pdm.toml
|
111 |
+
|
112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
113 |
+
__pypackages__/
|
114 |
+
|
115 |
+
# Celery stuff
|
116 |
+
celerybeat-schedule
|
117 |
+
celerybeat.pid
|
118 |
+
|
119 |
+
# SageMath parsed files
|
120 |
+
*.sage.py
|
121 |
+
|
122 |
+
# Environments
|
123 |
+
.env
|
124 |
+
.venv
|
125 |
+
env/
|
126 |
+
venv/
|
127 |
+
ENV/
|
128 |
+
env.bak/
|
129 |
+
venv.bak/
|
130 |
+
|
131 |
+
# Spyder project settings
|
132 |
+
.spyderproject
|
133 |
+
.spyproject
|
134 |
+
|
135 |
+
# Rope project settings
|
136 |
+
.ropeproject
|
137 |
+
|
138 |
+
# mkdocs documentation
|
139 |
+
/site
|
140 |
+
|
141 |
+
# mypy
|
142 |
+
.mypy_cache/
|
143 |
+
.dmypy.json
|
144 |
+
dmypy.json
|
145 |
+
|
146 |
+
# Pyre type checker
|
147 |
+
.pyre/
|
148 |
+
|
149 |
+
# pytype static type analyzer
|
150 |
+
.pytype/
|
151 |
+
|
152 |
+
# Cython debug symbols
|
153 |
+
cython_debug/
|
154 |
+
|
155 |
+
# PyCharm
|
156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
+
.idea/
|
161 |
+
resources
|
162 |
+
images
|
163 |
+
|
Dockerfiles
ADDED
File without changes
|
README.md
CHANGED
@@ -1,13 +1,126 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# weapon-detection-alert-location-share
|
2 |
+
|
3 |
+
This project focuses on enhancing the security of different places, like public areas and banks, by implementing a
|
4 |
+
robust gun detection and location-sharing system. The system is designed to capture images from an IP camera
|
5 |
+
strategically placed within the bank premises. When a captured image contains an identifiable gun, the system takes
|
6 |
+
appropriate actions, such as sending alerts or sharing the location of the detected firearm.
|
7 |
+
|
8 |
+
1. **Image Capture:**
|
9 |
+
|
10 |
+
Utilizes an IP camera to continuously capture images in real-time within the camera range.Images are processed and
|
11 |
+
analyzed for potential gun presence.
|
12 |
+
|
13 |
+
2. **Gun Detection:**
|
14 |
+
|
15 |
+
Employs advanced computer vision algorithms for accurate gun detection in captured images.
|
16 |
+
The system recognizes various types of firearms and distinguishes them from other objects.
|
17 |
+
3. Alert Systems:
|
18 |
+
|
19 |
+
When a gun is detected, the system triggers immediate alerts.
|
20 |
+
Alerts can be configured to notify security personnel, law enforcement, and relevant authorities.
|
21 |
+
4. Location Sharing:
|
22 |
+
|
23 |
+
If a gun is detected, the system captures the location of the incident.
|
24 |
+
The location information is shared in real-time with designated security personnel and law enforcement agencies.
|
25 |
+
|
26 |
+
## File Structure
|
27 |
+
|
28 |
+
File structure of the project
|
29 |
+
|
30 |
+
```commandline
|
31 |
+
|
32 |
+
├── config
|
33 |
+
│ ├── config.ini
|
34 |
+
│ └── config.yaml
|
35 |
+
├── examples
|
36 |
+
│ ├── start_cap.py
|
37 |
+
│ └── stop_cap.py
|
38 |
+
├── images
|
39 |
+
│ ├── cam_images
|
40 |
+
│ └── detected_image
|
41 |
+
│ ├── img_1.png
|
42 |
+
│ └── img.png
|
43 |
+
├── logs
|
44 |
+
│ └── gun_det.log
|
45 |
+
├── resources
|
46 |
+
│ └── models
|
47 |
+
│ └── yolov7.pt
|
48 |
+
├── services
|
49 |
+
│ ├── entity
|
50 |
+
│ │ └── __init__.py
|
51 |
+
│ ├── gun_det_service
|
52 |
+
│ │ ├── detection_example_cam.py
|
53 |
+
│ │ └── __init__.py
|
54 |
+
│ ├── image_capture
|
55 |
+
│ │ ├── capture_main.py
|
56 |
+
│ │ ├── __init__.py
|
57 |
+
│ │ ├── main_start_stop.py
|
58 |
+
│ │ └── yolov7_detection_example.py
|
59 |
+
│ ├── image_load
|
60 |
+
│ │ ├── __init__.py
|
61 |
+
│ │ ├── main_load.py
|
62 |
+
│ │ ├── main_start_stop.py
|
63 |
+
│ │ ├── start_load.py
|
64 |
+
│ │ └── stop_load.py
|
65 |
+
│ ├── __init__.py
|
66 |
+
│ └── location_share
|
67 |
+
│ └── __init__.py
|
68 |
+
├── utils
|
69 |
+
│ ├── __init__.py
|
70 |
+
│ ├── __pycache__
|
71 |
+
│ │ ├── __init__.cpython-310.pyc
|
72 |
+
│ │ └── settings.cpython-310.pyc
|
73 |
+
│ └── settings.py
|
74 |
+
├── Dockerfiles
|
75 |
+
├── main.py
|
76 |
+
├── README.md
|
77 |
+
├── requirements.txt
|
78 |
+
└── visualization
|
79 |
+
|
80 |
+
|
81 |
+
```
|
82 |
+
|
83 |
+
## How to Run
|
84 |
+
|
85 |
+
1. **Clone the repository:**
|
86 |
+
```bash
|
87 |
+
git clone https://github.com/ishworrsubedii/gun-detection-alert.git
|
88 |
+
cd cd gun-detection
|
89 |
+
```
|
90 |
+
|
91 |
+
2. **Create and activate the Conda environment:**
|
92 |
+
```bash
|
93 |
+
conda create -n weapon-detection python=3.10 -y
|
94 |
+
conda activate gun-detection
|
95 |
+
```
|
96 |
+
|
97 |
+
3. **Install required packages:**
|
98 |
+
```bash
|
99 |
+
pip install -r requirements.txt
|
100 |
+
```
|
101 |
+
|
102 |
+
4. **Perform Inference/Prediction:**
|
103 |
+
```bash
|
104 |
+
python3 main.py
|
105 |
+
streamlit run streamlit_app.py
|
106 |
+
```
|
107 |
+
|
108 |
+
- We have to run both programs for inference. py for fastapi post request and streamlit for UI for the prediction.
|
109 |
+
|
110 |
+
### Docker
|
111 |
+
|
112 |
+
```commandline
|
113 |
+
```
|
114 |
+
|
115 |
+
# 🔥Features
|
116 |
+
|
117 |
+
# ⚠️ Limitations
|
118 |
+
|
119 |
+
# Future Work
|
120 |
+
|
121 |
+
# Demo
|
122 |
+
|
123 |
+
# Recommendations
|
124 |
+
|
125 |
+
Your recommendations are highly valuable, and I highly value your insights and suggestions to enhance this project! Feel
|
126 |
+
free to propose new features, report bugs, or suggest improvements.
|
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Created By: ishwor subedi
|
3 |
+
Date: 2024-04-04
|
4 |
+
"""
|
5 |
+
import streamlit as st
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
from demos.single_image_inference import single_image_inference
|
9 |
+
|
10 |
+
# Constants
|
11 |
+
MAX_FILE_SIZE = 5 * 1024 * 1024 # 5MB
|
12 |
+
|
13 |
+
# Configure Streamlit
|
14 |
+
st.set_page_config(layout="wide", page_title="Weapon Detection")
|
15 |
+
|
16 |
+
# Write to Streamlit
|
17 |
+
st.write("## Weapon Detection")
|
18 |
+
st.write(
|
19 |
+
"This app uses a custom trained yolov8 model to detect weapons in images. Upload an image to see the detection results."
|
20 |
+
)
|
21 |
+
st.sidebar.write("## Browse images:")
|
22 |
+
|
23 |
+
|
24 |
+
def process_image(upload):
|
25 |
+
"""
|
26 |
+
Process the uploaded image and display the original and processed images side by side.
|
27 |
+
"""
|
28 |
+
try:
|
29 |
+
image = Image.open(upload)
|
30 |
+
col1, col2 = st.columns(2)
|
31 |
+
col1.write("Original Uploaded Image")
|
32 |
+
col1.image(image)
|
33 |
+
|
34 |
+
processed_image = single_image_inference(image)
|
35 |
+
col2.write("Predicted Image")
|
36 |
+
col2.image(processed_image)
|
37 |
+
st.sidebar.markdown("\n")
|
38 |
+
except Exception as e:
|
39 |
+
st.error(f"Error processing image: {e}")
|
40 |
+
|
41 |
+
|
42 |
+
def handle_upload():
|
43 |
+
"""
|
44 |
+
Handle the file upload process.
|
45 |
+
"""
|
46 |
+
uploaded_file = st.sidebar.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
47 |
+
|
48 |
+
if uploaded_file is not None:
|
49 |
+
if uploaded_file.size > MAX_FILE_SIZE:
|
50 |
+
st.error("The uploaded file is too large. Please upload an image smaller than 5MB.")
|
51 |
+
else:
|
52 |
+
process_image(upload=uploaded_file)
|
53 |
+
|
54 |
+
|
55 |
+
# Call the upload handler
|
56 |
+
handle_upload()
|
config/config.ini
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[default]
|
2 |
+
server.host = localhost
|
3 |
+
server.port = 8000
|
4 |
+
|
5 |
+
[example.model_info]
|
6 |
+
yolo.model_path = resources/models/v1/best.pt
|
7 |
+
path.cam_image_dir = images/cam_images
|
demos/__init__.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Created By: ishwor subedi
|
3 |
+
Date: 2023-12-28
|
4 |
+
"""
|
demos/__pycache__/__init__.cpython-39.pyc
ADDED
Binary file (202 Bytes). View file
|
|
demos/__pycache__/single_image_inference.cpython-39.pyc
ADDED
Binary file (1.19 kB). View file
|
|
demos/alert_service_example.py
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Created By: ishwor subedi
|
3 |
+
Date: 2023-12-30
|
4 |
+
"""
|
5 |
+
from services.alert_service.alert_service import AlertService
|
6 |
+
|
7 |
+
if __name__ == '__main__':
|
8 |
+
alert_service = AlertService("resources/alert/alert.mp3")
|
9 |
+
alert_service.play_alert()
|
demos/cam_service_example/__pycache__/start_frame_capturing.cpython-39.pyc
ADDED
Binary file (3.27 kB). View file
|
|
demos/cam_service_example/__pycache__/stop_frame_capturing.cpython-39.pyc
ADDED
Binary file (1.07 kB). View file
|
|
demos/cam_service_example/start_frame_capturing.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
from services.image_capture_service.capture_main import FrameSaver
|
4 |
+
|
5 |
+
|
6 |
+
class ImageCaptureService:
|
7 |
+
def __init__(self, flag_path, source, image_path_to_save, image_hash_threshold):
|
8 |
+
self.flag_path = flag_path
|
9 |
+
self.source = source
|
10 |
+
self.image_path_to_save = image_path_to_save
|
11 |
+
self.image_hash_threshold = image_hash_threshold
|
12 |
+
self.running = False
|
13 |
+
|
14 |
+
def create_stop_flag(self):
|
15 |
+
try:
|
16 |
+
with open(self.flag_path, 'w') as flag_file:
|
17 |
+
flag_file.write('False') # Set initial content as 'False'
|
18 |
+
print("Flag file created and set to 'False'.")
|
19 |
+
except Exception as e:
|
20 |
+
print(f"Error creating flag file: {e}")
|
21 |
+
|
22 |
+
def check_stop_flag(self):
|
23 |
+
try:
|
24 |
+
if os.path.exists(self.flag_path):
|
25 |
+
with open(self.flag_path, 'r') as flag_file:
|
26 |
+
content = flag_file.read().strip()
|
27 |
+
return content.lower() == "true"
|
28 |
+
else:
|
29 |
+
return False
|
30 |
+
except Exception as e:
|
31 |
+
print(f"Error checking stop flag: {e}")
|
32 |
+
return False
|
33 |
+
|
34 |
+
def update_stop_flag(self, value):
|
35 |
+
try:
|
36 |
+
with open(self.flag_path, 'w') as flag_file:
|
37 |
+
flag_file.write(str(value)) # Write the provided value to the flag file
|
38 |
+
except Exception as e:
|
39 |
+
print(f"Error updating stop flag: {e}")
|
40 |
+
|
41 |
+
def start_service(self):
|
42 |
+
try:
|
43 |
+
print("Image Capturing service starting.")
|
44 |
+
self.running = True
|
45 |
+
start_capture = FrameSaver(self.source, self.image_path_to_save, self.image_hash_threshold)
|
46 |
+
start_capture.start_stream()
|
47 |
+
|
48 |
+
while self.running:
|
49 |
+
time.sleep(1)
|
50 |
+
stop_flag = self.check_stop_flag()
|
51 |
+
|
52 |
+
if stop_flag:
|
53 |
+
print("Stop flag detected. Stopping all frame capturing services.")
|
54 |
+
self.running = False
|
55 |
+
stop_successful = start_capture.stop_stream()
|
56 |
+
if stop_successful:
|
57 |
+
print("Frame capturing services successfully stopped.")
|
58 |
+
else:
|
59 |
+
print("Issue encountered while stopping frame capturing services.")
|
60 |
+
|
61 |
+
except Exception as e:
|
62 |
+
print(f"Error in setting up FrameSaver: {e}")
|
63 |
+
print(f"Error in setting up FrameSaver: {e}")
|
64 |
+
finally:
|
65 |
+
if 'start_capture' in locals():
|
66 |
+
stop_successful = start_capture.stop_stream()
|
67 |
+
if stop_successful:
|
68 |
+
print("Frame capturing services successfully stopped in finally block.")
|
69 |
+
else:
|
70 |
+
print("Issue encountered while stopping frame capturing services in finally block.")
|
71 |
+
|
72 |
+
def stop_service(self):
|
73 |
+
self.update_stop_flag("True")
|
74 |
+
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
FLAG_PATH = "resources/image_capturing"
|
78 |
+
# SOURCE = 'rtsp://ishwor:subedi@192.168.1.106:5555/h264_opus.sdp'
|
79 |
+
SOURCE = 0
|
80 |
+
IMAGE_PATH_TO_SAVE = "images/cam_images"
|
81 |
+
IMAGE_HASH_THRESHOLD = 5
|
82 |
+
|
83 |
+
image_capture_service = ImageCaptureService(FLAG_PATH, SOURCE, IMAGE_PATH_TO_SAVE, IMAGE_HASH_THRESHOLD)
|
84 |
+
image_capture_service.create_stop_flag()
|
85 |
+
image_capture_service.start_service()
|
86 |
+
time.sleep(1)
|
demos/cam_service_example/stop_frame_capturing.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
|
4 |
+
class StopImageCaptureServiceExample:
|
5 |
+
def __init__(self, stop_flag_path):
|
6 |
+
self.stop_flag_path = stop_flag_path
|
7 |
+
|
8 |
+
def stop_example_ipcam_webcam(self):
|
9 |
+
try:
|
10 |
+
with open(self.stop_flag_path, 'w') as flag_file:
|
11 |
+
flag_file.write("True")
|
12 |
+
print("Stop flag set.")
|
13 |
+
os.remove(self.stop_flag_path)
|
14 |
+
except Exception as e:
|
15 |
+
print(f"Error setting stop flag: {e}")
|
16 |
+
|
17 |
+
|
18 |
+
if __name__ == "__main__":
|
19 |
+
STOP_FLAG_PATH = "resources/flag"
|
20 |
+
stop_load_image_example = StopImageCaptureServiceExample(STOP_FLAG_PATH)
|
21 |
+
stop_load_image_example.stop_example_ipcam_webcam()
|
demos/image_load_service_example/__pycache__/start_image_load_example.cpython-39.pyc
ADDED
Binary file (3.18 kB). View file
|
|
demos/image_load_service_example/__pycache__/stop_image_load_example.cpython-39.pyc
ADDED
Binary file (1.19 kB). View file
|
|
demos/image_load_service_example/start_image_load_example.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import time
|
4 |
+
import threading
|
5 |
+
from services.image_capture_service.image_load_main import ImageLoad
|
6 |
+
|
7 |
+
|
8 |
+
class StartImageLoadExample:
|
9 |
+
def __init__(self, flag_path, image_dir, model_path):
|
10 |
+
self.flag_path = flag_path
|
11 |
+
self.image_dir = image_dir
|
12 |
+
self.model_path = model_path
|
13 |
+
|
14 |
+
self.running = False
|
15 |
+
|
16 |
+
def create_stop_flag(self):
|
17 |
+
try:
|
18 |
+
with open(self.flag_path, 'w') as flag_file:
|
19 |
+
flag_file.write('False')
|
20 |
+
except Exception as e:
|
21 |
+
print(f"Error creating stop flag: {e}")
|
22 |
+
|
23 |
+
def check_stop_flag(self):
|
24 |
+
try:
|
25 |
+
if os.path.exists(self.flag_path):
|
26 |
+
with open(self.flag_path, 'r') as flag_file:
|
27 |
+
content = flag_file.read().strip()
|
28 |
+
return content.lower() == "true"
|
29 |
+
else:
|
30 |
+
return False
|
31 |
+
except Exception as e:
|
32 |
+
print(f"Error checking stop flag: {e}")
|
33 |
+
return False
|
34 |
+
|
35 |
+
def update_stop_flag(self, value):
|
36 |
+
try:
|
37 |
+
with open(self.flag_path, 'w') as flag_file:
|
38 |
+
flag_file.write(str(value))
|
39 |
+
except Exception as e:
|
40 |
+
print(f"Error updating stop flag: {e}")
|
41 |
+
|
42 |
+
def start_service(self):
|
43 |
+
try:
|
44 |
+
self.running = True
|
45 |
+
start_load = ImageLoad(self.image_dir, self.model_path)
|
46 |
+
start_load.start_load_image()
|
47 |
+
print("Image loading service successfully started")
|
48 |
+
|
49 |
+
while self.running:
|
50 |
+
time.sleep(1)
|
51 |
+
stop_flag = self.check_stop_flag()
|
52 |
+
|
53 |
+
if stop_flag:
|
54 |
+
self.running = False
|
55 |
+
stop_successful = start_load.stop_load_image()
|
56 |
+
if stop_successful:
|
57 |
+
print("Image loading services_trinetra successfully stopped.")
|
58 |
+
else:
|
59 |
+
print("Issue encountered while stopping image loading services_trinetra.")
|
60 |
+
|
61 |
+
except Exception as e:
|
62 |
+
print(f"Error starting image loading services_trinetra: {e}")
|
63 |
+
finally:
|
64 |
+
if 'start_load' in locals():
|
65 |
+
stop_successful = start_load.stop_load_image()
|
66 |
+
if stop_successful:
|
67 |
+
print("Image loading services_trinetra successfully stopped in finally block.")
|
68 |
+
else:
|
69 |
+
print(
|
70 |
+
"Issue encountered while stopping image loading services_trinetra in finally block.")
|
71 |
+
|
72 |
+
def stop_service(self):
|
73 |
+
self.update_stop_flag("True")
|
74 |
+
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
flag_path = "resources/flag_load_image"
|
78 |
+
image_dir = 'images/cam_images'
|
79 |
+
model_path = 'resources/models/v1/best.pt'
|
80 |
+
|
81 |
+
image_load_service = StartImageLoadExample(flag_path, image_dir, model_path)
|
82 |
+
image_load_service.create_stop_flag()
|
83 |
+
image_load_service.start_service()
|
84 |
+
time.sleep(1)
|
demos/image_load_service_example/stop_image_load_example.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Created By: ishwor subedi
|
3 |
+
Date: 2023-12-28
|
4 |
+
"""
|
5 |
+
|
6 |
+
import os
|
7 |
+
import os
|
8 |
+
from services import ipcam_logger
|
9 |
+
|
10 |
+
|
11 |
+
class StopImageLoadServiceExample:
|
12 |
+
def __init__(self, stop_flag_path):
|
13 |
+
self.stop_flag_path = stop_flag_path
|
14 |
+
self.logger = ipcam_logger()
|
15 |
+
|
16 |
+
def stop_service(self):
|
17 |
+
try:
|
18 |
+
with open(self.stop_flag_path, 'w') as flag_file:
|
19 |
+
flag_file.write("True")
|
20 |
+
self.logger.info("Stop flag set.")
|
21 |
+
except Exception as e:
|
22 |
+
self.logger.error(f"Error setting stop flag: {e}")
|
23 |
+
|
24 |
+
|
25 |
+
if __name__ == "__main__":
|
26 |
+
STOP_FLAG_PATH = "resources/flag_load_image"
|
27 |
+
webcam_controller = StopImageLoadServiceExample(STOP_FLAG_PATH)
|
28 |
+
webcam_controller.stop_service()
|
demos/single_image_inference.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Created By: ishwor subedi
|
3 |
+
Date: 2024-04-04
|
4 |
+
"""
|
5 |
+
from services.weapon_det_service.weapon_detection_service import DetectionService
|
6 |
+
import cv2 as cv
|
7 |
+
|
8 |
+
|
9 |
+
def single_image_inference(image_path):
|
10 |
+
detection_service = DetectionService(
|
11 |
+
model_path='resources/models/v1/best.pt',
|
12 |
+
|
13 |
+
)
|
14 |
+
results = detection_service.image_det_save(
|
15 |
+
image_path=image_path,
|
16 |
+
thresh=0.2
|
17 |
+
)
|
18 |
+
for result in results:
|
19 |
+
original_image = result.orig_img
|
20 |
+
bbox = result.boxes.xyxy.int().tolist()
|
21 |
+
for i, bbox in enumerate(bbox):
|
22 |
+
x1, y1, x2, y2 = bbox
|
23 |
+
|
24 |
+
cv.putText(original_image, f'{result.names[result.boxes.cls[i].int().tolist()]}', (x1, y1 - 10),
|
25 |
+
cv.FONT_HERSHEY_SIMPLEX,
|
26 |
+
0.9,
|
27 |
+
(0, 0, 255), 2)
|
28 |
+
cv.rectangle(original_image, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
29 |
+
|
30 |
+
return original_image
|
31 |
+
|
32 |
+
|
33 |
+
if __name__ == '__main__':
|
34 |
+
image_path = '/home/ishwor/Desktop/gun-detection/images/cam_images/th-3711382641.jpg'
|
35 |
+
image = single_image_inference(image_path)
|
36 |
+
cv.imshow('Weapon Detection', image)
|
37 |
+
cv.waitKey(0)
|
38 |
+
cv.destroyAllWindows()
|
images/cam_images/PY-4856_Crosman-Bushmaster-MPW-Full_1558033480-458822316.jpg
ADDED
images/cam_images/th-2208139001.jpg
ADDED
images/cam_images/th-3711382641.jpg
ADDED
logs/gun_det.log
ADDED
File without changes
|
logs/ipcam.log
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
[2024-04-04 00:05:06,251:INFO:stop_image_load_example:Stop flag set.]
|
main.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Created By: ishwor subedi
|
3 |
+
Date: 2024-01-02
|
4 |
+
"""
|
5 |
+
import uvicorn
|
6 |
+
|
7 |
+
from services.api.fast_api import app
|
8 |
+
|
9 |
+
if __name__ == "__main__":
|
10 |
+
uvicorn.run(app, host="localhost", port=8000)
|
resources/alert/alert.mp3
ADDED
Binary file (150 kB). View file
|
|
resources/flag_load_image
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
True
|
resources/image_capturing
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
False
|
resources/models/best.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:21d61ad8068caca3062d33fd8d05da445ef9ae81a2bb817249e085b0f1307cf0
|
3 |
+
size 6263257
|
resources/models/v1/best.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4937605a869713f16e72981374933daf2ce884ff5603a91c42403ece282c2988
|
3 |
+
size 12349682
|
resources/models/weapon_detector.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:74ee5662c4a57267ac270068aa20a805b221f1ffe6d88c08c9131b9eb873b296
|
3 |
+
size 74802191
|
resources/models/yolov7.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d1ac3c74eb96a3eec77949c0f37a06bc272756606ff011d9353f7abff4e0c71d
|
3 |
+
size 75587165
|
resources/models/yolov7_2.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d1ac3c74eb96a3eec77949c0f37a06bc272756606ff011d9353f7abff4e0c71d
|
3 |
+
size 75587165
|
services/__init__.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import logging
|
4 |
+
|
5 |
+
|
6 |
+
def configure_logger(logger_name, log_filename, log_dir="logs"):
|
7 |
+
"""
|
8 |
+
Configures a logger with specified parameters.
|
9 |
+
|
10 |
+
:param logger_name: Name of the logger.
|
11 |
+
:param log_filename: Name of the log file.
|
12 |
+
:param log_dir: Directory where logs will be stored. Defaults to "logs".
|
13 |
+
:return: Configured logger.
|
14 |
+
"""
|
15 |
+
logging_str = "[%(asctime)s:%(levelname)s:%(module)s:%(message)s]"
|
16 |
+
|
17 |
+
log_path = os.path.join(log_dir, log_filename)
|
18 |
+
os.makedirs(log_dir, exist_ok=True)
|
19 |
+
|
20 |
+
logger = logging.getLogger(logger_name)
|
21 |
+
logger.setLevel(logging.INFO)
|
22 |
+
|
23 |
+
formatter = logging.Formatter(logging_str)
|
24 |
+
|
25 |
+
file_handler = logging.FileHandler(log_path)
|
26 |
+
file_handler.setFormatter(formatter)
|
27 |
+
|
28 |
+
stream_handler = logging.StreamHandler(sys.stdout)
|
29 |
+
stream_handler.setFormatter(formatter)
|
30 |
+
|
31 |
+
logger.addHandler(file_handler)
|
32 |
+
logger.addHandler(stream_handler)
|
33 |
+
|
34 |
+
return logger
|
35 |
+
|
36 |
+
|
37 |
+
def main_sys_logger(log_dir="logs"):
|
38 |
+
"""
|
39 |
+
Configures and returns the main system logger.
|
40 |
+
|
41 |
+
:param log_dir: Directory where logs will be stored. Defaults to "logs".
|
42 |
+
:return: Main system logger.
|
43 |
+
"""
|
44 |
+
return configure_logger("main_sys_logger", "gun_det.log", log_dir)
|
45 |
+
|
46 |
+
|
47 |
+
def ipcam_logger(log_dir="logs"):
|
48 |
+
"""
|
49 |
+
Configures and returns the IP camera logger.
|
50 |
+
|
51 |
+
:param log_dir: Directory where logs will be stored. Defaults to "logs".
|
52 |
+
:return: IP camera logger.
|
53 |
+
"""
|
54 |
+
return configure_logger("ipcam_logger", "ipcam.log", log_dir)
|
55 |
+
|
56 |
+
|
57 |
+
def detection_logger(log_dir="logs"):
|
58 |
+
"""
|
59 |
+
Configures and returns the detection logger.
|
60 |
+
|
61 |
+
:param log_dir: Directory where logs will be stored. Defaults to "logs".
|
62 |
+
:return: Detection logger.
|
63 |
+
"""
|
64 |
+
return configure_logger("detection_logger", "detection.log", log_dir)
|
services/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (1.83 kB). View file
|
|
services/__pycache__/__init__.cpython-39.pyc
ADDED
Binary file (1.97 kB). View file
|
|
services/alert_service/__init__.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Created By: ishwor subedi
|
3 |
+
Date: 2023-12-29
|
4 |
+
"""
|
services/alert_service/__pycache__/__init__.cpython-39.pyc
ADDED
Binary file (216 Bytes). View file
|
|
services/alert_service/__pycache__/alert_service.cpython-39.pyc
ADDED
Binary file (1.12 kB). View file
|
|
services/alert_service/alert_service.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Created By: ishwor subedi
|
3 |
+
Date: 2023-12-30
|
4 |
+
"""
|
5 |
+
|
6 |
+
import pygame
|
7 |
+
|
8 |
+
|
9 |
+
class AlertService:
|
10 |
+
def __init__(self, alert_sound_path):
|
11 |
+
self.alert_sound_path = alert_sound_path
|
12 |
+
pygame.init()
|
13 |
+
|
14 |
+
def play_alert(self):
|
15 |
+
"""
|
16 |
+
This function help us to play the alert sound
|
17 |
+
:return:
|
18 |
+
"""
|
19 |
+
try:
|
20 |
+
pygame.mixer.init()
|
21 |
+
alert_sound = pygame.mixer.Sound(self.alert_sound_path)
|
22 |
+
alert_sound.play()
|
23 |
+
pygame.time.wait(int(alert_sound.get_length() * 1000)) # Wait for the sound to finish playing
|
24 |
+
except pygame.error as e:
|
25 |
+
print(f"Error playing alert sound: {e}")
|
26 |
+
|
27 |
+
|
28 |
+
if __name__ == '__main__':
|
29 |
+
alert_service = AlertService("resources/alert/alert.mp3")
|
30 |
+
alert_service.play_alert()
|
services/api/__init__.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Created By: ishwor subedi
|
3 |
+
Date: 2023-12-28
|
4 |
+
"""
|
services/api/__pycache__/__init__.cpython-39.pyc
ADDED
Binary file (206 Bytes). View file
|
|
services/api/__pycache__/fast_api.cpython-39.pyc
ADDED
Binary file (3.89 kB). View file
|
|
services/api/fast_api.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
from fastapi import HTTPException
|
4 |
+
import uvicorn
|
5 |
+
import base64
|
6 |
+
import io
|
7 |
+
from PIL import Image
|
8 |
+
from demos.cam_service_example.start_frame_capturing import ImageCaptureService
|
9 |
+
from demos.cam_service_example.stop_frame_capturing import StopImageCaptureServiceExample
|
10 |
+
from demos.image_load_service_example.start_image_load_example import StartImageLoadExample
|
11 |
+
from demos.image_load_service_example.stop_image_load_example import StopImageLoadServiceExample
|
12 |
+
from demos.single_image_inference import single_image_inference
|
13 |
+
from pydantic import BaseModel
|
14 |
+
|
15 |
+
app = FastAPI()
|
16 |
+
|
17 |
+
|
18 |
+
# Helper functions
|
19 |
+
def start_ipcam():
|
20 |
+
flag_path = "resources/flag"
|
21 |
+
|
22 |
+
source = 'rtsp://ishwor:subedi@192.168.1.106:5555/h264_opus.sdp'
|
23 |
+
|
24 |
+
image_path_to_save = "images/cam_images"
|
25 |
+
image_hash_threshold = 5
|
26 |
+
image_capture_start_example = ImageCaptureService(flag_path, source, image_path_to_save,
|
27 |
+
image_hash_threshold)
|
28 |
+
image_capture_start_example.start_service()
|
29 |
+
|
30 |
+
|
31 |
+
def stop_ipcam():
|
32 |
+
flag_path = "resources/flag"
|
33 |
+
|
34 |
+
image_capture_stop_example = StopImageCaptureServiceExample(flag_path)
|
35 |
+
image_capture_stop_example.stop_load_image_example()
|
36 |
+
|
37 |
+
|
38 |
+
def start_detection():
|
39 |
+
flag_path = "resources/flag_load_image"
|
40 |
+
|
41 |
+
image_dir_path = "images/cam_images"
|
42 |
+
|
43 |
+
start_load_image_example = StartImageLoadExample(flag_path, image_dir_path, model_path="resources/model/v1/best.pt")
|
44 |
+
start_load_image_example.start_service()
|
45 |
+
|
46 |
+
|
47 |
+
def stop_detection():
|
48 |
+
flag_path = "resources/flag_load_image"
|
49 |
+
|
50 |
+
stop_load_image_example = StopImageLoadServiceExample(flag_path)
|
51 |
+
stop_load_image_example.stop_service()
|
52 |
+
|
53 |
+
|
54 |
+
# FastAPI Endpoints
|
55 |
+
@app.post("/start_ipcam_server")
|
56 |
+
def start_ipcam_server_api():
|
57 |
+
try:
|
58 |
+
start_ipcam()
|
59 |
+
return JSONResponse(content={"message": "IP Cam Server started!"}, status_code=200)
|
60 |
+
except Exception as e:
|
61 |
+
raise HTTPException(status_code=500, detail=str(e))
|
62 |
+
|
63 |
+
|
64 |
+
@app.post("/stop_ipcam_server")
|
65 |
+
def stop_ipcam_server_api():
|
66 |
+
try:
|
67 |
+
stop_ipcam()
|
68 |
+
return JSONResponse(content={"message": "IP Cam Server stopped!"}, status_code=200)
|
69 |
+
except Exception as e:
|
70 |
+
raise HTTPException(status_code=500, detail=str(e))
|
71 |
+
|
72 |
+
|
73 |
+
@app.post("/start_detection_service")
|
74 |
+
def start_detection_service_api():
|
75 |
+
try:
|
76 |
+
start_detection()
|
77 |
+
return JSONResponse(content={"message": "Image loading Server started!"}, status_code=200)
|
78 |
+
except Exception as e:
|
79 |
+
raise HTTPException(status_code=500, detail=str(e))
|
80 |
+
|
81 |
+
|
82 |
+
@app.post("/stop_detection_service")
|
83 |
+
def stop_detection_service_api():
|
84 |
+
try:
|
85 |
+
stop_detection()
|
86 |
+
return JSONResponse(content={"message": "Image loading Server stopped!"}, status_code=200)
|
87 |
+
except Exception as e:
|
88 |
+
raise HTTPException(status_code=500, detail=str(e))
|
89 |
+
|
90 |
+
|
91 |
+
class ImageInput(BaseModel):
|
92 |
+
base64_image: str
|
93 |
+
|
94 |
+
|
95 |
+
@app.post("/single_image_detection")
|
96 |
+
def single_image_detection_api(image_input: ImageInput):
|
97 |
+
try:
|
98 |
+
image_data = base64.b64decode(image_input.base64_image)
|
99 |
+
|
100 |
+
image = Image.open(io.BytesIO(image_data))
|
101 |
+
image = single_image_inference(image)
|
102 |
+
encoded_img = base64.b64encode(image)
|
103 |
+
return encoded_img
|
104 |
+
except Exception as e:
|
105 |
+
raise HTTPException(status_code=500, detail=str(e))
|
106 |
+
|
107 |
+
|
108 |
+
if __name__ == "__main__":
|
109 |
+
uvicorn.run(app, host="localhost", port=8001)
|
services/entity/__init__.py
ADDED
File without changes
|
services/entity/entity_config.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dataclasses import dataclass
|
2 |
+
from pathlib import Path
|
3 |
+
|
4 |
+
|
5 |
+
@dataclass(frozen=True)
|
6 |
+
class StreamLitConfig:
|
7 |
+
pass
|
services/image_capture_service/__init__.py
ADDED
File without changes
|
services/image_capture_service/__pycache__/__init__.cpython-39.pyc
ADDED
Binary file (168 Bytes). View file
|
|
services/image_capture_service/__pycache__/capture_main.cpython-39.pyc
ADDED
Binary file (2.61 kB). View file
|
|
services/image_capture_service/__pycache__/image_load_main.cpython-39.pyc
ADDED
Binary file (2.12 kB). View file
|
|
services/image_capture_service/capture_main.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Author: ishwor subedi
|
3 |
+
Date: 2023-12-28
|
4 |
+
Project Name:gun-detection
|
5 |
+
|
6 |
+
"""
|
7 |
+
import os
|
8 |
+
from PIL import Image
|
9 |
+
from datetime import datetime
|
10 |
+
import threading
|
11 |
+
import imagehash
|
12 |
+
import cv2 as cv
|
13 |
+
from utils.settings import get_frame_save_dir
|
14 |
+
|
15 |
+
|
16 |
+
class FrameSaver:
|
17 |
+
def __init__(self, source, image_path_to_save, image_hash_threshold):
|
18 |
+
self.source = source
|
19 |
+
self.image_path_to_save = image_path_to_save
|
20 |
+
if self.image_path_to_save is None or not os.path.exists(self.image_path_to_save):
|
21 |
+
self.image_path_to_save = get_frame_save_dir()
|
22 |
+
self.thread_running = False
|
23 |
+
self.frame_save_thread = None
|
24 |
+
self.cap = cv.VideoCapture(self.source)
|
25 |
+
self.image_hash_threshold = image_hash_threshold
|
26 |
+
|
27 |
+
def start_stream(self):
|
28 |
+
self.frame_save_thread = threading.Thread(target=self.video_webcam_frame_extraction)
|
29 |
+
self.frame_save_thread.start()
|
30 |
+
self.thread_running = True
|
31 |
+
|
32 |
+
def stop_stream(self):
|
33 |
+
if self.thread_running:
|
34 |
+
self.thread_running = False
|
35 |
+
self.frame_save_thread.join()
|
36 |
+
self.cap.release()
|
37 |
+
|
38 |
+
def hashing_diff(self, prev_frame, current_frame):
|
39 |
+
if prev_frame is None:
|
40 |
+
return None
|
41 |
+
else:
|
42 |
+
hash1 = imagehash.average_hash(Image.fromarray(prev_frame))
|
43 |
+
hash2 = imagehash.average_hash(Image.fromarray(current_frame))
|
44 |
+
return hash2 - hash1
|
45 |
+
|
46 |
+
def video_webcam_frame_extraction(self):
|
47 |
+
"""
|
48 |
+
|
49 |
+
:return:
|
50 |
+
"""
|
51 |
+
|
52 |
+
try:
|
53 |
+
|
54 |
+
previous_frame = None
|
55 |
+
while self.cap.isOpened():
|
56 |
+
success, frame = self.cap.read()
|
57 |
+
if success:
|
58 |
+
hash_diff = self.hashing_diff(previous_frame, frame)
|
59 |
+
if hash_diff is None or hash_diff > self.image_hash_threshold:
|
60 |
+
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
61 |
+
name = os.path.join(self.image_path_to_save, f"{current_time}.jpg")
|
62 |
+
success = cv.imwrite(name, frame)
|
63 |
+
if success:
|
64 |
+
print(f"Saved {name}...")
|
65 |
+
else:
|
66 |
+
print(f"Failed to save image: {name}")
|
67 |
+
previous_frame = frame
|
68 |
+
except Exception as e:
|
69 |
+
print(f"Exception occurred: {e}")
|
70 |
+
try:
|
71 |
+
self.stop_stream()
|
72 |
+
except Exception as e:
|
73 |
+
print(f"Failed to stop stream: {e}")
|
74 |
+
|
75 |
+
|
76 |
+
if __name__ == '__main__':
|
77 |
+
source = 'rtsp://ishwor:subedi@192.168.1.106:5555/h264_opus.sdp'
|
78 |
+
image_path_to_save = "images/cam_images"
|
79 |
+
image_hash_threshold = 5
|
80 |
+
image_capture_service = FrameSaver(source, image_path_to_save, image_hash_threshold)
|
81 |
+
image_capture_service.video_webcam_frame_extraction()
|