Commit 85dfc12b authored by Sikhin VC's avatar Sikhin VC

code changes for staging

parent 310b078e
This source diff could not be displayed because it is too large. You can view the blob instead.
import os import os
# os.environ["config"]="{\"TZ\": \"Asia/Kolkata\", \"MONGO_URI\": \"mongodb://svc-ilens:svc2345@192.168.3.220:21017\", \"MONGO_DATABASE\": \"ilens_wps\", \"MONGO_COLLECTION\": \"janusDeployment\", \"MONGO_KEY\": \"deploymentId\", \"MONGO_VALUE\": \"rahul_12345\", \"MONGO_COLL\": \"serviceConfiguration\", \"MONGO_DB\": \"ilens_wps\"}" # os.environ["config"]="{\"TZ\": \"Asia/Kolkata\", \"MONGO_URI\": \"mongodb://svc-ilens:svc2345@192.168.3.220:21017\", \"MONGO_DATABASE\": \"ilens_wps\", \"MONGO_COLLECTION\": \"janusDeployment\", \"MONGO_KEY\": \"deploymentId\", \"MONGO_VALUE\": \"rahul_12345\", \"MONGO_COLL\": \"serviceConfiguration\", \"MONGO_DB\": \"ilens_wps\"}"
# os.environ["config"]="{\"TZ\": \"Asia/Kolkata\", \"MONGO_URI\": \"mongodb://admin:iLens$HPCLv605@10.5.2.91:2717\", \"MONGO_DATABASE\": \"ilens_ai\", \"MONGO_COLLECTION\": \"janusDeployment\", \"MONGO_KEY\": \"deploymentId\", \"MONGO_VALUE\": \"hpcl_cctv_ppe\", \"MONGO_COLL\": \"serviceConfiguration\", \"MONGO_DB\": \"ilens_ai\"}" # os.environ["config"]="{\"TZ\": \"Asia/Kolkata\", \"MONGO_URI\": \"mongodb://admin:iLens$HPCLv605@10.5.2.91:2717\", \"MONGO_DATABASE\": \"ilens_ai\", \"MONGO_COLLECTION\": \"janusDeployment\", \"MONGO_KEY\": \"deploymentId\", \"MONGO_VALUE\": \"hpcl_cctv_ppe\", \"MONGO_COLL\": \"serviceConfiguration\", \"MONGO_DB\": \"ilens_ai\"}"
# os.environ["config"]="{\"TZ\": \"Asia/Kolkata\", \"MONGO_URI\": \"mongodb://admin:iLens$1234@192.168.3.181:2717/admin\", \"MONGO_DATABASE\": \"ilens_ai\", \"MONGO_COLLECTION\": \"janusDeployment\", \"MONGO_KEY\": \"deploymentId\", \"MONGO_VALUE\": \"aarti_ppe\", \"MONGO_COLL\": \"serviceConfiguration\", \"MONGO_DB\": \"ilens_ai\"}" os.environ["config"]="{\"TZ\": \"Asia/Kolkata\", \"MONGO_URI\": \"mongodb://admin:iLens%241234@192.168.3.181:2717/admin\", \"MONGO_DATABASE\": \"ilens_ai\", \"MONGO_COLLECTION\": \"janusDeployment\", \"MONGO_KEY\": \"deploymentId\", \"MONGO_VALUE\": \"aarti_ppe\", \"MONGO_COLL\": \"serviceConfiguration\", \"MONGO_DB\": \"ilens_ai\"}"
from edge_engine.edge_processor import ExecutePipeline from edge_engine.edge_processor import ExecutePipeline
from edge_engine.edge_processor import Pubs from edge_engine.edge_processor import Pubs
from scripts import Ppe from scripts import Ppe
......
...@@ -7,8 +7,6 @@ class FrameProcessor: ...@@ -7,8 +7,6 @@ class FrameProcessor:
self.model = model self.model = model
self.stream = stream self.stream = stream
logger.info("Setting up frame processor !!") logger.info("Setting up frame processor !!")
self.count = 0
self.skip_frame_every = 1 # 1 does not skip any frame (n-1 frames get skipped)
def run_model(self): def run_model(self):
while self.stream.stream.isOpened(): while self.stream.stream.isOpened():
...@@ -16,13 +14,9 @@ class FrameProcessor: ...@@ -16,13 +14,9 @@ class FrameProcessor:
logger.debug("Getting frame mask_model") logger.debug("Getting frame mask_model")
frame = self.stream.read() frame = self.stream.read()
logger.debug("Running mask_model") logger.debug("Running mask_model")
self.count += 1 data = {"frame": frame[0], "frameId":frame[1] , "deviceId": "{}".format(DEVICE_ID)}
if frame is not None and self.count % self.skip_frame_every == 0:
data = {"frame": frame, "frameId": "{}".format(self.count), "deviceId": "{}".format(DEVICE_ID)}
self.model.predict(data) self.model.predict(data)
logger.debug("publishing mask_model output") logger.debug("publishing mask_model output")
except Exception as e: except Exception as e:
logger.error(e) logger.error(e)
logger.error(traceback.format_exc()) logger.error(traceback.format_exc())
\ No newline at end of file
...@@ -3,6 +3,7 @@ from threading import Thread ...@@ -3,6 +3,7 @@ from threading import Thread
import sys import sys
import cv2 import cv2
import time import time
# import the Queue class from Python 3 # import the Queue class from Python 3
if sys.version_info >= (3, 0): if sys.version_info >= (3, 0):
from queue import Queue from queue import Queue
...@@ -13,16 +14,15 @@ else: ...@@ -13,16 +14,15 @@ else:
class FileVideoStream: class FileVideoStream:
def __init__(self,stream_config, transform=None): def __init__(self, stream_config, transform=None):
# initialize the file video stream along with the boolean # initialize the file video stream along with the boolean
# used to indicate if the thread should be stopped or not # used to indicate if the thread should be stopped or not
self.transform = transform self.transform = transform
self.stream_config =stream_config self.stream_config = stream_config
# initialize the queue used to store frames read from # initialize the queue used to store frames read from
# the video file # the video file
self.build_pipeline() self.build_pipeline()
def start(self): def start(self):
# start a thread to read frames from the file video stream # start a thread to read frames from the file video stream
self.thread.start() self.thread.start()
...@@ -35,19 +35,19 @@ class FileVideoStream: ...@@ -35,19 +35,19 @@ class FileVideoStream:
def build_pipeline(self): def build_pipeline(self):
self.build_cv_obj() self.build_cv_obj()
if "queueSize" not in self.stream_config: if "queueSize" not in self.stream_config:
self.stream_config["queueSize"] =128 self.stream_config["queueSize"] = 128
self.Q = Queue(maxsize=int(self.stream_config["queueSize"])) self.Q = Queue(maxsize=int(self.stream_config["queueSize"]))
# intialize thread # intialize thread
self.thread = Thread(target=self.update, args=()) self.thread = Thread(target=self.update, args=())
self.thread.daemon = True self.thread.daemon = True
def is_opened(self): def is_opened(self):
return self.stream.isOpened() return self.stream.isOpened()
def update(self): def update(self):
# keep looping infinitely # keep looping infinitely
count = 0
while True: while True:
# if the thread indicator variable is set, stop the # if the thread indicator variable is set, stop the
# thread # thread
...@@ -62,7 +62,8 @@ class FileVideoStream: ...@@ -62,7 +62,8 @@ class FileVideoStream:
# if the `grabbed` boolean is `False`, then we have # if the `grabbed` boolean is `False`, then we have
# reached the end of the video file # reached the end of the video file
if grabbed is False or frame is None: if grabbed is False or frame is None:
#self.stopped = True # self.stopped = True
count = 0
self.build_cv_obj() self.build_cv_obj()
continue continue
...@@ -82,7 +83,8 @@ class FileVideoStream: ...@@ -82,7 +83,8 @@ class FileVideoStream:
frame = self.transform(frame) frame = self.transform(frame)
# add the frame to the queue # add the frame to the queue
self.Q.put(frame) self.Q.put((frame, count))
count = count+1
else: else:
time.sleep(0.1) # Rest for 10ms, we have a full queue time.sleep(0.1) # Rest for 10ms, we have a full queue
......
This source diff could not be displayed because it is too large. You can view the blob instead.
numpy numpy==1.23.1
pymongo pymongo
opencv-python opencv-python
cachetools cachetools
...@@ -15,3 +15,4 @@ torch==1.9.0 ...@@ -15,3 +15,4 @@ torch==1.9.0
torchvision==0.10.0 torchvision==0.10.0
pandas pandas
seaborn seaborn
scikit-learn==0.22.2
\ No newline at end of file
import cv2
import base64
import numpy as np
from scipy.spatial import distance
from expiringdict import ExpiringDict
from edge_engine.common.logsetup import logger
from scripts.utils.infocenter import MongoLogger
from yolov5processor.infer import ExecuteInference
from scripts.utils.edge_utils import get_extra_fields
from edge_engine.ai.model.modelwraper import ModelWrapper
from scripts.utils.centroidtracker import CentroidTracker
from scripts.common.constants import JanusDeploymentConstants
from scripts.utils.image_utils import draw_circles_on_frame, resize_to_64_64
from scripts.utils.edge_utils import Utilities
from collections import deque
from scripts.utils.tracker import Tracker
from scripts.utils.helpers import box_iou2
from sklearn.utils.linear_assignment_ import linear_assignment
class CementBagCounter(ModelWrapper):
def __init__(self, config, model_config, pubs, device_id):
super().__init__()
"""
init function
"""
self.config = config["config"]
self.device_id = device_id
self.rtp = pubs.rtp_write
self.mongo_logger = MongoLogger()
self.frame_skip = self.config.get('frame_skip', False)
model = "data/ACC_v3.pt"
self.yp = ExecuteInference(weight=model,
gpu=model_config.get("gpu", False),
agnostic_nms=model_config.get("agnostic_nms", True),
iou=model_config.get("iou", 0.2),
confidence=model_config.get("confidence", 0.4))
self.print_eu_dist = model_config.get('print_eu_dist', 200)
self.ct1 = CentroidTracker(maxDisappeared=5)
self.ct2 = CentroidTracker(maxDisappeared=5)
self.frame_skipping = {
"skip_current_frame": True,
"detection_value": None
}
self.count = 0
self.cement_bag = 0
self.count_suraksha = 0
self.count_whitecem = 0
self.count_gold = 0
self.mrp_counter = 0
self.initial_object_position = Utilities.get_direction(self.device_id)
self.tracker_list = []
self.max_age = 15
self.min_hits = 10
self.track_id_list = deque([str(i) for i in range(1, 50)])
self.prev_annotation = []
self.initial_object_position = None
self.uncounted_objects = ExpiringDict(max_len=model_config.get("uncounted_obj_length", 50),
max_age_seconds=model_config.get("uncounted_obj_age", 60))
self.janus_metadata = ExpiringDict(max_age_seconds=120, max_len=1)
def _pre_process(self, x):
"""
Do preprocessing here, if any
:param x: payload
:return: payload
"""
return x
def _post_process(self, x):
"""
Apply post processing here, if any
:param x: payload
:return: payload
"""
self.rtp.publish(x) # video stream
return x
def send_payload(self, frame, label='CementBagDetected', bg_color="#474520", font_color="#FFFF00", alert_sound=None,
message="Cement Bag Detected!"):
"""
Insert event to Mongo
:param message:
:param frame:
:param label:
:param bg_color:
:param font_color:
:param alert_sound:
:return: None
"""
payload = {"deviceId": self.device_id, "message": message,
"frame": 'data:image/jpeg;base64,' + base64.b64encode(
cv2.imencode('.jpg', frame)[1].tostring()).decode("utf-8"), "activity": label,
"bg_color": bg_color, "font_color": font_color, "alert_sound": alert_sound}
self.mongo_logger.insert_attendance_event_to_mongo(payload)
def track_bags(self, tracker_obj, dets, im0, filter_name, centroid_color=(255, 0, 0)):
"""
Track the bags using Centroid based tracking
:param dets: prediction output
:param tracker_obj: prediction output
:param filter_name: prediction output
:param im0: raw frame
:param centroid_color: color given to the centroid marking
:return: centroid points, frame
"""
bags = list()
classes = list()
for det in dets:
if (det["class"] in filter_name):
bags.append(np.array(det['points']).astype("int"))
classes.append(det["class"])
objects = tracker_obj.update(bags)
objects.pop("frame", None)
if centroid_color is not False:
for (objectID, centroid) in objects.items():
if centroid['has_print']:
centroid_color = (0, 255, 0)
cv2.putText(im0, str(objectID), (centroid['centroid'][0] - 10, centroid['centroid'][1] - 10),
cv2.FONT_HERSHEY_SIMPLEX,
1, centroid_color, 2, cv2.LINE_AA)
cv2.circle(im0, (centroid['centroid'][0], centroid['centroid'][1]), 8, centroid_color, -1)
return objects, classes, im0
def kalman_tracker(
self,
bboxs,
img,
):
z_box = bboxs
x_box = []
if len(self.tracker_list) > 0:
for trk in self.tracker_list:
x_box.append(trk.box)
matched, unmatched_dets, unmatched_trks = self.assign_detections_to_trackers(x_box, z_box, iou_thrd=0.01)
# Deal with matched detections
if matched.size > 0:
for trk_idx, det_idx in matched:
z = z_box[det_idx]
z = np.expand_dims(z, axis=0).T
tmp_trk = self.tracker_list[trk_idx]
tmp_trk.kalman_filter(z)
xx = tmp_trk.x_state.T[0].tolist()
xx = [xx[0], xx[2], xx[4], xx[6]]
x_box[trk_idx] = xx
tmp_trk.box = xx
tmp_trk.hits += 1
# Deal with unmatched detections
if len(unmatched_dets) > 0:
for idx in unmatched_dets:
z = z_box[idx]
z = np.expand_dims(z, axis=0).T
tmp_trk = Tracker() # Create a new tracker
x = np.array([[z[0], 0, z[1], 0, z[2], 0, z[3], 0]]).T
tmp_trk.x_state = x
tmp_trk.predict_only()
xx = tmp_trk.x_state
xx = xx.T[0].tolist()
xx = [xx[0], xx[2], xx[4], xx[6]]
tmp_trk.box = xx
tmp_trk.id = self.track_id_list.popleft() # assign an ID for the tracker
self.tracker_list.append(tmp_trk)
x_box.append(xx)
# Deal with unmatched tracks
if len(unmatched_trks) > 0:
for trk_idx in unmatched_trks:
tmp_trk = self.tracker_list[trk_idx]
tmp_trk.no_losses += 1
tmp_trk.predict_only()
xx = tmp_trk.x_state
xx = xx.T[0].tolist()
xx = [xx[0], xx[2], xx[4], xx[6]]
tmp_trk.box = xx
x_box[trk_idx] = xx
# The list of tracks to be annotated
good_tracker_list = []
objects = []
boxs = []
for trk in self.tracker_list:
if (trk.hits >= self.min_hits) and (trk.no_losses <= self.max_age):
good_tracker_list.append(trk)
x_cv2 = trk.box
left, top, right, bottom = x_cv2[1], x_cv2[0], x_cv2[3], x_cv2[2]
centroid = [int(left + ((right - left) / 2)), bottom]
objects.append([int(trk.id), centroid])
boxs.append(x_cv2)
deleted_tracks = filter(lambda _x: _x.no_losses > self.max_age, self.tracker_list)
for trk in deleted_tracks:
self.track_id_list.append(trk.id)
self.tracker_list = [x for x in self.tracker_list if x.no_losses <= self.max_age]
print("object is ", str(objects))
return img, objects, boxs
@staticmethod
def assign_detections_to_trackers(
trackers,
detections,
iou_thrd=0.3,
):
"""
From current list of trackers and new detections, output matched detections,
un matched trackers, unmatched detections.
"""
iou_mat = np.zeros((len(trackers), len(detections)), dtype=np.float32)
for t, trk in enumerate(trackers):
for d, det in enumerate(detections):
iou_mat[t, d] = box_iou2(trk, det)
matched_idx = linear_assignment(-iou_mat)
unmatched_trackers, unmatched_detections = [], []
for t, trk in enumerate(trackers):
if t not in matched_idx[:, 0]:
unmatched_trackers.append(t)
for d, det in enumerate(detections):
if d not in matched_idx[:, 1]:
unmatched_detections.append(d)
matches = []
for m in matched_idx:
if iou_mat[m[0], m[1]] < iou_thrd:
unmatched_trackers.append(m[0])
unmatched_detections.append(m[1])
else:
matches.append(m.reshape(1, 2))
if len(matches) == 0:
matches = np.empty((0, 2), dtype=int)
else:
matches = np.concatenate(matches, axis=0)
return matches, np.array(unmatched_detections), np.array(unmatched_trackers)
def get_line_coordinates(self):
"""
Get the line coordinates from the deployment JSON
"""
if not self.janus_metadata.get('metadata'):
self.janus_metadata['metadata'] = get_extra_fields(self.device_id)
_coordinates = [self.janus_metadata['metadata'].get(coordinate_key) for coordinate_key in
JanusDeploymentConstants.LINE_COORDINATES]
_alignment = self.janus_metadata['metadata'].get(JanusDeploymentConstants.ALIGNMENT_KEY)
return _alignment, _coordinates
def line_point_position(self, point):
"""
Get the position of point w.r.t. the line
:param point: point to be compared
:return: boolean
"""
_alignment, line_coordinates = self.get_line_coordinates()
assert len(line_coordinates) == 4, "Line coordinates variable is invalid"
assert len(point) == 2, "Point variable is invalid"
_slope = (line_coordinates[3] - line_coordinates[1]) / (line_coordinates[2] - line_coordinates[0])
_point_equation_value = point[1] - line_coordinates[1] - _slope * (point[0] - line_coordinates[0])
if _point_equation_value > 0:
return True
else:
return False
def validate_point_position(self, point):
"""
Validate the position of the point w.r.t. the line
:param point: centroid
:return: bool
"""
_alignment, line_coordinates = self.get_line_coordinates()
assert _alignment in [JanusDeploymentConstants.VERTICAL, JanusDeploymentConstants.HORIZONTAL], \
"Invalid alignment variable"
if _alignment == JanusDeploymentConstants.VERTICAL:
line_y2 = line_coordinates[3]
line_y1 = line_coordinates[1]
if line_y1 < point[1] < line_y2 or line_y2 < point[1] < line_y1:
return True
else:
return False
else:
line_x2 = line_coordinates[2]
line_x1 = line_coordinates[0]
if line_x1 < point[0] < line_x2 or line_x2 < point[0] < line_x1:
return True
else:
return False
def update_bag_count(self, frame, detection_objects, classes):
"""
Maintains the bag counts
:param frame: image
:param detection_objects: detection object having object id and centroids
"""
for class_name, (objectID, centroid) in zip(classes, detection_objects.items()):
if self.validate_point_position(centroid['centroid']):
logger.debug("centroid detected")
if not isinstance(self.initial_object_position, bool):
logger.debug("Initializing the initial object position")
self.initial_object_position = self.line_point_position(point=centroid['centroid'])
Utilities.set_direction(self.device_id, self.initial_object_position)
#self.initial_object_position = True
logger.debug(self.initial_object_position)
_point_position = self.line_point_position(point=centroid['centroid'])
logger.debug("object ID is : ", str(objectID))
logger.debug(self.uncounted_objects)
# Check point in the same side as the initial object
if _point_position == self.initial_object_position:
logger.debug("same side only")
# Check the object is not already counted
if objectID not in self.uncounted_objects:
self.uncounted_objects[objectID] = centroid['centroid']
frame = draw_circles_on_frame(frame, centroid['centroid'], radius=10, color=(0, 0, 255),
thickness=-1)
elif objectID in self.uncounted_objects:
logger.debug("different side")
self.uncounted_objects.pop(objectID, None)
if (class_name == "acc_gold"):
self.count_gold += 1
logger.debug(self.count_gold)
elif (class_name == "acc_suraksha_plus"):
self.count_suraksha += 1
logger.debug(self.count_suraksha)
elif (class_name == "ambuja_whitecem"):
self.count_whitecem += 1
logger.debug(self.count_whitecem)
frame = draw_circles_on_frame(frame, centroid['centroid'], radius=10, color=(0, 255, 0),
thickness=-1)
if centroid['has_print']:
self.send_payload(resize_to_64_64(frame=frame), message='Print Detected!')
logger.info(f"Count: {self.count}, Print Found: True")
else:
self.send_payload(resize_to_64_64(frame=frame), message='Print Missing!')
logger.info(f"Count: {self.count}, Print Found: False")
else:
frame = draw_circles_on_frame(frame, centroid['centroid'], radius=10, color=(0, 255, 0),
thickness=-1)
count_text_gold = "ACC_GOLD: " + str(self.count_gold)
count_text_suraksha = "ACC_SURAKSHA_P_PLUS: " + str(self.count_suraksha)
count_text_whitecem = "PPC_WHITE: " + str(self.count_whitecem)
cv2.putText(frame, count_text_gold, (1300, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 3,
cv2.LINE_AA)
cv2.putText(frame, count_text_suraksha, (1300, 400), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 3,
cv2.LINE_AA)
cv2.putText(frame, count_text_whitecem, (1300, 600), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 3,
cv2.LINE_AA)
return frame
def draw_line_over_image(self, frame, color=(255, 255, 255)):
"""
Draws line over the counting line
:param frame: frame for
:param color:
:return:
"""
_alignment, line_coordinates = self.get_line_coordinates()
assert len(line_coordinates) == 4, "Line coordinates variable is invalid"
# return cv2.line(frame, (line_coordinates[0], line_coordinates[1]), (line_coordinates[2], line_coordinates[3]),
# color, 3)
self.drawline(frame, (line_coordinates[0], line_coordinates[1]), (line_coordinates[2],
line_coordinates[3]), color, thickness=3)
return frame
@staticmethod
def drawline(img, pt1, pt2, color, thickness=1, style='dotted', gap=20):
dist = ((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2) ** .5
pts = []
for i in np.arange(0, dist, gap):
r = i / dist
x = int((pt1[0] * (1 - r) + pt2[0] * r) + .5)
y = int((pt1[1] * (1 - r) + pt2[1] * r) + .5)
p = (x, y)
pts.append(p)
if style == 'dotted':
for p in pts:
cv2.circle(img, p, thickness, color, -1)
else:
s = pts[0]
e = pts[0]
i = 0
for p in pts:
s = e
e = p
if i % 2 == 1:
cv2.line(img, s, e, color, thickness)
i += 1
def distances(self, objs1, objs2):
for key1, val1 in objs1.items():
for key2, val2 in objs2.items():
dst = distance.euclidean(val1['centroid'], val2['centroid'])
if objs1[key1]['has_print']:
self.mrp_counter += 1
if(self.mrp_counter >= 5):
#STOP THE RELAY
pass
continue
elif dst < self.print_eu_dist:
objs1[key1]['has_print'] = True
self.mrp_counter = 0
# def inference(
# self,
# frame,
# classes,
#
# ):
# dets = self.yp.predict(frame)
# class_name = list()
# bboxs = []
#
# if dets:
# for i in dets:
# if(i["class"] in classes):
# class_name.append(i["class"])
# bboxs.append([i["points"][1], i["points"][0], i["points"][3], i["points"][2]])
#
# print("#######")
# print(bboxs)
# #frame = cv2.rectangle(frame, (bboxs[0][0], bboxs[0][1]), (bboxs[0][2], bboxs[0][3]),(255, 255, 0) , 2)
# return bboxs, frame, dets, class_name
def inference(
self,
frame,
):
dets = self.yp.predict(frame)
bboxs = []
if dets:
for i in dets:
bboxs.append([i["points"][1], i["points"][0], i["points"][3], i["points"][2]])
return bboxs, frame, dets
def _predict(self, obj):
class_list = ["acc_gold", "acc_suraksha_plus", "ambuja_buildcem"]
try:
frame = obj['frame']
if self.frame_skip:
if not self.frame_skipping["skip_current_frame"]:
dets = self.yp.predict(frame)
self.frame_skipping["detection_value"] = dets
self.frame_skipping["skip_current_frame"] = True
else:
dets = self.frame_skipping["detection_value"]
self.frame_skipping["skip_current_frame"] = False
else:
dets, frame, _dets = self.inference(frame)
print("PRINTING INFERENCE FUNCTION OUTPUT")
print(dets)
print(_dets)
#print(class_name)
#if dets:
frame, objects, boxs = self.kalman_tracker(dets, frame)
print("PRINTING KALMAN OUTPUTS")
print(objects)
print(boxs)
dets = self.yp.predict(frame)
frame = self.draw_line_over_image(frame)
# if [True for e in dets if e['class'] == 'cement_bag']:
#class_list = ["acc_gold", "acc_suraksha_plus", "ambuja_whitecem"]
mrp = ["mrp"]
objects,classes_cement, frame = self.track_bags(self.ct1, dets, frame, class_list)
_,classes, frame = self.track_bags(self.ct2, dets, frame, mrp)
frame = self.update_bag_count(frame=frame, detection_objects=objects, classes = classes_cement)
cv2.imshow("output is ", cv2.resize(frame, (900, 600)))
cv2.waitKey(1)
self.distances(objects, _)
logger.debug("self.uncounted_objects --> {}".format(self.uncounted_objects))
# for each in dets:
# color = (255, 255, 0)
# class_n = "Cement Bag"
#
# if each['class'] == 'label':
# color = (0, 255, 0)
# class_n = "Printing Detected!"
# cv2.rectangle(frame, (each['points'][0], each['points'][1]), (each['points'][2], each['points'][3]),
# color, 2)
# cv2.putText(frame, class_n, (each['points'][2], each['points'][1]), cv2.FONT_HERSHEY_SIMPLEX,
# 1, color, 2, cv2.LINE_AA)
obj['frame'] = cv2.resize(frame, (self.config.get('FRAME_WIDTH'), self.config.
get('FRAME_HEIGHT')))
except Exception as e:
logger.exception(f"Error: {e}", exc_info=True)
obj['frame'] = cv2.resize(obj['frame'], (self.config.get('FRAME_WIDTH'), self.config.get('FRAME_HEIGHT')))
return obj
import cv2
import base64
import numpy as np
from scipy.spatial import distance
from expiringdict import ExpiringDict
from edge_engine.common.logsetup import logger
from scripts.utils.infocenter import MongoLogger
from yolov5processor.infer import ExecuteInference
from scripts.utils.edge_utils import get_extra_fields
from edge_engine.ai.model.modelwraper import ModelWrapper
from scripts.utils.centroidtracker import CentroidTracker
from scripts.common.constants import JanusDeploymentConstants
from scripts.utils.image_utils import draw_circles_on_frame, resize_to_64_64
import cv2
import base64
import datetime
import numpy as np
from collections import deque
from expiringdict import ExpiringDict
from sklearn.utils.linear_assignment_ import linear_assignment
from edge_engine.common.logsetup import logger
from edge_engine.ai.model.modelwraper import ModelWrapper
from scripts.utils.tracker import Tracker
from scripts.utils.helpers import box_iou2
from scripts.utils.edge_utils import Utilities
from scripts.utils.infocenter import MongoLogger
from scripts.utils.model_tracker import ModelCountTracker
from scripts.common.constants import JanusDeploymentConstants
from yolov5processor.infer import ExecuteInference
class CementBagCounter(ModelWrapper):
def __init__(self, config, model_config, pubs, device_id):
super().__init__()
"""
init function
"""
self.config = config["config"]
self.device_id = device_id
self.rtp = pubs.rtp_write
self.mongo_logger = MongoLogger()
self.frame_skip = self.config.get('frame_skip', False)
model = "data/ACC_v3.pt"
self.yp = ExecuteInference(weight=model,
gpu=model_config.get("gpu", False),
agnostic_nms=model_config.get("agnostic_nms", True),
iou=model_config.get("iou", 0.2),
confidence=model_config.get("confidence", 0.4))
self.print_eu_dist = model_config.get('print_eu_dist', 200)
self.ct1 = CentroidTracker(maxDisappeared=5)
self.ct2 = CentroidTracker(maxDisappeared=5)
self.frame_skipping = {
"skip_current_frame": True,
"detection_value": None
}
self.count = 0
self.cement_bag = 0
self.count_suraksha = 0
self.count_whitecem = 0
self.count_gold = 0
self.tracker_list = []
self.max_age = 3
self.min_hits = 0
self.track_id_list = deque([str(i) for i in range(1, 50)])
self.prev_annotation = []
self.initial_object_position = None
self.uncounted_objects = ExpiringDict(max_len=model_config.get("uncounted_obj_length", 50),
max_age_seconds=model_config.get("uncounted_obj_age", 60))
self.janus_metadata = ExpiringDict(max_age_seconds=120, max_len=1)
def _pre_process(self, x):
"""
Do preprocessing here, if any
:param x: payload
:return: payload
"""
return x
def _post_process(self, x):
"""
Apply post processing here, if any
:param x: payload
:return: payload
"""
self.rtp.publish(x) # video stream
return x
def send_payload(self, frame, label='CementBagDetected', bg_color="#474520", font_color="#FFFF00", alert_sound=None,
message="Cement Bag Detected!"):
"""
Insert event to Mongo
:param message:
:param frame:
:param label:
:param bg_color:
:param font_color:
:param alert_sound:
:return: None
"""
payload = {"deviceId": self.device_id, "message": message,
"frame": 'data:image/jpeg;base64,' + base64.b64encode(
cv2.imencode('.jpg', frame)[1].tostring()).decode("utf-8"), "activity": label,
"bg_color": bg_color, "font_color": font_color, "alert_sound": alert_sound}
self.mongo_logger.insert_attendance_event_to_mongo(payload)
def kalman_tracker(
self,
bboxs,
img,
):
z_box = bboxs
x_box = []
if len(self.tracker_list) > 0:
for trk in self.tracker_list:
x_box.append(trk.box)
matched, unmatched_dets, unmatched_trks = self.assign_detections_to_trackers(x_box, z_box, iou_thrd=0.03)
# Deal with matched detections
if matched.size > 0:
for trk_idx, det_idx in matched:
z = z_box[det_idx]
z = np.expand_dims(z, axis=0).T
tmp_trk = self.tracker_list[trk_idx]
tmp_trk.kalman_filter(z)
xx = tmp_trk.x_state.T[0].tolist()
xx = [xx[0], xx[2], xx[4], xx[6]]
x_box[trk_idx] = xx
tmp_trk.box = xx
tmp_trk.hits += 1
# Deal with unmatched detections
if len(unmatched_dets) > 0:
for idx in unmatched_dets:
z = z_box[idx]
z = np.expand_dims(z, axis=0).T
tmp_trk = Tracker() # Create a new tracker
x = np.array([[z[0], 0, z[1], 0, z[2], 0, z[3], 0]]).T
tmp_trk.x_state = x
tmp_trk.predict_only()
xx = tmp_trk.x_state
xx = xx.T[0].tolist()
xx = [xx[0], xx[2], xx[4], xx[6]]
tmp_trk.box = xx
tmp_trk.id = self.track_id_list.popleft() # assign an ID for the tracker
self.tracker_list.append(tmp_trk)
x_box.append(xx)
# Deal with unmatched tracks
if len(unmatched_trks) > 0:
for trk_idx in unmatched_trks:
tmp_trk = self.tracker_list[trk_idx]
tmp_trk.no_losses += 1
tmp_trk.predict_only()
xx = tmp_trk.x_state
xx = xx.T[0].tolist()
xx = [xx[0], xx[2], xx[4], xx[6]]
tmp_trk.box = xx
x_box[trk_idx] = xx
# The list of tracks to be annotated
good_tracker_list = []
objects = []
boxs = []
for trk in self.tracker_list:
if (trk.hits >= self.min_hits) and (trk.no_losses <= self.max_age):
good_tracker_list.append(trk)
x_cv2 = trk.box
left, top, right, bottom = x_cv2[1], x_cv2[0], x_cv2[3], x_cv2[2]
centroid = [int(left + ((right - left) / 2)), bottom]
objects.append([int(trk.id), centroid])
boxs.append(x_cv2)
deleted_tracks = filter(lambda _x: _x.no_losses > self.max_age, self.tracker_list)
for trk in deleted_tracks:
self.track_id_list.append(trk.id)
self.tracker_list = [x for x in self.tracker_list if x.no_losses <= self.max_age]
# print("object is ", str(objects))
return img, objects, boxs
@staticmethod
def assign_detections_to_trackers(
trackers,
detections,
iou_thrd=0.3,
):
"""
From current list of trackers and new detections, output matched detections,
un matched trackers, unmatched detections.
"""
iou_mat = np.zeros((len(trackers), len(detections)), dtype=np.float32)
for t, trk in enumerate(trackers):
for d, det in enumerate(detections):
iou_mat[t, d] = box_iou2(trk, det)
matched_idx = linear_assignment(-iou_mat)
unmatched_trackers, unmatched_detections = [], []
for t, trk in enumerate(trackers):
if t not in matched_idx[:, 0]:
unmatched_trackers.append(t)
for d, det in enumerate(detections):
if d not in matched_idx[:, 1]:
unmatched_detections.append(d)
matches = []
for m in matched_idx:
if iou_mat[m[0], m[1]] < iou_thrd:
unmatched_trackers.append(m[0])
unmatched_detections.append(m[1])
else:
matches.append(m.reshape(1, 2))
if len(matches) == 0:
matches = np.empty((0, 2), dtype=int)
else:
matches = np.concatenate(matches, axis=0)
return matches, np.array(unmatched_detections), np.array(unmatched_trackers)
def get_line_coordinates(self):
"""
Get the line coordinates from the deployment JSON
"""
if not self.janus_metadata.get('metadata'):
self.janus_metadata['metadata'] = get_extra_fields(self.device_id)
_coordinates = [self.janus_metadata['metadata'].get(coordinate_key) for coordinate_key in
JanusDeploymentConstants.LINE_COORDINATES]
_alignment = self.janus_metadata['metadata'].get(JanusDeploymentConstants.ALIGNMENT_KEY)\
# _coordinates = [550, 200, 555, 1100]
#
# _alignment = "vertical"
return _alignment, _coordinates
def line_point_position(self, point):
"""
Get the position of point w.r.t. the line
:param point: point to be compared
:return: boolean
"""
_alignment, line_coordinates = self.get_line_coordinates()
assert len(line_coordinates) == 4, "Line coordinates variable is invalid"
assert len(point) == 2, "Point variable is invalid"
_slope = (line_coordinates[3] - line_coordinates[1]) / (line_coordinates[2] - line_coordinates[0])
_point_equation_value = point[1] - line_coordinates[1] - _slope * (point[0] - line_coordinates[0])
if _point_equation_value > 0:
return True
else:
return False
def validate_point_position(self, point):
"""
Validate the position of the point w.r.t. the line
:param point: centroid
:return: bool
"""
_alignment, line_coordinates = self.get_line_coordinates()
assert _alignment in [JanusDeploymentConstants.VERTICAL, JanusDeploymentConstants.HORIZONTAL], \
"Invalid alignment variable"
if _alignment == JanusDeploymentConstants.VERTICAL:
# _alignment, line_coordinates = self.get_line_coordinates()
# assert _alignment in ["horizontal", "vertical"], \
# "Invalid alignment variable"
# print(point)
# if _alignment == "vertical":
line_y2 = line_coordinates[3]
line_y1 = line_coordinates[1]
if line_y1 < point[1] < line_y2 or line_y2 < point[1] < line_y1:
return True
else:
return False
else:
line_x2 = line_coordinates[2]
line_x1 = line_coordinates[0]
if line_x1 < point[0] < line_x2 or line_x2 < point[0] < line_x1:
return True
else:
return False
def update_bag_count(self, frame, detection_objects, class_name, detections):
"""
Maintains the bag counts
:param frame: image
:param detection_objects: detection object having object id and centroids
"""
#for class_name, (objectID, centroid) in zip(classes, detection_objects):
for (object_id, det) in zip(detection_objects, detections):
centroid = object_id[1]
object_id = object_id[0]
logger.debug(detections)
#print(object_id)
frame = draw_circles_on_frame(frame, centroid, radius=10, color=(0, 0, 255),
thickness=-1)
if self.validate_point_position(centroid):
logger.debug("centroid detected")
#if self.validate_point_position(centroid):
# # if not isinstance(self.count, int):
# # logger.debug("Initializing the count variable")
# print("again entering")
# # Initializing the bag count
# if (class_name == "acc_gold"):
# if not isinstance(self.count_gold, int):
# logger.debug("Initializing the count variable")
# self.count_gold = 0
# elif (class_name == "acc_suraksha"):
# if not isinstance(self.count_suraksha, int):
# logger.debug("Initializing the count variable")
# self.count_suraksha = 0
# elif (class_name == "acc_buildcem"):
# if not isinstance(self.count_whitecem, int):
# logger.debug("Initializing the count variable")
# self.count_whitecem = 0
if not isinstance(self.initial_object_position, bool):
logger.debug("Initializing the initial object position")
#self.initial_object_position = self.line_point_position(point=centroid)
self.initial_object_position = True
logger.debug(self.initial_object_position)
_point_position = self.line_point_position(point=centroid)
#print("object ID is : ", str(objectID))
logger.debug(self.uncounted_objects)
# Check point in the same side as the initial object
if _point_position == self.initial_object_position:
logger.debug("same side only")
#print(class_name)
# Check the object is not already counted
if object_id not in self.uncounted_objects:
self.uncounted_objects[object_id] = centroid
elif object_id in self.uncounted_objects:
self.uncounted_objects.pop(object_id, None)
if ("acc_gold" in class_name):
self.count_gold += 1
mrp_result = self.distances(detections)
if mrp_result:
self.send_payload(resize_to_64_64(frame=frame),
message='ACC GOLD Bag Detected: Print Detected!')
logger.info(f"Count: {self.count_gold}, Print Found: True")
else:
self.send_payload(resize_to_64_64(frame=frame),
message='ACC GOLD Bag Detected: Print Missing!')
logger.info(f"Count: {self.count_gold}, Print Found: False")
elif ("acc_suraksha_plus" in class_name):
self.count_suraksha += 1
logger.debug(self.count_suraksha)
mrp_result = self.distances(detections)
if mrp_result:
self.send_payload(resize_to_64_64(frame=frame),
message='ACC SURAKSHA PLUS Bag Detected: Print Detected!')
logger.info(f"Count: {self.count_suraksha}, Print Found: True")
else:
self.send_payload(resize_to_64_64(frame=frame),
message='ACC SURAKSHA PLUS Bag Detected: Print Missing!')
logger.info(f"Count: {self.count_suraksha}, Print Found: False")
elif ("ambuja_whitecem" in class_name):
self.count_whitecem += 1
mrp_result = self.distances(detections)
if mrp_result:
self.send_payload(resize_to_64_64(frame=frame),
message='PPC White Bag Detected: Print Detected!')
logger.info(f"Count: {self.count_whitecem}, Print Found: True")
else:
self.send_payload(resize_to_64_64(frame=frame),
message='PPC White Bag Detected: Print Missing!')
logger.info(f"Count: {self.count_whitecem}, Print Found: False")
frame = draw_circles_on_frame(frame, centroid['centroid'], radius=10, color=(0, 255, 0),
thickness=-1)
if centroid['has_print']:
self.send_payload(resize_to_64_64(frame=frame), message='Print Detected!')
logger.info(f"Count: {self.count}, Print Found: True")
else:
self.send_payload(resize_to_64_64(frame=frame), message='Print Missing!')
logger.info(f"Count: {self.count}, Print Found: False")
else:
frame = draw_circles_on_frame(frame, centroid['centroid'], radius=10, color=(0, 255, 0),
thickness=-1)
# count_text_gold = "ACC_GOLD: " + str(self.count_gold)
# count_text_suraksha = "ACC_SURAKSHA_PLUS: " + str(self.count_suraksha)
# count_text_whitecem = "ACC_WHITE_CEM: " + str(self.count_whitecem)
# cv2.putText(frame, count_text_gold, (1300, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 3,
# cv2.LINE_AA)
# cv2.putText(frame, count_text_suraksha, (1300, 400), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 3,
# cv2.LINE_AA)
# cv2.putText(frame, count_text_whitecem, (1300, 600), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 3,
# cv2.LINE_AA)
return frame
def draw_line_over_image(self, frame, color=(255, 255, 255)):
"""
Draws line over the counting line
:param frame: frame for
:param color:
:return:
"""
_alignment, line_coordinates = self.get_line_coordinates()
assert len(line_coordinates) == 4, "Line coordinates variable is invalid"
# return cv2.line(frame, (line_coordinates[0], line_coordinates[1]), (line_coordinates[2], line_coordinates[3]),
# color, 3)
self.drawline(frame, (line_coordinates[0], line_coordinates[1]), (line_coordinates[2],
line_coordinates[3]), color, thickness=3)
return frame
@staticmethod
def drawline(img, pt1, pt2, color, thickness=1, style='dotted', gap=20):
dist = ((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2) ** .5
pts = []
for i in np.arange(0, dist, gap):
r = i / dist
x = int((pt1[0] * (1 - r) + pt2[0] * r) + .5)
y = int((pt1[1] * (1 - r) + pt2[1] * r) + .5)
p = (x, y)
pts.append(p)
if style == 'dotted':
for p in pts:
cv2.circle(img, p, thickness, color, -1)
else:
s = pts[0]
e = pts[0]
i = 0
for p in pts:
s = e
e = p
if i % 2 == 1:
cv2.line(img, s, e, color, thickness)
i += 1
def distances(self, detections):
mrp_cord = list()
cem_bag_cord = list()
for det in detections:
if(det["class"] == "mrp"):
mrp_cord.append(det["points"])
else:
cem_bag_cord.append(det["points"])
for c_cord in cem_bag_cord:
for m_cord in mrp_cord:
if (m_cord[0] > c_cord[0] and m_cord[0] < c_cord[2] and
m_cord[1] > c_cord[1] and m_cord[1] < c_cord[3]):
logger.debug("print is detected")
#cv2.waitKey(0)
return True
else:
return False
def inference(
self,
frame,
classes,
):
dets = self.yp.predict(frame)
class_name = list()
bboxs = []
if dets:
for i in dets:
if(i["class"] in classes):
class_name.append(i["class"])
#cv2.rectangle(frame, (i["points"][0], i["points"][1]), (i["points"][2], i["points"][3]), (255, 255, 0), 2)
bboxs.append([i["points"][1], i["points"][0], i["points"][3], i["points"][2]])
# frame = cv2.rectangle(frame, (bboxs[0][0], bboxs[0][1]), (bboxs[0][2], bboxs[0][3]),(255, 255, 0) , 2)
return bboxs, frame, dets, class_name
def _predict(self, obj):
class_list = ["acc_gold", "acc_suraksha_plus", "ambuja_whitecem"]
mrp = ["mrp"]
try:
frame = obj['frame']
if self.frame_skip:
if not self.frame_skipping["skip_current_frame"]:
dets = self.yp.predict(frame)
self.frame_skipping["detection_value"] = dets
self.frame_skipping["skip_current_frame"] = True
else:
dets = self.frame_skipping["detection_value"]
self.frame_skipping["skip_current_frame"] = False
else:
dets, frame, _dets, class_name = self.inference(frame, class_list)
#dets_mrp, frame_mrp, _dets_mrp, class_name_mrp = self.inference(frame, mrp)
frame = self.draw_line_over_image(frame)
# if [True for e in dets if e['class'] == 'cement_bag']:
#if dets:
frame, objects, boxs = self.kalman_tracker(dets, frame)
print("PRINTING KALMAN OUTPUT")
print(objects)
print(boxs)
for box in boxs:
cv2.rectangle(frame, (box[1], box[0]), (box[3], box[2]), (255, 0, 0), 2)
#objects,classes_cement, frame = self.track_bags(self.ct1, dets, frame, class_list)
#_,classes, frame = self.track_bags(self.ct2, _dets, frame, mrp)
#frame, _, box_mrp = self.kalman_tracker(dets_mrp, frame)
frame = self.update_bag_count(frame=frame, detection_objects=objects, class_name = class_name, detections = _dets)
# cv2.imshow("output is ", cv2.resize(frame, (900, 600)))
# cv2.waitKey(1)
# print("******")
# print(objects)
# print(_)
# self.distances(objects, _)
logger.debug("self.uncounted_objects --> {}".format(self.uncounted_objects))
# for each in dets:
# color = (255, 255, 0)
# class_n = "Cement Bag"
#
# if each['class'] == 'label':
# color = (0, 255, 0)
# class_n = "Printing Detected!"
# cv2.rectangle(frame, (each['points'][0], each['points'][1]), (each['points'][2], each['points'][3]),
# color, 2)
# cv2.putText(frame, class_n, (each['points'][2], each['points'][1]), cv2.FONT_HERSHEY_SIMPLEX,
# 1, color, 2, cv2.LINE_AA)
obj['frame'] = cv2.resize(frame, (self.config.get('FRAME_WIDTH'), self.config.
get('FRAME_HEIGHT')))
except Exception as e:
logger.exception(f"Error: {e}", exc_info=True)
obj['frame'] = cv2.resize(obj['frame'], (self.config.get('FRAME_WIDTH'), self.config.get('FRAME_HEIGHT')))
return obj
import os import os
import uuid import uuid
from scipy.spatial import distance
# from edge_engine.common.logsetup import logger
# from yolov5processor.infer import ExecuteInference
from scripts.utils.edge_utils import get_extra_fields from scripts.utils.edge_utils import get_extra_fields
# from edge_engine.ai.model.modelwraper import ModelWrapper
from scripts.utils.centroidtracker import CentroidTracker from scripts.utils.centroidtracker import CentroidTracker
from scripts.common.constants import JanusDeploymentConstants
from scripts.utils.image_utils import draw_circles_on_frame, resize_to_64_64 from scripts.utils.image_utils import draw_circles_on_frame, resize_to_64_64
import cv2 import cv2
import base64 import base64
import datetime
import numpy as np import numpy as np
from collections import deque from collections import deque
...@@ -25,19 +18,10 @@ from edge_engine.ai.model.modelwraper import ModelWrapper ...@@ -25,19 +18,10 @@ from edge_engine.ai.model.modelwraper import ModelWrapper
from scripts.utils.tracker import Tracker from scripts.utils.tracker import Tracker
from scripts.utils.helpers import box_iou2 from scripts.utils.helpers import box_iou2
from scripts.utils.edge_utils import Utilities
from scripts.utils.infocenter import MongoLogger from scripts.utils.infocenter import MongoLogger
from scripts.utils.model_tracker import ModelCountTracker
from scripts.common.constants import JanusDeploymentConstants from scripts.common.constants import JanusDeploymentConstants
# from yolov5processor.infer import ExecuteInference
from yolov5.detect import run
from yolov5.detect import load_model
import random
import psycopg2
import time import time
import requests import json
...@@ -48,18 +32,17 @@ class Ppe(ModelWrapper): ...@@ -48,18 +32,17 @@ class Ppe(ModelWrapper):
""" """
init function init function
""" """
self.type = config['inputConf']['sourceType']
if config['inputConf']['sourceType'] == 'videofile':
f = open('aarti.json', "r")
self.dets = json.loads(f.read())
f.close()
self.config = config["config"] self.config = config["config"]
self.device_id = device_id self.device_id = device_id
self.rtp = pubs.rtp_write self.rtp = pubs.rtp_write
self.mongo_logger = MongoLogger() self.mongo_logger = MongoLogger()
self.frame_skip = self.config.get('frame_skip', False) self.frame_skip = self.config.get('frame_skip', False)
model = "data/aarti_v3.pt" model = "data/aarti_v3.pt"
self.yolo_model = load_model(model)
# self.yp = ExecuteInference(weight=model,
# gpu=model_config.get("gpu", False),
# agnostic_nms=model_config.get("agnostic_nms", True),
# iou=model_config.get("iou", 0.2),
# confidence=model_config.get("confidence", 0.4))
self.print_eu_dist = model_config.get('print_eu_dist', 200) self.print_eu_dist = model_config.get('print_eu_dist', 200)
self.ct1 = CentroidTracker(maxDisappeared=5) self.ct1 = CentroidTracker(maxDisappeared=5)
self.ct2 = CentroidTracker(maxDisappeared=5) self.ct2 = CentroidTracker(maxDisappeared=5)
...@@ -81,10 +64,6 @@ class Ppe(ModelWrapper): ...@@ -81,10 +64,6 @@ class Ppe(ModelWrapper):
max_age_seconds=model_config.get("uncounted_obj_age", 60)) max_age_seconds=model_config.get("uncounted_obj_age", 60))
self.janus_metadata = ExpiringDict(max_age_seconds=120, max_len=1) self.janus_metadata = ExpiringDict(max_age_seconds=120, max_len=1)
self.safety_equip = ExpiringDict(max_age_seconds=30, max_len=10) self.safety_equip = ExpiringDict(max_age_seconds=30, max_len=10)
# self.polygon = np.array([[[7, 753],[774, 574], [1688, 672], [1473, 977], [39, 962]]])
# self.polygon = np.array([[[300, 753], [774, 674], [1900, 672], [1900, 1000], [39, 1000]]])
# self.polygon = np.array([[[300, 753], [774, 674], [1900, 672], [1900, 900], [100, 900]]])
#self.polygon = np.array([[[100, 753], [300, 750], [1900, 750], [1900, 900], [50, 900]]])
self.polygon = np.array([[[400, 700], [1600, 500], [1900, 650], [1900, 1000], [700, 1000]]]) self.polygon = np.array([[[400, 700], [1600, 500], [1900, 650], [1900, 1000], [700, 1000]]])
self.tracking_people = {} self.tracking_people = {}
self.final_ppe_result = {} self.final_ppe_result = {}
...@@ -94,13 +73,9 @@ class Ppe(ModelWrapper): ...@@ -94,13 +73,9 @@ class Ppe(ModelWrapper):
self.reported_violation_ids = {} self.reported_violation_ids = {}
self.violation_count = {"Air Breathing Mask": [] , "Safety helmet": [], "Hand gloves": [], "coverall suit": []} self.violation_count = {"Air Breathing Mask": [] , "Safety helmet": [], "Hand gloves": [], "coverall suit": []}
#self.uri = "postgres://postgres:postgres@192.168.3.181:5432/postgres?sslmode=disable"
self.uri = "postgres://vision_user:Vision#4321@192.168.0.237:30009/ilens_vision_aarti?sslmode=disable"
self.payload_classes = {"Air Breathing Mask": "air_breathing_mask_violation", "Safety helmet": "helmet_violation", "Hand gloves": "glove_violation", "coverall suit": "coverall_suit_violation"} self.payload_classes = {"Air Breathing Mask": "air_breathing_mask_violation", "Safety helmet": "helmet_violation", "Hand gloves": "glove_violation", "coverall suit": "coverall_suit_violation"}
# connect to the PostgreSQL server
# print('Connecting to the PostgreSQL database...')
self.conn = psycopg2.connect(self.uri)
self.active_rec = {} self.active_rec = {}
...@@ -238,7 +213,8 @@ class Ppe(ModelWrapper): ...@@ -238,7 +213,8 @@ class Ppe(ModelWrapper):
iou_mat[t, d] = box_iou2(trk, det) iou_mat[t, d] = box_iou2(trk, det)
matched_idx = linear_assignment(-iou_mat) matched_idx = linear_assignment(-iou_mat)
# row, col = matched_idx
# matched_idx = np.concatenate((row.reshape(-1, 1), col.reshape(-1, 1)), axis=1)
unmatched_trackers, unmatched_detections = [], [] unmatched_trackers, unmatched_detections = [], []
for t, trk in enumerate(trackers): for t, trk in enumerate(trackers):
if t not in matched_idx[:, 0]: if t not in matched_idx[:, 0]:
...@@ -325,11 +301,6 @@ class Ppe(ModelWrapper): ...@@ -325,11 +301,6 @@ class Ppe(ModelWrapper):
assert _alignment in [JanusDeploymentConstants.VERTICAL, JanusDeploymentConstants.HORIZONTAL], \ assert _alignment in [JanusDeploymentConstants.VERTICAL, JanusDeploymentConstants.HORIZONTAL], \
"Invalid alignment variable" "Invalid alignment variable"
if _alignment == JanusDeploymentConstants.VERTICAL: if _alignment == JanusDeploymentConstants.VERTICAL:
# _alignment, line_coordinates = self.get_line_coordinates()
# assert _alignment in ["horizontal", "vertical"], \
# "Invalid alignment variable"
# print(point)
# if _alignment == "vertical":
line_y2 = line_coordinates[3] line_y2 = line_coordinates[3]
line_y1 = line_coordinates[1] line_y1 = line_coordinates[1]
if line_y1 < point[1] < line_y2 or line_y2 < point[1] < line_y1: if line_y1 < point[1] < line_y2 or line_y2 < point[1] < line_y1:
...@@ -345,27 +316,6 @@ class Ppe(ModelWrapper): ...@@ -345,27 +316,6 @@ class Ppe(ModelWrapper):
else: else:
return False return False
def video_write(self, frame, unique_id):
frame_width = int(frame.shape[1])
frame_height = int(frame.shape[0])
size = (frame_width, frame_height)
working_dir = "output"
file_name =str(unique_id) + ".mp4"
file_name = os.path.join(working_dir, file_name)
# Below VideoWriter object will create
# a frame of above defined The output
# is stored in 'filename.avi' file.
result = cv2.VideoWriter(file_name,
cv2.VideoWriter_fourcc(*'avc1'),
10, size)
# fourcc = cv2.VideoWriter_fourcc(*'h264')
# result = cv2.VideoWriter(file_name,
# fourcc,
# 10, size)
# fourcc = cv2.VideoWriter_fourcc(*'vp80')
# result = cv2.VideoWriter(file_name, fourcc, 20, (640, 480))
return result
def ppe_detection(self, frame, bbox, detection_objects, class_name, other_class_name, other_centroid): def ppe_detection(self, frame, bbox, detection_objects, class_name, other_class_name, other_centroid):
""" """
...@@ -388,29 +338,11 @@ class Ppe(ModelWrapper): ...@@ -388,29 +338,11 @@ class Ppe(ModelWrapper):
centroid = object_id[1] centroid = object_id[1]
# print(object_id) # print(object_id)
object_id = object_id[0] object_id = object_id[0]
# logger.debug(detections)
# print(object_id)
# print("person bb")
# print(person_bb)
# cv2.putText(frame, str(object_id), (centroid[1], centroid[0]), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0),
# 2, cv2.LINE_AA)
# cv2.putText(frame, class_detected, (centroid[1], centroid[0]), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0),
# 2, cv2.LINE_AA)
# frame = draw_circles_on_frame(
# frame, centroid, radius=10, color=(0, 0, 255), thickness=-1
# )
cv2.circle(frame, (centroid[1], centroid[0]), 2, (0, 255, 0), thickness=1, lineType=8, shift=0) cv2.circle(frame, (centroid[1], centroid[0]), 2, (0, 255, 0), thickness=1, lineType=8, shift=0)
# for person_bb in bbox: # for person_bb in bbox:
person_safety_status = set() person_safety_status = set()
# print("other classes are")
# print(other_class_name)
# print(other_centroid)
temp_list = set()
for (safety_object, object_bb) in zip(other_class_name, other_centroid): for (safety_object, object_bb) in zip(other_class_name, other_centroid):
# print("inside check loop")
violations = set()
if (person_bb[0] < object_bb[0] and person_bb[2] > object_bb[0]): if (person_bb[0] < object_bb[0] and person_bb[2] > object_bb[0]):
if(person_bb[1] < object_bb[1] and person_bb[3] > object_bb[1]): if(person_bb[1] < object_bb[1] and person_bb[3] > object_bb[1]):
person_safety_status.add(safety_object) person_safety_status.add(safety_object)
...@@ -430,10 +362,7 @@ class Ppe(ModelWrapper): ...@@ -430,10 +362,7 @@ class Ppe(ModelWrapper):
temp_list.add(safety_object) temp_list.add(safety_object)
# print("object id not present") # print("object id not present")
self.safety_equip["object_id"] = temp_list self.safety_equip["object_id"] = temp_list
# print("first li")
# print(self.safety_equip["object_id"])
# print(needed_objects)
# print(bbox)
if("Air Breathing Mask" in self.safety_equip["object_id"] and "Safety helmet" in self.safety_equip["object_id"]): if("Air Breathing Mask" in self.safety_equip["object_id"] and "Safety helmet" in self.safety_equip["object_id"]):
# print("with both helmet and air breathing mask if") # print("with both helmet and air breathing mask if")
...@@ -450,24 +379,10 @@ class Ppe(ModelWrapper): ...@@ -450,24 +379,10 @@ class Ppe(ModelWrapper):
print(temp_v_list) print(temp_v_list)
# cv2.waitKey(1) # cv2.waitKey(1)
for v in temp_v_list: for v in temp_v_list:
# try:
# sql = 'INSERT INTO "historical_video_2" ("event_id", "camera_id", "frame_id", "timestamp", "video_url", "violation_type") VALUES (%s, %s, %s, %s, %s, %s);'
# # for violated_items in violations:
# # present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table'
# cur = self.conn.cursor()
# frame_count = self.frame_id
# file_path = "/home/administrator/aarti_violation_videos/" + str(unique_id) + ".webm"
# cur.execute(sql, [frame_count, 10, str(unique_id), datetime.datetime.now(), file_path, v])
# self.conn.commit()
# cur.close()
# except:
# self.conn.rollback()
self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list, self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list,
event=v, frame_id=rec_inf[2]) event=v, frame_id=rec_inf[2])
with open("output/recorded_video_list.txt", "a") as f:
f.write(rec_inf[2] + ".webm")
f.write("\n")
del self.active_rec[object_id] del self.active_rec[object_id]
...@@ -482,65 +397,16 @@ class Ppe(ModelWrapper): ...@@ -482,65 +397,16 @@ class Ppe(ModelWrapper):
# violated_items = ', '.join(list(map(str, temp_violation_list))) # violated_items = ', '.join(list(map(str, temp_violation_list)))
temp_violation_list.sort() temp_violation_list.sort()
violated_items_2 = ', '.join(list(map(str, violations))) violated_items_2 = ', '.join(list(map(str, violations)))
# print("violated items")
# print(violated_items)
# violated_items = violated_items
# print(violated_items)
# if (object_id in self.active_rec):
# rec_inf = self.active_rec[object_id]
# rec_inf[1].write(frame)
for elem in violations: for elem in violations:
self.violation_count[elem].append(elem) self.violation_count[elem].append(elem)
if (object_id not in self.reported_violation_ids): if (object_id not in self.reported_violation_ids):
print("sending to mongo") print("sending to mongo")
unique_id = str(uuid.uuid4()) + str(time.time())
# result = self.video_write(frame, unique_id)
requests.post('http://192.168.3.181:2328/items/', json={
"rtsp": "rtsp://admin:Admin@123@117.222.36.90:10556",
"name": str(unique_id)
})
print("unique id is:: ", str(unique_id))
self.active_rec[object_id] = [time.time(), "result", unique_id, temp_violation_list]
# result.write(frame)
# self.send_payload(frame=resize_to_64_64(frame), message=temp_violation_list, event = violated_items,frame_id=self.frame_id)
self.send_payload(frame=resize_to_64_64(frame), message=temp_violation_list, event = violated_items,frame_id=self.frame_id)
#
self.reported_violation_ids[object_id] = time.time() self.reported_violation_ids[object_id] = time.time()
for v in temp_violation_list:
# self.send_payload(frame=resize_to_64_64(frame), message=temp_violation_list,
# event=v, frame_id=self.frame_id)
sql = 'INSERT INTO "aarti_violation_event_table" ("camera_id", "frame_id", "timestamp", "violation_type") VALUES (%s, %s, %s, %s);'
# for violated_items in violations:
# present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table'
cur = self.conn.cursor()
# cur.execute("ROLLBACK")
frame_count = self.frame_id
cur.execute(sql, [1, frame_count, datetime.datetime.now(), v])
self.conn.commit()
cur.close()
try:
sql = 'INSERT INTO "historical_video_2" ("event_id", "camera_id", "frame_id", "timestamp", "video_url", "violation_type") VALUES (%s, %s, %s, %s, %s, %s);'
# for violated_items in violations:
# present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table'
cur = self.conn.cursor()
frame_count = self.frame_id
file_path = "/home/administrator/aarti_violation_videos/" + str(unique_id) + ".webm"
cur.execute(sql, [frame_count, 1, str(unique_id), datetime.datetime.now(), file_path, v])
self.conn.commit()
cur.close()
except:
self.conn.rollback()
else: else:
time_diff = time.time() - self.reported_violation_ids[object_id] time_diff = time.time() - self.reported_violation_ids[object_id]
...@@ -554,26 +420,10 @@ class Ppe(ModelWrapper): ...@@ -554,26 +420,10 @@ class Ppe(ModelWrapper):
print(temp_v_list) print(temp_v_list)
cv2.waitKey(1) cv2.waitKey(1)
for v in temp_v_list: for v in temp_v_list:
# # try:
#
# sql = 'INSERT INTO "historical_video_2" ("event_id", "camera_id", "frame_id", "timestamp", "video_url", "violation_type") VALUES (%s, %s, %s, %s, %s, %s);'
# # for violated_items in violations:
# # present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table'
# cur = self.conn.cursor()
# frame_count = self.frame_id
# file_path = "/home/administrator/aarti_violation_videos/" + str(unique_id) + ".webm"
# cur.execute(sql,
# [frame_count, 10, str(unique_id), datetime.datetime.now(), file_path,
# v])
# self.conn.commit()
# cur.close()
# except:
# self.conn.rollback()
self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list, self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list,
event=v, frame_id=rec_inf[2]) event=v, frame_id=rec_inf[2])
with open("output/recorded_video_list.txt", "a") as f:
f.write(rec_inf[2] + ".webm")
f.write("\n")
del self.active_rec[object_id] del self.active_rec[object_id]
...@@ -596,26 +446,12 @@ class Ppe(ModelWrapper): ...@@ -596,26 +446,12 @@ class Ppe(ModelWrapper):
print(temp_v_list) print(temp_v_list)
# cv2.waitKey(1) # cv2.waitKey(1)
for v in temp_v_list: for v in temp_v_list:
# # try:
#
# sql = 'INSERT INTO "historical_video_2" ("event_id", "camera_id", "frame_id", "timestamp", "video_url", "violation_type") VALUES (%s, %s, %s, %s, %s, %s);'
# # for violated_items in violations:
# # present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table'
# cur = self.conn.cursor()
# # cur.execute("ROLLBACK")
# frame_count = self.frame_id
# file_path = "/home/administrator/aarti_violation_videos/" + str(unique_id) + ".webm"
# cur.execute(sql, [frame_count, 10, str(unique_id), datetime.datetime.now(), file_path, v])
# self.conn.commit()
# cur.close()
# # except:
# self.conn.rollback()
self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list, self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list,
event=v, frame_id=rec_inf[2]) event=v, frame_id=rec_inf[2])
with open("output/recorded_video_list.txt", "a") as f: # with open("output/recorded_video_list.txt", "a") as f:
f.write(rec_inf[2] + ".webm") # f.write(rec_inf[2] + ".webm")
f.write("\n") # f.write("\n")
del self.active_rec[object_id] del self.active_rec[object_id]
...@@ -623,13 +459,6 @@ class Ppe(ModelWrapper): ...@@ -623,13 +459,6 @@ class Ppe(ModelWrapper):
else: else:
violations = needed_objects_with_mask.difference(self.safety_equip["object_id"]) violations = needed_objects_with_mask.difference(self.safety_equip["object_id"])
# print("violations")
# print(violations)
# if (object_id in self.active_rec):
# rec_inf = self.active_rec[object_id]
# rec_inf[1].write(frame)
for elem in violations: for elem in violations:
self.violation_count[elem].append(elem) self.violation_count[elem].append(elem)
...@@ -647,47 +476,9 @@ class Ppe(ModelWrapper): ...@@ -647,47 +476,9 @@ class Ppe(ModelWrapper):
print(violated_items) print(violated_items)
if (object_id not in self.reported_violation_ids): if (object_id not in self.reported_violation_ids):
print("sending to mongo") print("sending to mongo")
# time.sleep(5)
# unique_id = uuid.uuid4()
unique_id = str(uuid.uuid4()) + str(time.time())
# with open("recorded_video_list.txt", "a") as f:
# f.write(unique_id + ".webm")
# f.write("\n")
# result = self.video_write(frame, unique_id)
requests.post('http://192.168.3.181:2328/items/', json={
"rtsp": "rtsp://admin:Admin@123@117.222.36.90:10556",
"name": str(unique_id)
})
print("unique id is:: ", str(unique_id))
self.active_rec[object_id] = [time.time(), "result", unique_id, temp_violation_list]
# result.write(frame)
# self.send_payload(frame=resize_to_64_64(frame), message=temp_violation_list , event = violated_items, frame_id=self.frame_id)
self.reported_violation_ids[object_id] = time.time()
for v in temp_violation_list:
# self.send_payload(frame=resize_to_64_64(frame), message=temp_violation_list,
# event=v, frame_id=self.frame_id)
sql = 'INSERT INTO "aarti_violation_event_table" ("camera_id", "frame_id", "timestamp", "violation_type") VALUES (%s, %s, %s, %s);'
# for violated_items in violations:
# present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table'
cur = self.conn.cursor()
frame_count = self.frame_id
cur.execute(sql, [1, frame_count, datetime.datetime.now(), v])
self.conn.commit()
cur.close()
try:
sql = 'INSERT INTO "historical_video_2" ("event_id", "camera_id", "frame_id", "timestamp", "video_url", "violation_type") VALUES (%s, %s, %s, %s, %s, %s);' self.send_payload(frame=resize_to_64_64(frame), message=temp_violation_list , event = violated_items, frame_id=self.frame_id)
# for violated_items in violations: self.reported_violation_ids[object_id] = time.time()
# present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table'
cur = self.conn.cursor()
# cur.execute("ROLLBACK")
frame_count = self.frame_id
file_path = "/home/administrator/aarti_violation_videos/" + str(unique_id) + ".webm"
cur.execute(sql, [frame_count, 1, str(unique_id), datetime.datetime.now(), file_path, v])
self.conn.commit()
cur.close()
except:
self.conn.rollback()
else: else:
time_diff = time.time() - self.reported_violation_ids[object_id] time_diff = time.time() - self.reported_violation_ids[object_id]
...@@ -701,27 +492,10 @@ class Ppe(ModelWrapper): ...@@ -701,27 +492,10 @@ class Ppe(ModelWrapper):
print(temp_v_list) print(temp_v_list)
# cv2.waitKey(1) # cv2.waitKey(1)
for v in temp_v_list: for v in temp_v_list:
# try:
# sql = 'INSERT INTO "historical_video_2" ("event_id", "camera_id", "frame_id", "timestamp", "video_url", "violation_type") VALUES (%s, %s, %s, %s, %s, %s);'
# # for violated_items in violations:
# # present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table'
# cur = self.conn.cursor()
# # cur.execute("ROLLBACK")
# frame_count = self.frame_id
# file_path = "/home/administrator/aarti_violation_videos/" + str(unique_id) + ".webm"
# cur.execute(sql,
# [frame_count, 10, str(unique_id), datetime.datetime.now(), file_path,
# v])
# self.conn.commit()
# cur.close()
# except:
# self.conn.rollback()
self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list, self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list,
event=v, frame_id=rec_inf[2]) event=v, frame_id=rec_inf[2])
with open("output/recorded_video_list.txt", "a") as f:
f.write(rec_inf[2] + ".webm")
f.write("\n")
del self.active_rec[object_id] del self.active_rec[object_id]
person_without_safety_kit += 1 person_without_safety_kit += 1
...@@ -742,26 +516,10 @@ class Ppe(ModelWrapper): ...@@ -742,26 +516,10 @@ class Ppe(ModelWrapper):
print(temp_v_list) print(temp_v_list)
cv2.waitKey(1) cv2.waitKey(1)
for v in temp_v_list: for v in temp_v_list:
# # try:
#
# sql = 'INSERT INTO "historical_video_2" ("event_id", "camera_id", "frame_id", "timestamp", "video_url", "violation_type") VALUES (%s, %s, %s, %s, %s, %s);'
# # for violated_items in violations:
# # present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table'
# cur = self.conn.cursor()
# # cur.execute("ROLLBACK")
# frame_count = self.frame_id
# file_path = "/home/administrator/aarti_violation_videos/" + str(unique_id) + ".webm"
# cur.execute(sql, [frame_count, 10, str(unique_id), datetime.datetime.now(), file_path, v])
# self.conn.commit()
# cur.close()
# except:
# self.conn.rollback()
self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list, self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list,
event=v, frame_id=rec_inf[2]) event=v, frame_id=rec_inf[2])
with open("output/recorded_video_list.txt", "a") as f:
f.write(rec_inf[2] + ".webm")
f.write("\n")
del self.active_rec[object_id] del self.active_rec[object_id]
...@@ -780,11 +538,6 @@ class Ppe(ModelWrapper): ...@@ -780,11 +538,6 @@ class Ppe(ModelWrapper):
print("violated items") print("violated items")
print(violated_items) print(violated_items)
violated_items = violated_items violated_items = violated_items
# violated_items_2 = violated_items_2 + ", Air Breathing Mask"
# print(violated_items)
# if (object_id in self.active_rec):
# rec_inf = self.active_rec[object_id]
# rec_inf[1].write(frame)
for elem in violations: for elem in violations:
...@@ -792,49 +545,9 @@ class Ppe(ModelWrapper): ...@@ -792,49 +545,9 @@ class Ppe(ModelWrapper):
if (object_id not in self.reported_violation_ids): if (object_id not in self.reported_violation_ids):
print("sending to mongo") print("sending to mongo")
# unique_id = uuid.uuid4()
unique_id = str(uuid.uuid4()) + str(time.time())
# with open("recorded_video_list.txt", "a") as f:
# f.write(unique_id + ".webm")
# f.write("\n")
# result = self.video_write(frame, unique_id)
requests.post('http://192.168.3.181:2328/items/', json={
"rtsp": "rtsp://admin:Admin@123@117.222.36.90:10556",
"name": str(unique_id)
})
print("unique id is:: ", str(unique_id))
self.active_rec[object_id] = [time.time(), "result", unique_id, temp_violation_list]
# result.write(frame)
# self.send_payload(frame=resize_to_64_64(frame), message=temp_violation_list, event = violated_items, frame_id=self.frame_id)
self.reported_violation_ids[object_id] = time.time()
for v in temp_violation_list:
# self.send_payload(frame=resize_to_64_64(frame), message=temp_violation_list,
# event=v, frame_id=self.frame_id)
sql = 'INSERT INTO "aarti_violation_event_table" ("camera_id", "frame_id", "timestamp", "violation_type") VALUES (%s, %s, %s, %s);'
# for violated_items in violations:
# present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table'
cur = self.conn.cursor()
frame_count = self.frame_id
cur.execute(sql, [1, frame_count, datetime.datetime.now(), v])
self.conn.commit()
cur.close()
try:
sql = 'INSERT INTO "historical_video_2" ("event_id", "camera_id", "frame_id", "timestamp", "video_url", "violation_type") VALUES (%s, %s, %s, %s, %s, %s);'
# for violated_items in violations:
# present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table'
cur = self.conn.cursor()
# cur.execute("ROLLBACK")
frame_count = self.frame_id
file_path = "/home/administrator/aarti_violation_videos/" + str(unique_id) + ".webm"
cur.execute(sql, [frame_count, 1, str(unique_id), datetime.datetime.now(), file_path, v])
self.conn.commit()
cur.close()
except:
self.conn.rollback()
self.send_payload(frame=resize_to_64_64(frame), message=temp_violation_list, event = violated_items, frame_id=self.frame_id)
self.reported_violation_ids[object_id] = time.time()
else: else:
time_diff = time.time() - self.reported_violation_ids[object_id] time_diff = time.time() - self.reported_violation_ids[object_id]
...@@ -849,85 +562,12 @@ class Ppe(ModelWrapper): ...@@ -849,85 +562,12 @@ class Ppe(ModelWrapper):
print(temp_v_list) print(temp_v_list)
cv2.waitKey(1) cv2.waitKey(1)
for v in temp_v_list: for v in temp_v_list:
# try:
# sql = 'INSERT INTO "historical_video_2" ("event_id", "camera_id", "frame_id", "timestamp", "video_url", "violation_type") VALUES (%s, %s, %s, %s, %s, %s);'
# # for violated_items in violations:
# # present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table'
# cur = self.conn.cursor()
# # cur.execute("ROLLBACK")
# frame_count = self.frame_id
# file_path = "/home/administrator/aarti_violation_videos/" + str(unique_id) + ".webm"
# cur.execute(sql,
# [frame_count, 10, str(unique_id), datetime.datetime.now(), file_path,
# v])
# self.conn.commit()
# cur.close()
# except:
#
# self.conn.rollback()
self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list, self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list,
event=v, frame_id=rec_inf[2]) event=v, frame_id=rec_inf[2])
with open("output/recorded_video_list.txt", "a") as f:
f.write(rec_inf[2] + ".webm")
f.write("\n")
del self.active_rec[object_id]
person_without_safety_kit += 1 person_without_safety_kit += 1
cv2.rectangle(frame, (person_bb[0], person_bb[1]), (person_bb[2], person_bb[3]), (0, 0, 255), 2) cv2.rectangle(frame, (person_bb[0], person_bb[1]), (person_bb[2], person_bb[3]), (0, 0, 255), 2)
# cv2.waitKey(1)
# for (object_id, det) in zip(other_class_name, other_centroid):
#
# centroid = object_id[1]
# object_id = object_id[0]
# #logger.info(det)
# c = (int(det[1] + (det[3] - det[1]) / 2), int(det[0] + (det[2] - det[0]) / 2))
# frame = draw_circles_on_frame(frame, tuple(centroid), radius=4, color=(0, 0, 255),
# thickness=-1)
#
# for (other_class_item, other_bb) in zip(other_class, other_bbox):
# y1, x1, y2, x2 = other_bb
# bb_x, bb_y = int(x1 + (x2 - x1)/2), int(y1 + (y2 - y1)/2)
# # frame = draw_circles_on_frame(frame, (bb_x, bb_y), radius=10, color=(0, 255, 255),
# # thickness=-1)
# if(bb_x > x1 and bb_x < x2 and bb_y > y1 and bb_y < y2):
# detected_objects.append(other_class_item) if other_class_item not in detected_objects else detected_objects
# if other_class_item == 'Helmet':
# frame = cv2.putText(frame, "Safety Helmet Present", (bb_x, bb_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
# if other_class_item == 'Vest':
# frame = cv2.putText(frame, "Safety Vest Present", (bb_x, bb_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
# #logger.info("Detected Objects " + str(detected_objects))
# else:
# cv2.rectangle(frame, (other_bb[1], other_bb[0]), (other_bb[3], other_bb[2]),(0, 0, 255), 2)
# # else:
# # frame = draw_circles_on_frame(frame, centroid, radius=10, color=(0, 255, 0),thickness=-1)
# # logger.info("Tracking Person with object ID " + str(object_id) + " Detected Items: " +
# # str(detected_objects) + " Missing Items: " + str(set(needed_objects) - set(detected_objects)))
#
# if object_id in self.tracking_people:
# if len(detected_objects) > len(self.tracking_people[object_id]):
# self.tracking_people[object_id] = detected_objects
# else:
# self.tracking_people[object_id] = detected_objects
#
# #logger.info("Tracking People: " + str(self.tracking_people))
#
# if (cv2.pointPolygonTest(self.polygon, tuple(centroid), False) == 1.0):
# frame = cv2.putText(frame, str(object_id), tuple(centroid), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, 2)
# frame = cv2.putText(frame, "Inside Bounding Area", tuple(centroid), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 1, 2)
# else:
# frame = cv2.rectangle(frame, (det[1], det[0]), (det[3], det[2]), (255, 255, 0), 2)
# frame = cv2.putText(frame, "Outside Bounding Area", tuple(centroid), cv2.FONT_HERSHEY_SIMPLEX, 1,
# (0, 0, 255), 1, 2)
# cv2.putText(frame, str(object_id), tuple(centroid), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, 2)
# self.final_ppe_result[object_id] = {"Found":self.tracking_people[object_id],
# "Missing":set(set(needed_objects) - set(detected_objects))
# }
# self.send_payload(frame=resize_to_64_64(frame), message = str(self.final_ppe_result))
# self.final_ppe_result = {}
# print("person with safety kit", str(person_with_safety_kit))
# print("person without safety kit", str(person_without_safety_kit))
person_without_kit_text = "PERSON WITHOUT AIR BREATHING MASK : " + str(person_without_safety_kit) person_without_kit_text = "PERSON WITHOUT AIR BREATHING MASK : " + str(person_without_safety_kit)
person_with_kit_text = "PERSON WITH AIR BREATHING MASK : " + str(person_with_safety_kit) person_with_kit_text = "PERSON WITH AIR BREATHING MASK : " + str(person_with_safety_kit)
...@@ -1023,11 +663,12 @@ class Ppe(ModelWrapper): ...@@ -1023,11 +663,12 @@ class Ppe(ModelWrapper):
def inference( def inference(
self, self,
frame, dets,
classes, classes,
frame
): ):
dets = run(self.yolo_model, frame) # dets = run(self.yolo_model, frame)
class_name = list() class_name = list()
bboxs = [] bboxs = []
other_centroid = [] other_centroid = []
...@@ -1061,39 +702,47 @@ class Ppe(ModelWrapper): ...@@ -1061,39 +702,47 @@ class Ppe(ModelWrapper):
# cv2.rectangle(frame, (i["points"][0], i["points"][1]), (i["points"][2], i["points"][3]), # cv2.rectangle(frame, (i["points"][0], i["points"][1]), (i["points"][2], i["points"][3]),
# (255, 0, 0), 2) # (255, 0, 0), 2)
other_centroid.append([i["centroid"][0], i["centroid"][1]]) other_centroid.append([i["centroid"][0], i["centroid"][1]])
return bboxs,frame, class_name, other_class_name, other_centroid return bboxs, frame, class_name, other_class_name, other_centroid
def _predict(self, obj): def _predict(self, obj):
self.count+= 1 self.count+= 1
class_list = ["person"] class_list = ["person"]
###################
try: try:
time.sleep(0.05)
frame = obj['frame'] frame = obj['frame']
self.frame_id = obj["frameId"] # frame = cv2.resize(frame, (480, 270))
if(self.frame_id == 0): id = int(obj['frameId'])
self.reported_violation_ids = {} # if self.frame_skipping["to_skip"]:
self.safety_equip = ExpiringDict(max_age_seconds=30, max_len=10) # if not self.frame_skipping["skip_current_frame"]:
# if self.type == 'videofile':
if self.frame_skip: # dets = self.dets[id][str(id)]['detections']
if not self.frame_skipping["skip_current_frame"]: # else:
dets = self.yp.predict(frame) # # dets = self.yp.predict(frame)
self.frame_skipping["detection_value"] = dets # print("**************")
self.frame_skipping["skip_current_frame"] = True # # dets = self.yp.predict(frame)
else: # self.frame_skipping["detection_value"] = dets
dets = self.frame_skipping["detection_value"] # self.frame_skipping["skip_current_frame"] = True
self.frame_skipping["skip_current_frame"] = False # else:
# dets = self.frame_skipping["detection_value"]
# self.frame_skipping["skip_current_frame"] = False
# else:
if self.type == 'videofile':
dets = self.dets[id][str(id)]['detections']
else: else:
# frame = cv2.polylines(frame, self.polygon, # dets = self.yp.predict(frame)
# True, (255, 255, 255), 2) print("******************")
bbox, frame, class_name, other_class_name, other_centroid = self.inference(frame, class_list)
# cv2.imshow("out", cv2.resize(frame, (900, 600)))
# cv2.waitKey(1)
# frame = self.draw_line_over_image(frame)
# if [True for e in dets if e['class'] == 'cement_bag']:
##################
# if bbox: bbox, frame, class_name, other_class_name, other_centroid = self.inference(dets, class_list, frame)
print("bounding box")
print(bbox)
frame, objects, boxs = self.kalman_tracker(bbox, frame) frame, objects, boxs = self.kalman_tracker(bbox, frame)
print("kalman outputs")
print(objects)
#logger.info("PRINTING KALMAN OUTPUT") #logger.info("PRINTING KALMAN OUTPUT")
#logger.info(objects) #logger.info(objects)
#logger.info(boxs) #logger.info(boxs)
...@@ -1111,8 +760,8 @@ class Ppe(ModelWrapper): ...@@ -1111,8 +760,8 @@ class Ppe(ModelWrapper):
1, cv2.LINE_AA) 1, cv2.LINE_AA)
# cv2.imshow("output is ", cv2.resize(frame, (1000, 800))) cv2.imshow("output is ", cv2.resize(frame, (1000, 800)))
# cv2.waitKey(1) cv2.waitKey(1)
except Exception as e: except Exception as e:
logger.exception(f"Error: {e}", exc_info=True) logger.exception(f"Error: {e}", exc_info=True)
obj['frame'] = cv2.resize(obj['frame'], (self.config.get('FRAME_WIDTH'), self.config.get('FRAME_HEIGHT'))) obj['frame'] = cv2.resize(obj['frame'], (self.config.get('FRAME_WIDTH'), self.config.get('FRAME_HEIGHT')))
......
...@@ -9,6 +9,7 @@ import numpy as np ...@@ -9,6 +9,7 @@ import numpy as np
import cv2 import cv2
np.seterr(divide='ignore', invalid='ignore')
class Box: class Box:
def __init__(self): def __init__(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment