Commit fb3213d2 authored by Sikhin VC's avatar Sikhin VC

added video rec feature

parent fafb4719
...@@ -8,7 +8,7 @@ class FrameProcessor: ...@@ -8,7 +8,7 @@ class FrameProcessor:
self.stream = stream self.stream = stream
logger.info("Setting up frame processor !!") logger.info("Setting up frame processor !!")
self.count = 0 self.count = 0
self.skip_frame_every = 30 # 1 does not skip any frame (n-1 frames get skipped) 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():
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
import os
import uuid
from scipy.spatial import distance from scipy.spatial import distance
# from edge_engine.common.logsetup import logger # from edge_engine.common.logsetup import logger
# from yolov5processor.infer import ExecuteInference # from yolov5processor.infer import ExecuteInference
...@@ -35,6 +38,9 @@ import psycopg2 ...@@ -35,6 +38,9 @@ import psycopg2
import time import time
class Ppe(ModelWrapper): class Ppe(ModelWrapper):
def __init__(self, config, model_config, pubs, device_id): def __init__(self, config, model_config, pubs, device_id):
...@@ -94,6 +100,7 @@ class Ppe(ModelWrapper): ...@@ -94,6 +100,7 @@ class Ppe(ModelWrapper):
# connect to the PostgreSQL server # connect to the PostgreSQL server
# print('Connecting to the PostgreSQL database...') # print('Connecting to the PostgreSQL database...')
self.conn = psycopg2.connect(self.uri) self.conn = psycopg2.connect(self.uri)
self.active_rec = {}
def _pre_process(self, x): def _pre_process(self, x):
...@@ -337,6 +344,22 @@ class Ppe(ModelWrapper): ...@@ -337,6 +344,22 @@ 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 = os.getcwd()
file_name =str(unique_id) + ".avi"
file_name = os.path.join(working_dir, "output", 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(*'MJPG'),
10, size)
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):
""" """
Maintains the bag counts Maintains the bag counts
...@@ -413,6 +436,18 @@ class Ppe(ModelWrapper): ...@@ -413,6 +436,18 @@ class Ppe(ModelWrapper):
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(0) # cv2.waitKey(0)
if (object_id in self.active_rec):
rec_inf = self.active_rec[object_id]
rec_inf[1].release()
temp_v_list = rec_inf[3]
for v in temp_v_list:
self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list,
event=v, frame_id=rec_inf[2])
with open("recorded_video_list.txt", "a") as f:
f.write(rec_inf[2] + ".avi")
f.write("\n")
del self.active_rec[object_id]
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"])
...@@ -425,32 +460,78 @@ class Ppe(ModelWrapper): ...@@ -425,32 +460,78 @@ 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")
print(violated_items) # print(violated_items)
# violated_items = violated_items # violated_items = violated_items
# print(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")
self.send_payload(frame=resize_to_64_64(frame), message=temp_violation_list, event = violated_items,frame_id=self.frame_id) unique_id = str(uuid.uuid4()) + str(time.time())
self.reported_violation_ids[object_id] = time.time()
for v in violations:
result = self.video_write(frame, 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);' sql = 'INSERT INTO "aarti_violation_event_table" ("camera_id", "frame_id", "timestamp", "violation_type") VALUES (%s, %s, %s, %s);'
# for violated_items in violations: # for violated_items in violations:
# present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table' # present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table'
cur = self.conn.cursor() cur = self.conn.cursor()
# cur.execute("ROLLBACK")
frame_count = self.frame_id frame_count = self.frame_id
cur.execute(sql, [4, frame_count, datetime.datetime.now(), v]) cur.execute(sql, [4, frame_count, datetime.datetime.now(), v])
self.conn.commit() self.conn.commit()
cur.close() 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/historical_videos/" + str(unique_id) + ".avi"
cur.execute(sql, [frame_count, 4, 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]
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):
rec_inf = self.active_rec[object_id]
rec_inf[1].release()
temp_v_list = rec_inf[3]
for v in temp_v_list:
self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list,
event=v, frame_id=rec_inf[2])
with open("recorded_video_list.txt", "a") as f:
f.write(rec_inf[2] + ".avi")
f.write("\n")
del self.active_rec[object_id]
person_without_safety_kit += 1 person_without_safety_kit += 1
...@@ -464,12 +545,31 @@ class Ppe(ModelWrapper): ...@@ -464,12 +545,31 @@ class Ppe(ModelWrapper):
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(0) # cv2.waitKey(0)
if (object_id in self.active_rec):
rec_inf = self.active_rec[object_id]
rec_inf[1].release()
temp_v_list = rec_inf[3]
for v in temp_v_list:
self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list,
event=v, frame_id=rec_inf[2])
with open("recorded_video_list.txt", "a") as f:
f.write(rec_inf[2] + ".avi")
f.write("\n")
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"])
# print("violations") # print("violations")
# 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)
...@@ -486,10 +586,21 @@ class Ppe(ModelWrapper): ...@@ -486,10 +586,21 @@ 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")
self.send_payload(frame=resize_to_64_64(frame), message=temp_violation_list , event = violated_items, frame_id=self.frame_id) # 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 + ".avi")
# f.write("\n")
result = self.video_write(frame, 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() self.reported_violation_ids[object_id] = time.time()
for v in violations: 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);' sql = 'INSERT INTO "aarti_violation_event_table" ("camera_id", "frame_id", "timestamp", "violation_type") VALUES (%s, %s, %s, %s);'
# for violated_items in violations: # for violated_items in violations:
# present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table' # present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table'
...@@ -498,12 +609,38 @@ class Ppe(ModelWrapper): ...@@ -498,12 +609,38 @@ class Ppe(ModelWrapper):
cur.execute(sql, [4, frame_count, datetime.datetime.now(), v]) cur.execute(sql, [4, frame_count, datetime.datetime.now(), v])
self.conn.commit() self.conn.commit()
cur.close() 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/historical_videos/" + str(unique_id) + ".avi"
cur.execute(sql, [frame_count, 4, 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]
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):
rec_inf = self.active_rec[object_id]
rec_inf[1].release()
temp_v_list = rec_inf[3]
for v in temp_v_list:
self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list,
event=v, frame_id=rec_inf[2])
with open("recorded_video_list.txt", "a") as f:
f.write(rec_inf[2] + ".avi")
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(0) # cv2.waitKey(0)
...@@ -515,6 +652,17 @@ class Ppe(ModelWrapper): ...@@ -515,6 +652,17 @@ class Ppe(ModelWrapper):
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(0) # cv2.waitKey(0)
if(object_id in self.active_rec):
rec_inf = self.active_rec[object_id]
rec_inf[1].release()
temp_v_list = rec_inf[3]
for v in temp_v_list:
self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list,
event=v, frame_id=rec_inf[2])
with open("recorded_video_list.txt", "a") as f:
f.write(rec_inf[2] + ".avi")
f.write("\n")
del self.active_rec[object_id]
else: else:
...@@ -534,15 +682,30 @@ class Ppe(ModelWrapper): ...@@ -534,15 +682,30 @@ class Ppe(ModelWrapper):
violated_items = violated_items violated_items = violated_items
# violated_items_2 = violated_items_2 + ", Air Breathing Mask" # violated_items_2 = violated_items_2 + ", Air Breathing Mask"
# print(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")
self.send_payload(frame=resize_to_64_64(frame), message=temp_violation_list, event = violated_items, frame_id=self.frame_id) # 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 + ".avi")
# f.write("\n")
result = self.video_write(frame, 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() self.reported_violation_ids[object_id] = time.time()
for v in violations: 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);' sql = 'INSERT INTO "aarti_violation_event_table" ("camera_id", "frame_id", "timestamp", "violation_type") VALUES (%s, %s, %s, %s);'
# for violated_items in violations: # for violated_items in violations:
# present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table' # present_event_id = 'SELECT "event_id" FROM aarti_violation_event_table'
...@@ -551,13 +714,42 @@ class Ppe(ModelWrapper): ...@@ -551,13 +714,42 @@ class Ppe(ModelWrapper):
cur.execute(sql, [4, frame_count, datetime.datetime.now(), v]) cur.execute(sql, [4, frame_count, datetime.datetime.now(), v])
self.conn.commit() self.conn.commit()
cur.close() 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/historical_videos/" + str(unique_id) + ".avi"
cur.execute(sql, [frame_count, 4, 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]
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):
rec_inf = self.active_rec[object_id]
rec_inf[1].release()
temp_v_list = rec_inf[3]
for v in temp_v_list:
self.send_payload(frame=resize_to_64_64(frame), message=temp_v_list,
event=v, frame_id=rec_inf[2])
with open("recorded_video_list.txt", "a") as f:
f.write(rec_inf[2] + ".avi")
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(0) # cv2.waitKey(0)
......
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