def predict_accident(self): insert_into_DB = 1 db = DBConnection() conn = db.get_connection() mycursor = conn.cursor() mycursor.execute("SELECT path FROM buffer") buffer_items = mycursor.fetchall() for path_row in buffer_items: path = path_row[0] clf = Classify(path) class_name, percentage = clf.classify_image() if (class_name[0] is 'a' or class_name[0] is 'A') and (insert_into_DB is 1): insert_into_DB = 0 print('accident detected') Camera_id = 'CAM001' db1 = DBConnection() conn1 = db1.get_connection() mycursor1 = conn1.cursor() mycursor1.execute("SELECT count(path) FROM Accident") count_row = mycursor1.fetchone() new_path = '../accident/Accident' + str(count_row[0]) + '.jpg' copyfile(path, new_path) date_time = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S") timestamp = time.time() sql1 = "insert into Accident(Camera_id,path,date_time,timestampAcc) values(%s,%s,%s,%s);" mycursor1.execute( sql1, [Camera_id, new_path, date_time, int(timestamp)]) conn1.commit() mycursor1.execute( "UPDATE flag set flag_var = 1 where flag_key = 1;") conn1.commit() mycursor1.execute( "UPDATE smbool set continue_buffer = 0 where flag_var = 0") conn1.commit() if (insert_into_DB is 0): print('skipping database entry') sql = "DELETE FROM buffer WHERE path = %s" mycursor.execute(sql, [path]) conn.commit() os.remove(path)
def add_to_buffer(self): count = 0 db = DBConnection() conn = db.get_connection() mycursor = conn.cursor() mycursor.execute("SELECT path FROM vidbuffer") vid_buffer_items = mycursor.fetchall() for row in vid_buffer_items: should_continue = 1 path = row[0] vidcap = cv2.VideoCapture(path) success, image = vidcap.read() while success: img_path = "../buffer/frame" + str(count) + ".jpg" cv2.imwrite(img_path, image) db1 = DBConnection() conn1 = db1.get_connection() mycursor1 = conn1.cursor() mycursor1.execute("INSERT INTO `buffer`(`path`) VALUES (%s);", [img_path]) conn1.commit() count = count + 1 for _ in range(25): success, image = vidcap.read() mycursor1.execute( "SELECT continue_buffer FROM smbool where flag_var = 0") row1 = mycursor1.fetchone() should_continue = row1[0] print("should continue", should_continue) mycursor1.execute( "UPDATE smbool set continue_buffer = 1 where flag_var = 0") conn1.commit() if should_continue is 0: break mycursor1.execute("DELETE FROM `vidbuffer` WHERE `path` = %s", [path]) conn1.commit()