-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase.py
More file actions
116 lines (90 loc) · 3.9 KB
/
Copy pathbase.py
File metadata and controls
116 lines (90 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import cv2
import time
import tensorflow as tf
import numpy as np
import logging
from PIL import Image
from keras.models import load_model
from keras.preprocessing.image import img_to_array
from datetime import datetime
from PySide6 import QtCore
# local imports
from DataBase import store_alert_data
from cloudStorage import upload_blob
from AlertAdmin import send_sms
from multiprocess import QValkkaOpenCVProcess
from .PersonDetector import PersonDetector
class QValkkaRobberyDetectorProcess(QValkkaOpenCVProcess):
incoming_signal_defs = { # each key corresponds to a front- and backend methods
"create_client_": [],
"test_": {"test_int": int, "test_str": str},
"stop_": [],
"ping_": {"message": str}
}
outgoing_signal_defs = {
"pong_o": {"message": str},
"Robbery_detected": {},
}
# For each outgoing signal, create a Qt signal with the same name. The
# frontend Qt thread will read processes communication pipe and emit these
# signals.
class Signals(QtCore.QObject):
# PySide2 version:
pong_o = QtCore.Signal(object)
start_move = QtCore.Signal()
Robbery_detected = QtCore.Signal()
stop_move = QtCore.Signal()
def __init__(self, name, **kwargs):
super().__init__(name, **kwargs) # does parameterInitCheck
self.signals = self.Signals()
self.personDetector = PersonDetector()
self.RobberyDetector = load_model('/home/iheb/PycharmProjects/Vision-Alarm/MachineVision/RobberyDetection/Robbery_Detection_Model3.h5')
def alarm(self):
logging.debug(f"Robbery detected")
self.sendSignal_(name="Robbery_detected")
def cycle_(self):
"""
Cycle function will be automatically called within QValka main process and runs the expected Machine vision analyses
on the passed frames.
If robbery detected, The frames of the incident will be stored in the cloud And the admin will be alerted through an SMS using twilio.
"""
if self.client is None:
time.sleep(1.0)
logging('client timedout')
else:
index, isize = self.client.pull()
if (index is None):
logging(f"{self.pre} Client timed out..")
pass
else:
logging(f"Client index: {index} size: {isize}")
try:
data = self.client.shmem_list[index]
except BaseException:
print("There is an issue in getting data from shmem_list")
try:
img = data.reshape(
(self.image_dimensions[1], self.image_dimensions[0], 3))
except BaseException:
print("QValkkaRobberyDetectorProcess: WARNING: could not reshape image")
print("Let's try our model for each input image : \n")
img_resized = cv2.resize(img, (224, 224))
img = Image.fromarray(img_resized)
logging(img_resized.shape)
img_array = img_to_array(img=img)
img_array = tf.expand_dims(img_array, 0)
logging("img : ",type(img))
logging("img_array : ",type(img_array))
try:
predictions = self.RobberyDetector.predict(img_array)
logging("preds :",predictions)
score = predictions[0]
logging("This image is %.2f No Robber and %.2f Robbery" %(100 * (1-score), 100 * score))
except Exception as e:
print(f"Unable to predict image class : {e}")
def Robbery_detected(self):
"""
Emits the robbery detection signal when a robbery is detected.
"""
logging("At frontend: Robbery detected ")
self.signals.Robbery_detected.emit()