Alter this code so that for object tracking we use the existing YOLO model instead of…
- User
Alter this code so that for object tracking we use the existing YOLO model instead of DeepSort: `import sqlite3 import datetime from Status import Status import numpy as np
class Database: """ A class to handle interactions with the SQLite database for object tracking.
Attributes: conn (sqlite3.Connection): The database connection. cursor (sqlite3.Cursor): The database cursor. """ def __init__(self, db_path='object_tracking.db'): """ Initializes the Database class with a connection to the specified SQLite database. Args: db_path (str): The path to the SQLite database file. """ self.conn = sqlite3.connect(db_path) self.cursor = self.conn.cursor() self.create_table() self.add_person_name_column() self.create_known_faces_table() self.create_known_face_object_link_table() def create_table(self): """ Creates the 'tracked_objects' table if it does not already exist. """ self.cursor.execute(''' CREATE TABLE IF NOT EXISTS tracked_objects ( id INTEGER PRIMARY KEY AUTOINCREMENT, class_name TEXT, x1 INTEGER, y1 INTEGER, width INTEGER, height INTEGER, last_seen DATETIME, status TEXT, detection_count INTEGER DEFAULT 0 ) ''') self.conn.commit() def create_known_face_object_link_table(self): """ Creates the 'known_face_object_link' table if it does not already exist. """ self.cursor.execute(''' CREATE TABLE IF NOT EXISTS known_face_object_link ( known_face_name INTEGER, object_id INTEGER, FOREIGN KEY (known_face_name) REFERENCES known_faces(name), FOREIGN KEY (object_id) REFERENCES tracked_objects(id), PRIMARY KEY (known_face_name, object_id) ) ''') self.conn.commit() def create_known_faces_table(self): """ Creates the 'known_faces' table if it does not already exist. """ self.cursor.execute(''' CREATE TABLE IF NOT EXISTS known_faces ( name TEXT NOT NULL PRIMARY KEY, embedding BLOB NOT NULL ) ''') self.conn.commit() def fetch_objects(self, class_name=None): """ Fetches objects from the database, optionally filtered by class name. Args: class_name (str, optional): The class name to filter objects. Returns: list: A list of tuples representing the objects. """ if class_name: self.cursor.execute('SELECT * FROM tracked_objects WHERE class_name=?', (class_name,)) else: self.cursor.execute('SELECT * FROM tracked_objects') return self.cursor.fetchall() def fetch_all_objects(self): """ Fetches all objects from the database. Returns: list: A list of tuples representing all objects. """ self.cursor.execute('SELECT * FROM tracked_objects') return self.cursor.fetchall() def fetch_object_by_id(self, obj_id): """ Fetches a single object by its ID. Args: obj_id (int): The ID of the object. Returns: tuple: The object data as a tuple. """ self.cursor.execute('SELECT * FROM tracked_objects WHERE id=?', (obj_id,)) return self.cursor.fetchone() def update_object(self, obj_id, x1, y1, width, height): """ Updates the position and size of an existing object in the database, and increments detection_count. Args: obj_id (int): The ID of the object to update. x1 (int): The new x-coordinate of the object's top-left corner. y1 (int): The new y-coordinate of the object's top-left corner. width (int): The new width of the object. height (int): The new height of the object. Returns: int: The ID of the updated object. """ last_seen = datetime.datetime.now().isoformat() status = Status.IN_FRAME.value # Set status to 'In Frame' self.cursor.execute(''' UPDATE tracked_objects SET x1=?, y1=?, width=?, height=?, last_seen=?, status=?, detection_count=detection_count+1 WHERE id=? ''', (x1, y1, width, height, last_seen, status, obj_id)) self.conn.commit() return obj_id def insert_object(self, class_name, x1, y1, width, height, person_name=None): """ Inserts a new object into the database, and returns the new object's ID. Args: class_name (str): The class name of the object. x1 (int): The x-coordinate of the object's top-left corner. y1 (int): The y-coordinate of the object's top-left corner. width (int): The width of the object. height (int): The height of the object. person_name (str, optional): The name of the person associated with the object. Returns: int: The ID of the newly inserted object. """ last_seen = datetime.datetime.now().isoformat() status = Status.IN_FRAME.value detection_count = 1 self.cursor.execute(''' INSERT INTO tracked_objects (class_name, x1, y1, width, height, last_seen, status, detection_count, person_name) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ''', (class_name, x1, y1, width, height, last_seen, status, detection_count, person_name)) self.conn.commit() return self.cursor.lastrowid def update_status(self, obj_id, status): """ Updates the status of an object in the database. Args: obj_id (int): The ID of the object to update. status (Status): The new status of the object. """ self.cursor.execute(''' UPDATE tracked_objects SET status=? WHERE id=? ''', (status.value, obj_id)) self.conn.commit() def delete_object(self, obj_id): """ Deletes an object from the database. Args: obj_id (int): The ID of the object to delete. """ self.cursor.execute(''' DELETE FROM tracked_objects WHERE id=? ''', (obj_id,)) self.conn.commit() def add_person_name_column(self): """ Adds the 'person_name' column to 'tracked_objects' table if it doesn't exist. """ self.cursor.execute("PRAGMA table_info(tracked_objects)") columns = [column[1] for column in self.cursor.fetchall()] if 'person_name' not in columns: self.cursor.execute('ALTER TABLE tracked_objects ADD COLUMN person_name TEXT') self.conn.commit() def insert_known_face(self, name, embedding): """ Inserts a new known face into the database. Args: name (str): The name of the person. embedding (np.ndarray): The face embedding. """ embedding_blob = embedding.astype(np.float32).tobytes() self.cursor.execute(''' INSERT INTO known_faces (name, embedding) VALUES (?, ?) ''', (name, embedding_blob)) self.conn.commit() def fetch_known_faces(self): """ Fetches all known faces from the database. Returns: list: A list of tuples representing known faces. """ self.cursor.execute('SELECT * FROM known_faces') known_faces = [] for row in self.cursor.fetchall(): name = row[0] embedding_blob = row[1] embedding = np.frombuffer(embedding_blob, dtype=np.float32) known_faces.append((name, embedding)) return known_faces def get_known_face_by_name(self, name): """ Fetches a known face by name from the database. Args: name (str): The name of the person. Returns: tuple: A tuple representing the known face. """ self.cursor.execute('SELECT * FROM known_faces WHERE name=?', (name,)) row = self.cursor.fetchone() if row: name = row[0] embedding_blob = row[1] embedding = np.frombuffer(embedding_blob, dtype=np.float32) return (name, embedding) return None def update_known_face_object_link(self, obj_id, person_name): self.cursor.execute(''' SELECT object_id FROM known_face_object_link WHERE known_face_name=? ''', (person_name,)) obj_ids = self.cursor.fetchall() for obj in obj_ids: if obj[0] != obj_id: self.cursor.execute(''' DELETE FROM tracked_objects WHERE id=? ''', (obj[0],)) # Delete any existing records with the same known_face_name self.cursor.execute(''' DELETE FROM known_face_object_link WHERE known_face_name=? OR object_id=? ''', (person_name,obj_id,)) # Insert the new record self.cursor.execute(''' INSERT INTO known_face_object_link (known_face_name, object_id) VALUES (?, ?) ''', (person_name, obj_id)) self.conn.commit()`,`import cv2 - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.