Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

important parallelization improvements and fixes #17

Merged
merged 6 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ dist
# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
# VSCode files
.vscode-test
.vscode

# yarn v2
.yarn/cache
Expand Down
2 changes: 0 additions & 2 deletions Backend/.env-openvidu
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
OPENVIDU_SECRET=MY_SECRET
OPENVIDU_WEBHOOK=true
OPENVIDU_WEBHOOK_ENDPOINT=https://localhost:8080/media-server


Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class MachineLearningController {
new ParseFilePipeBuilder()
.addFileTypeValidator({ fileType: 'image/jpeg' })
.addMaxSizeValidator({
maxSize: 100000, // 100Kb
maxSize: 50000000, // 100Kb
})
.build({
errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
Expand Down
1 change: 0 additions & 1 deletion Backend/src/cameraStream/cameraStream.gateway.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Test, TestingModule } from '@nestjs/testing';
import { CameraStreamGateway } from './cameraStream.gateway';
import { JwtModule } from '@nestjs/jwt';

import { OpenVidu } from 'openvidu-node-client';
import { CSSOpenVidu } from './open-vidu.service';
import { DatabaseService } from '../database/database.service';

Expand Down
2 changes: 1 addition & 1 deletion Backend/src/database/database.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('DatabaseService', () => {
});

it('should get aggregated data', async () => {
const aggregateData = await databaseService.aggregateCamera();
const aggregateData = await databaseService.aggregateCamera('all');

expect(aggregateData).not.toBeNull();
});
Expand Down
87 changes: 39 additions & 48 deletions Backend/src/database/database.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,29 @@ export class DatabaseService {
);
}

async initDBNvr() {
const data = {
name: 'NVR',
};
const filter = {
name: 'NVR',
ip: process.env.NVR_IP_ADDRESS,
channels: [0, 1, 2, 3, 4, 5, 6, 7],
};

const size = await this.DB.collection('General').countDocuments(filter);

if (size == 0) {
await this.DB.collection('General').insertOne(data);
}
}

constructor() {
const client = new MongoClient(url);

this.DB = client.db('csd');
this.initDBUser();
this.initDBNvr();
}

async addData(data: DataType<number>) {
Expand Down Expand Up @@ -101,9 +119,23 @@ export class DatabaseService {
else return res.toArray();
}

aggregateCamera(filter?: FiltersAvailable): Promise<Document[]> {
aggregateCamera(filter: FiltersAvailable): Promise<Document[]> {
return this.DB.collection('cameras')
.aggregate()
.aggregate([
{
$addFields: {
intrusionDetection: {
$cond: {
if: {
$ifNull: ['$intrusionDetection', false],
},
then: true,
else: false,
},
},
},
},
])
.match(this.getFilter(filter))
.group({
_id: '$cameraId',
Expand Down Expand Up @@ -149,7 +181,7 @@ export class DatabaseService {
});
}

private getFilter(filter?: FiltersAvailable) {
private getFilter(filter: FiltersAvailable) {
switch (filter) {
case 'intrusionDetection':
return {
Expand Down Expand Up @@ -206,54 +238,13 @@ export class DatabaseService {
// TODO TESTME
// Returns NVR info such as IP address and available channels
async getNVRData(): Promise<Document> {
const array = await this.getOtherwiseInsert(
'General',
{
name: 'NVR',
},
{
name: 'NVR',
ip: process.env.NVR_IP_ADDRESS,
channels: [0, 1, 2, 3, 4, 5, 6, 7],
},
);
const array = await this.getRawDataArray('General', {
name: 'NVR',
});

return {
ip: array[0].ip,
channels: array[0].channels,
};

// try {
// const array = await this.getRawDataArray('General', {
// name: 'NVR',
// });
// return array[0];
// } catch (e) {
// if (e instanceof NotFoundException) {
// const data = {
// name: 'NVR',
// ip: process.env.NVR_IP_ADDRESS,
// channels: [0, 1, 2, 3, 4, 5, 6, 7],
// };
//
// await this.DB.collection(`General`).insertOne(data);
// return data;
// } else {
// console.error(e);
// }
// }
}

async getOtherwiseInsert(
name: string,
filter: Filter<Document>,
data: Document,
): Promise<WithId<Document>[]> {
const size = await this.DB.collection(name).countDocuments(filter);

if (size == 0) {
await this.DB.collection(name).insertOne(data);
}

return await this.getRawDataArray(name, filter);
}
}
24 changes: 24 additions & 0 deletions ImageRecognition/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Technology
<p align="center">
<a href="https://www.ultralytics.com" target="blank"><img src="https://assets-global.website-files.com/646dd1f1a3703e451ba81ecc/64777c3e071ec953437e6950_logo.svg" alt="Ultralytics Yolo" /></a>
</p>

Ultralytics YOLO for building accurate and efficient object detection models.

## Running the app
First, open the terminal and move to the ImageRecognition directiory.
Run send_frame.py for sending camera frames:

```bash
python send_frame.py
```

To start the object detection function move to the src directory and run ObjectDetectionYOLO.py:

```bash
python ObjectDetectionYOLO.py
```

## Learn More
To learn more about YOLO architecture, take a look at the following resource:
- [YOLO Documentation](https://docs.ultralytics.com).
19 changes: 19 additions & 0 deletions ImageRecognition/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import concurrent.futures
from src.ObjectDetectionYOLO import detection

if __name__ == "__main__":
with concurrent.futures.ThreadPoolExecutor() as executor:
args: (int, int) = [(i, i) for i in range(0, 8)]

results = [executor.submit(detection, *arg) for arg in args]
concurrent.futures.wait(results)


cam1 = "rtsp://192.168.1.41:80/ch0_0.264"
cam2 = "rtsp://192.168.1.41:80/ch1_0.264"
cam3 = "rtsp://192.168.1.41:80/ch2_0.264"
cam4 = "rtsp://192.168.1.41:80/ch3_0.264"
cam5 = "rtsp://192.168.1.41:80/ch4_0.264"
cam6 = "rtsp://192.168.1.41:80/ch5_0.264"
cam7 = "rtsp://192.168.1.41:80/ch6_0.264"
cam8 = "rtsp://192.168.1.41:80/ch7_0.264"
4 changes: 3 additions & 1 deletion ImageRecognition/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
opencv-python~=4.8.1.78
numpy~=1.26.1
ping3~=4.0.4
ffmpeg-python~=0.2.0
ultralytics>=8.1.5
scikit-learn~=1.3.2
torch>=1.8
3 changes: 1 addition & 2 deletions ImageRecognition/send_frame.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import cv2
import socketio
import base64
import threading

#cameras data
Expand Down Expand Up @@ -40,7 +39,7 @@ def send_frames(camera_id, capture, interval=0.1):
for camera_id, camera_url in cameras.items():
capture = cv2.VideoCapture(camera_url)
if not capture.isOpened():
print(f"Errore nell'apertura della telecamera {camera_id}")
print(f"Error stream with camera {camera_id}")
continue

threading.Thread(target=send_frames, args=(camera_id, capture)).start()
Expand Down
Loading
Loading