Commit e0c2b01e authored by Sikhin VC's avatar Sikhin VC

code optimization and cleaning

parent 85dfc12b
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -24,7 +24,6 @@ import time ...@@ -24,7 +24,6 @@ import time
import json import json
class Ppe(ModelWrapper): class Ppe(ModelWrapper):
def __init__(self, config, model_config, pubs, device_id): def __init__(self, config, model_config, pubs, device_id):
...@@ -57,7 +56,6 @@ class Ppe(ModelWrapper): ...@@ -57,7 +56,6 @@ class Ppe(ModelWrapper):
self.track_id_list = deque([str(i) for i in range(1, 50)]) self.track_id_list = deque([str(i) for i in range(1, 50)])
self.prev_annotation = [] self.prev_annotation = []
self.initial_object_position = None self.initial_object_position = None
self.uncounted_objects = ExpiringDict(max_len=model_config.get("uncounted_obj_length", 50), self.uncounted_objects = ExpiringDict(max_len=model_config.get("uncounted_obj_length", 50),
...@@ -71,14 +69,14 @@ class Ppe(ModelWrapper): ...@@ -71,14 +69,14 @@ class Ppe(ModelWrapper):
self.skip_frame_count = 0 self.skip_frame_count = 0
self.frame_id = 0 self.frame_id = 0
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.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"}
self.active_rec = {} self.active_rec = {}
def _pre_process(self, x): def _pre_process(self, x):
""" """
Do preprocessing here, if any Do preprocessing here, if any
...@@ -97,7 +95,7 @@ class Ppe(ModelWrapper): ...@@ -97,7 +95,7 @@ class Ppe(ModelWrapper):
return x return x
def send_payload(self, frame, label='PPEFrame', bg_color="#474520", font_color="#FFFF00", alert_sound=None, def send_payload(self, frame, label='PPEFrame', bg_color="#474520", font_color="#FFFF00", alert_sound=None,
message=[], event = "", frame_id = None): message=[], event="", frame_id=None):
""" """
Insert event to Mongox Insert event to Mongox
:param message: :param message:
...@@ -112,7 +110,8 @@ class Ppe(ModelWrapper): ...@@ -112,7 +110,8 @@ class Ppe(ModelWrapper):
payload = {"deviceId": self.device_id, "message": message, payload = {"deviceId": self.device_id, "message": message,
"frame": 'data:image/jpeg;base64,' + base64.b64encode( "frame": 'data:image/jpeg;base64,' + base64.b64encode(
cv2.imencode('.jpg', frame)[1].tostring()).decode("utf-8"), "activity": label, cv2.imencode('.jpg', frame)[1].tostring()).decode("utf-8"), "activity": label,
"bg_color": bg_color, "font_color": font_color, "alert_sound": alert_sound, "frame_id": frame_id, "event_type": event} "bg_color": bg_color, "font_color": font_color, "alert_sound": alert_sound, "frame_id": frame_id,
"event_type": event}
self.mongo_logger.insert_attendance_event_to_mongo(payload) self.mongo_logger.insert_attendance_event_to_mongo(payload)
...@@ -256,8 +255,6 @@ class Ppe(ModelWrapper): ...@@ -256,8 +255,6 @@ class Ppe(ModelWrapper):
# _alignment = "vertical" # _alignment = "vertical"
return _alignment, _coordinates return _alignment, _coordinates
def get_line_coordinates(self): def get_line_coordinates(self):
""" """
Get the line coordinates from the deployment JSON Get the line coordinates from the deployment JSON
...@@ -267,7 +264,7 @@ class Ppe(ModelWrapper): ...@@ -267,7 +264,7 @@ class Ppe(ModelWrapper):
_coordinates = [self.janus_metadata['metadata'].get(coordinate_key) for coordinate_key in _coordinates = [self.janus_metadata['metadata'].get(coordinate_key) for coordinate_key in
JanusDeploymentConstants.LINE_COORDINATES] JanusDeploymentConstants.LINE_COORDINATES]
_alignment = self.janus_metadata['metadata'].get(JanusDeploymentConstants.ALIGNMENT_KEY)\ _alignment = self.janus_metadata['metadata'].get(JanusDeploymentConstants.ALIGNMENT_KEY) \
# _coordinates = [550, 200, 555, 1100] # _coordinates = [550, 200, 555, 1100]
# #
# _alignment = "vertical" # _alignment = "vertical"
...@@ -315,8 +312,11 @@ class Ppe(ModelWrapper): ...@@ -315,8 +312,11 @@ class Ppe(ModelWrapper):
return True return True
else: else:
return False return False
@staticmethod
def interpolation(x_res, y_res):
x_return = (x_res/1920) * 800
y_return = (y_res / 1080) * 600
return (int(x_return), int(y_return))
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):
""" """
Maintains the bag counts Maintains the bag counts
...@@ -331,55 +331,38 @@ class Ppe(ModelWrapper): ...@@ -331,55 +331,38 @@ class Ppe(ModelWrapper):
person_without_safety_kit = 0 person_without_safety_kit = 0
total_person_safety_kits = {"Air Breathing Mask"} total_person_safety_kits = {"Air Breathing Mask"}
#logger.info("Detections: " + str(detections))
for (object_id, class_detected, person_bb) in zip( for (object_id, class_detected, person_bb) in zip(
detection_objects, class_name, bbox detection_objects, class_name, bbox
): ):
centroid = object_id[1] centroid = object_id[1]
# print(object_id)
object_id = object_id[0] object_id = object_id[0]
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:
person_safety_status = set() person_safety_status = 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):
if (person_bb[0] < object_bb[0] and person_bb[2] > object_bb[0]): if person_bb[0] < object_bb[0] < person_bb[2]:
if(person_bb[1] < object_bb[1] and person_bb[3] > object_bb[1]): if person_bb[1] < object_bb[1] < person_bb[3]:
person_safety_status.add(safety_object) person_safety_status.add(safety_object)
print("SAFETY OBJECTSSSS")
print(safety_object)
# print(safety_object, object_bb) if "object_id" in self.safety_equip:
if("object_id" in self.safety_equip):
temp_list = set()
temp_list = self.safety_equip["object_id"] temp_list = self.safety_equip["object_id"]
temp_list.add(safety_object) temp_list.add(safety_object)
# temp_list = self.safety_equip["object_id"]
self.safety_equip["object_id"] = temp_list self.safety_equip["object_id"] = temp_list
# print("object id present")
else: else:
temp_list = set() temp_list = set()
temp_list.add(safety_object) temp_list.add(safety_object)
# print("object id not present")
self.safety_equip["object_id"] = temp_list self.safety_equip["object_id"] = temp_list
if "Air Breathing Mask" in self.safety_equip["object_id"] and "Safety helmet" in self.safety_equip[
if("Air Breathing Mask" in self.safety_equip["object_id"] and "Safety helmet" in self.safety_equip["object_id"]): "object_id"]:
# print("with both helmet and air breathing mask if") if self.safety_equip["object_id"] == needed_objects_with_mask_helmet:
if (self.safety_equip["object_id"] == needed_objects_with_mask_helmet):
# print("SAFE--------------------------------------------")
person_with_safety_kit += 1 person_with_safety_kit += 1
cv2.rectangle(frame, (person_bb[0], person_bb[1]), (person_bb[2], person_bb[3]), (0, 255, 0), 2) cv2.rectangle(frame, (person_bb[0], person_bb[1]), (person_bb[2], person_bb[3]), (0, 255, 0), 2)
# cv2.waitKey(1)
if (object_id in self.active_rec): if object_id in self.active_rec:
rec_inf = self.active_rec[object_id] rec_inf = self.active_rec[object_id]
# rec_inf[1].release()
temp_v_list = rec_inf[3] temp_v_list = rec_inf[3]
print(temp_v_list)
# cv2.waitKey(1)
for v in temp_v_list: for v in temp_v_list:
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])
...@@ -388,111 +371,73 @@ class Ppe(ModelWrapper): ...@@ -388,111 +371,73 @@ class Ppe(ModelWrapper):
else: else:
violations = needed_objects_with_mask_helmet.difference(self.safety_equip["object_id"]) violations = needed_objects_with_mask_helmet.difference(self.safety_equip["object_id"])
# print("violations")
# print(violations)
temp_violation_list = [] temp_violation_list = []
for v in violations: for v in violations:
temp_violation_list.append(self.payload_classes[v]) temp_violation_list.append(self.payload_classes[v])
# 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)))
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")
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()
else: else:
time_diff = time.time() - self.reported_violation_ids[object_id] time_diff = time.time() - self.reported_violation_ids[object_id]
if(time_diff > 30): if time_diff > 30:
del self.reported_violation_ids[object_id] del self.reported_violation_ids[object_id]
if (object_id in self.active_rec): if object_id in self.active_rec:
rec_inf = self.active_rec[object_id] rec_inf = self.active_rec[object_id]
# rec_inf[1].release()
temp_v_list = rec_inf[3] temp_v_list = rec_inf[3]
print(temp_v_list)
cv2.waitKey(1)
for v in temp_v_list: for v in temp_v_list:
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])
del self.active_rec[object_id] 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)
elif ("Air Breathing Mask" in self.safety_equip["object_id"]): elif "Air Breathing Mask" in self.safety_equip["object_id"]:
# print("air breathing mask if") if self.safety_equip["object_id"] == needed_objects_with_mask:
print(self.safety_equip["object_id"])
if (self.safety_equip["object_id"] == needed_objects_with_mask):
# print("SAFE--------------------------------------------")
person_with_safety_kit += 1 person_with_safety_kit += 1
cv2.rectangle(frame, (person_bb[0], person_bb[1]), (person_bb[2], person_bb[3]), (0, 255, 0), 2) cv2.rectangle(frame, (person_bb[0], person_bb[1]), (person_bb[2], person_bb[3]), (0, 255, 0), 2)
# cv2.waitKey(1) if object_id in self.active_rec:
if (object_id in self.active_rec):
rec_inf = self.active_rec[object_id] rec_inf = self.active_rec[object_id]
# rec_inf[1].release()
temp_v_list = rec_inf[3] temp_v_list = rec_inf[3]
print(temp_v_list)
# cv2.waitKey(1)
for v in temp_v_list: for v in temp_v_list:
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]
else: else:
violations = needed_objects_with_mask.difference(self.safety_equip["object_id"]) violations = needed_objects_with_mask.difference(self.safety_equip["object_id"])
for elem in violations: for elem in violations:
self.violation_count[elem].append(elem) self.violation_count[elem].append(elem)
temp_violation_list = [] temp_violation_list = []
for v in violations: for v in violations:
temp_violation_list.append(self.payload_classes[v]) temp_violation_list.append(self.payload_classes[v])
temp_violation_list.sort() temp_violation_list.sort()
violated_items = ', '.join(list(map(str, temp_violation_list))) violated_items = ', '.join(list(map(str, temp_violation_list)))
violated_items_2 = ', '.join(list(map(str, violations))) if object_id not in self.reported_violation_ids:
print("violated items")
print(violated_items) self.send_payload(frame=resize_to_64_64(frame), message=temp_violation_list,
# print(msg) event=violated_items, frame_id=self.frame_id)
# print("VIOLATION LIST")
print(violated_items)
if (object_id not in self.reported_violation_ids):
print("sending to mongo")
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()
else: else:
time_diff = time.time() - self.reported_violation_ids[object_id] time_diff = time.time() - self.reported_violation_ids[object_id]
if(time_diff > 30): if time_diff > 30:
del self.reported_violation_ids[object_id] del self.reported_violation_ids[object_id]
if (object_id in self.active_rec): if object_id in self.active_rec:
rec_inf = self.active_rec[object_id] rec_inf = self.active_rec[object_id]
# rec_inf[1].release()
temp_v_list = rec_inf[3] temp_v_list = rec_inf[3]
print(temp_v_list)
# cv2.waitKey(1)
for v in temp_v_list: for v in temp_v_list:
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])
...@@ -500,123 +445,95 @@ class Ppe(ModelWrapper): ...@@ -500,123 +445,95 @@ class Ppe(ModelWrapper):
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)
else: else:
print("without air breathing mask if") if self.safety_equip["object_id"] == needed_objects_with_helmet:
if (self.safety_equip["object_id"] == needed_objects_with_helmet):
# print("SAFE--------------------------------------------")
person_with_safety_kit += 1 person_with_safety_kit += 1
cv2.rectangle(frame, (person_bb[0], person_bb[1]), (person_bb[2], person_bb[3]), (0, 255, 0), 2) cv2.rectangle(frame, (person_bb[0], person_bb[1]), (person_bb[2], person_bb[3]), (0, 255, 0), 2)
# cv2.waitKey(1) if object_id in self.active_rec:
if(object_id in self.active_rec):
rec_inf = self.active_rec[object_id] rec_inf = self.active_rec[object_id]
# rec_inf[1].release()
temp_v_list = rec_inf[3] temp_v_list = rec_inf[3]
print(temp_v_list)
cv2.waitKey(1)
for v in temp_v_list: for v in temp_v_list:
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])
del self.active_rec[object_id] del self.active_rec[object_id]
else: else:
violations = needed_objects_with_helmet.difference(self.safety_equip["object_id"]) violations = needed_objects_with_helmet.difference(self.safety_equip["object_id"])
# print("violations")
# print(violations)
temp_violation_list = [] temp_violation_list = []
for v in violations: for v in violations:
temp_violation_list.append(self.payload_classes[v]) temp_violation_list.append(self.payload_classes[v])
# temp_violation_list.append("air_breathing_mask_violation")
temp_violation_list.sort() temp_violation_list.sort()
violated_items = ', '.join(list(map(str, temp_violation_list))) violated_items = ', '.join(list(map(str, temp_violation_list)))
violated_items_2 = ', '.join(list(map(str, violations)))
print("violated items")
print(violated_items)
violated_items = violated_items violated_items = violated_items
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") 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()
else: else:
time_diff = time.time() - self.reported_violation_ids[object_id] time_diff = time.time() - self.reported_violation_ids[object_id]
if(time_diff > 30): if time_diff > 30:
del self.reported_violation_ids[object_id] del self.reported_violation_ids[object_id]
if (object_id in self.active_rec): if object_id in self.active_rec:
rec_inf = self.active_rec[object_id] rec_inf = self.active_rec[object_id]
# rec_inf[1].release()
temp_v_list = rec_inf[3] temp_v_list = rec_inf[3]
print(temp_v_list)
cv2.waitKey(1)
for v in temp_v_list: for v in temp_v_list:
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])
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)
person_without_kit_text = "PERSON WITHOUT AIR BREATHING MASK : " + str(person_without_safety_kit) cv2.rectangle(frame, self.interpolation(870, 0), self.interpolation(1400, 50), (0, 0, 255), -1)
person_with_kit_text = "PERSON WITH AIR BREATHING MASK : " + str(person_with_safety_kit) cv2.rectangle(frame, self.interpolation(870, 50), self.interpolation(1400, 120), (50, 50, 50), -1)
cv2.putText(frame, "Air Breathing Mask Violation", self.interpolation(880, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1, 1)
cv2.rectangle(frame, (870, 0), (1400, 50), (0, 0, 255), -1) cv2.rectangle(frame, self.interpolation(1400, 300), self.interpolation(1900, 350), (0, 255, 0), -1)
cv2.rectangle(frame, (870, 50), (1400, 120), (50, 50, 50), -1) cv2.rectangle(frame, self.interpolation(1400, 350), self.interpolation(1900, 420), (50, 50, 50), -1)
cv2.putText(frame, "Air Breathing Mask Violation", (880, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 4, 2) cv2.putText(frame, "People following compliance", self.interpolation(1410, 330), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1, 1)
cv2.rectangle(frame, (1400, 300), (1900, 350), (0, 255, 0), -1) cv2.rectangle(frame, self.interpolation(870, 300), self.interpolation(1400, 350), (0, 0, 255), -1)
cv2.rectangle(frame, (1400, 350), (1900, 420), (50, 50, 50), -1) cv2.rectangle(frame, self.interpolation(870, 350), self.interpolation(1400, 420), (50, 50, 50), -1)
cv2.putText(frame, "People following compliance", (1410, 330), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 4, 2) cv2.putText(frame, "People not following compliance", self.interpolation(880, 330), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1, 1)
cv2.rectangle(frame, (870, 300), (1400, 350), (0, 0, 255), -1) cv2.rectangle(frame, self.interpolation(1400, 0), self.interpolation(1900, 50), (0, 0, 255), -1)
cv2.rectangle(frame, (870, 350), (1400, 420), (50, 50, 50), -1) cv2.rectangle(frame, self.interpolation(1400, 50), self.interpolation(1900, 120), (50, 50, 50), -1)
cv2.putText(frame, "People not following compliance", (880, 330), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 4, 2) cv2.putText(frame, "Coverall Suit Violation", self.interpolation(1410, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1, 1)
cv2.rectangle(frame, (1400, 0), (1900, 50), (0, 0, 255), -1) cv2.rectangle(frame, self.interpolation(870, 150), self.interpolation(1400, 200), (0, 0, 255), -1)
cv2.rectangle(frame, (1400, 50), (1900, 120), (50, 50, 50), -1) cv2.rectangle(frame, self.interpolation(870, 200), self.interpolation(1400, 270), (50, 50, 50), -1)
cv2.putText(frame, "Coverall Suit Violation", (1410, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 4, 2) cv2.putText(frame, "Helmet Violation", self.interpolation(880, 180), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1, 1)
cv2.rectangle(frame, (870, 150), (1400, 200), (0, 0, 255), -1) cv2.rectangle(frame, self.interpolation(1400, 150), self.interpolation(1900, 200), (0, 0, 255), -1)
cv2.rectangle(frame, (870, 200), (1400, 270), (50, 50, 50), -1) cv2.rectangle(frame, self.interpolation(1400, 200), self.interpolation(1900, 270), (50, 50, 50), -1)
cv2.putText(frame, "Helmet Violation", (880, 180), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 4, 2) cv2.putText(frame, "Glove Violation", self.interpolation(1410, 180), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1, 1)
cv2.rectangle(frame, (1400, 150), (1900, 200), (0, 0, 255), -1) if person_without_safety_kit > 0:
cv2.rectangle(frame, (1400, 200), (1900, 270), (50, 50, 50), -1) cv2.putText(frame, str(person_without_safety_kit), self.interpolation(1100, 400), cv2.FONT_HERSHEY_SIMPLEX, 1,
cv2.putText(frame, "Glove Violation", (1410, 180), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 4, 2) (255, 255, 255), 1, 1)
# print("dict")
# print(self.violation_count) if len(self.violation_count["Air Breathing Mask"]) != 0:
if (person_without_safety_kit > 0): cv2.putText(frame, str(len(self.violation_count[""])), self.interpolation(1100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1,
cv2.putText(frame, str(person_without_safety_kit), (1100, 400), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, 1)
(255, 255, 255), 4, 2)
if person_with_safety_kit > 0:
if (len(self.violation_count["Air Breathing Mask"]) != 0): cv2.putText(frame, str(person_with_safety_kit), self.interpolation(1600, 400), cv2.FONT_HERSHEY_SIMPLEX, 1,
cv2.putText(frame, str(len(self.violation_count[""])), (1100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, 1)
(255, 255, 255), 4, 2)
if len(self.violation_count["coverall suit"]) != 0:
if (person_with_safety_kit > 0): cv2.putText(frame, str(len(self.violation_count["coverall suit"])), self.interpolation(1600, 100), cv2.FONT_HERSHEY_SIMPLEX,
cv2.putText(frame, str(person_with_safety_kit), (1600, 400), cv2.FONT_HERSHEY_SIMPLEX, 1, 1, (255, 255, 255), 1, 1)
(255, 255, 255), 4, 2)
if len(self.violation_count["Safety helmet"]) != 0:
if (len(self.violation_count["coverall suit"]) != 0): cv2.putText(frame, str(len(self.violation_count["Safety helmet"])), self.interpolation(1100, 250), cv2.FONT_HERSHEY_SIMPLEX,
cv2.putText(frame, str(len(self.violation_count["coverall suit"])), (1600, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 4, 2) 1, (255, 255, 255), 1, 1)
if (len(self.violation_count["Safety helmet"]) != 0): if len(self.violation_count["Hand gloves"]) != 0:
cv2.putText(frame, str(len(self.violation_count["Safety helmet"])), (1100, 250), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 4, 2) cv2.putText(frame, str(len(self.violation_count["Hand gloves"])), self.interpolation(1600, 250), cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 255, 255), 1, 1)
if (len(self.violation_count["Hand gloves"]) != 0):
cv2.putText(frame, str(len(self.violation_count["Hand gloves"])), (1600, 250), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 4, 2)
self.violation_count = {"Air Breathing Mask": [], "Safety helmet": [], "Hand gloves": [], "coverall suit": []} self.violation_count = {"Air Breathing Mask": [], "Safety helmet": [], "Hand gloves": [], "coverall suit": []}
# print("expiring dict")
# print(self.safety_equip)
# if(len(self.safety_equip) == 0):
# cv2.waitKey(1)
return frame return frame
def draw_line_over_image(self, frame, color=(255, 255, 255)): def draw_line_over_image(self, frame, color=(255, 255, 255)):
...@@ -668,44 +585,32 @@ class Ppe(ModelWrapper): ...@@ -668,44 +585,32 @@ class Ppe(ModelWrapper):
frame frame
): ):
# dets = run(self.yolo_model, frame)
class_name = list() class_name = list()
bboxs = [] bboxs = []
other_centroid = [] other_centroid = []
other_class_name = list() other_class_name = list()
logger.info(dets) logger.info(dets)
print("PREDICTIONS ")
print(dets)
if dets: if dets:
for i in dets: for i in dets:
if(i["class"] in classes): if i["class"] in classes:
# print("detections are there")
# c = (int(i["points"][0] + (i["points"][2] - i["points"][0])/2), int(i["points"][1] + (i["points"][3] - i["points"][1])/2))
# if(cv2.pointPolygonTest(self.polygon, c, False) == 1.0):
#logger.info("Detections inside polygon (i): " + str(i))
class_name.append(i["class"]) class_name.append(i["class"])
frame = cv2.rectangle(frame, (i["points"][0], i["points"][1]), (i["points"][2], i["points"][3]), (255, 255, 0), 2) frame = cv2.rectangle(frame, (i["points"][0], i["points"][1]), (i["points"][2], i["points"][3]),
(255, 255, 0), 2)
bboxs.append([i["points"][0], i["points"][1], i["points"][2], i["points"][3]]) bboxs.append([i["points"][0], i["points"][1], i["points"][2], i["points"][3]])
# bboxs.append([i["points"][1], i["points"][0], i["points"][3], i["points"][2]])
# bboxs.append(
# [i["points"][1], i["points"][0], i["points"][3], i["points"][2]]
# )
#logger.info("BBOX inside polygon: " + str(bboxs))
else: else:
c = tuple(i["centroid"]) c = tuple(i["centroid"])
other_class_name.append(i["class"]) other_class_name.append(i["class"])
cv2.circle(frame, c, 1, 1, thickness=8, lineType=8, shift=0) 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"]
...@@ -713,42 +618,19 @@ class Ppe(ModelWrapper): ...@@ -713,42 +618,19 @@ class Ppe(ModelWrapper):
try: try:
time.sleep(0.05) time.sleep(0.05)
frame = obj['frame'] frame = obj['frame']
# frame = cv2.resize(frame, (480, 270)) frame = cv2.resize(frame, (800, 600))
id = int(obj['frameId']) id = int(obj['frameId'])
# if self.frame_skipping["to_skip"]:
# if not self.frame_skipping["skip_current_frame"]:
# if self.type == 'videofile':
# dets = self.dets[id][str(id)]['detections']
# else:
# # dets = self.yp.predict(frame)
# print("**************")
# # 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:
if self.type == 'videofile': if self.type == 'videofile':
dets = self.dets[id][str(id)]['detections'] dets = self.dets[id][str(id)]['detections']
else:
# dets = self.yp.predict(frame)
print("******************")
##################
bbox, frame, class_name, other_class_name, other_centroid = self.inference(dets, class_list, frame) 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("kalman outputs")
print(objects) print(objects)
#logger.info("PRINTING KALMAN OUTPUT")
#logger.info(objects)
#logger.info(boxs)
frame = self.ppe_detection(frame=frame, bbox=bbox, detection_objects = objects, class_name = class_name, other_class_name = other_class_name, other_centroid = other_centroid) frame = self.ppe_detection(frame=frame, bbox=bbox, detection_objects=objects, class_name=class_name,
#logger.info("Final PPE tracking people result: " + str(self.final_ppe_result)) other_class_name=other_class_name, other_centroid=other_centroid)
# logger.info("Final PPE tracking people result: " + str(self.final_ppe_result))
# call send payload to update final_ppe_result, empty final ppe result dict # call send payload to update final_ppe_result, empty final ppe result dict
...@@ -759,7 +641,6 @@ class Ppe(ModelWrapper): ...@@ -759,7 +641,6 @@ class Ppe(ModelWrapper):
cv2.putText(frame, str(self.frame_id), (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), cv2.putText(frame, str(self.frame_id), (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0),
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:
......
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