i'm trying to create a vehicle collision detection system for my thesis project but i need to detect the speed of objects and distance to calculate the possible collisions. i'm trying to create it using tensorflow lite and deploy it on a raspberry pi with webcam. i used the examples from tensorflow and i'm trying to modify it but does not seem to work. what's wrong with my code?i tried using AI's to help me with this but it's not much oh an help
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Main script to run the object detection routine."""
import argparse
import sys
import time
import cv2
from tflite_support.task import core
from tflite_support.task import processor
from tflite_support.task import vision
import utils
def run(model: str, camera_id: int, width: int, height: int, num_threads: int,
enable_edgetpu: bool) -> None:
"""Continuously run inference on images acquired from the camera.
Args:
model: Name of the TFLite object detection model.
camera_id: The camera id to be passed to OpenCV.
width: The width of the frame captured from the camera.
height: The height of the frame captured from the camera.
num_threads: The number of CPU threads to run the model.
enable_edgetpu: True/False whether the model is a EdgeTPU model.
"""
# Variables to calculate FPS
counter, fps = 0, 0
start_time = time.time()
# Start capturing video input from the camera
#cap = cv2.VideoCapture('/home/secret/Desktop/Design1/input_video.mp4')
cap = cv2.VideoCapture(1)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
# Visualization parameters
row_size = 20 # pixels
left_margin = 24 # pixels
text_color = (0, 0, 255) # red
font_size = 1
font_thickness = 1
fps_avg_frame_count = 10
# Initialize the object detection model
base_options = core.BaseOptions(
file_name=model, use_coral=enable_edgetpu, num_threads=num_threads)
detection_options = processor.DetectionOptions(
max_results=3, score_threshold=0.3)
options = vision.ObjectDetectorOptions(
base_options=base_options, detection_options=detection_options)
detector = vision.ObjectDetector.create_from_options(options)
trackers = []
# Continuously capture images from the camera and run inference
while cap.isOpened():
success, image = cap.read()
if not success:
sys.exit(
'ERROR: Unable to read from webcam. Please verify your webcam settings.'
)
counter += 1
image = cv2.flip(image, 1)
# Convert the image from BGR to RGB as required by the TFLite model.
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Create a TensorImage object from the RGB image.
input_tensor = vision.TensorImage.create_from_array(rgb_image)
# Run object detection estimation using the model.
detection_result = detector.detect(input_tensor)
# Draw keypoints and edges on input image
image = utils.visualize(image, detection_result)
# Calculate the FPS
if counter % fps_avg_frame_count == 0:
end_time = time.time()
fps = fps_avg_frame_count / (end_time - start_time)
start_time = time.time()
# Show the FPS
fps_text = 'FPS = {:.1f}'.format(fps)
text_location = (left_margin, row_size)
cv2.putText(image, fps_text, text_location, cv2.FONT_HERSHEY_PLAIN,
font_size, text_color, font_thickness)
# Initialize dictionaries to store object information
tracked_objects_info = {}
previous_frame_time = time.time() # Record the time of the previous frame
tracked_objects = []
# Inside the loop where you update object trackers and tracked objects
for i, bbox in enumerate(tracked_objects):
if i not in tracked_objects_info:
tracked_objects_info[i] = {
"previous_position": None,
"previous_time": None,
"speed": None,
}
# Calculate the center of the bounding box (assuming bbox is in the format [x, y, width, height])
x, y, w, h = bbox
center_x = x + w // 2
center_y = y + h // 2
# Calculate the Euclidean distance between the current position and the previous position
previous_position = tracked_objects_info[i]["previous_position"]
if previous_position is not None:
displacement = ((center_x - previous_position[0]) ** 2 + (center_y - previous_position[1]) ** 2) ** 0.5
else:
displacement = 0
# Calculate the time difference between the current frame and the previous frame
current_time = time.time()
previous_time = tracked_objects_info[i]["previous_time"]
if previous_time is not None:
time_difference = current_time - previous_time
else:
time_difference = 0
# Calculate the speed (in pixels per second)
if time_difference > 0:
speed = displacement / time_difference
else:
speed = 0
# Update the tracked object's information
tracked_objects_info[i]["previous_position"] = (center_x, center_y)
tracked_objects_info[i]["previous_time"] = current_time
tracked_objects_info[i]["speed"] = speed
# Display speed information on the frame
speed_text = f'Speed {i+1}: {speed:.2f} pixels/sec'
text_location = (left_margin, row_size * (i + 2))
cv2.putText(image, speed_text, text_location, cv2.FONT_HERSHEY_PLAIN,
font_size, text_color, font_thickness)
# Stop the program if the ESC key is pressed.
if cv2.waitKey(1) == 27:
break
cv2.imshow('object_detector', image)
cap.release()
cv2.destroyAllWindows()
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--model',
help='Path of the object detection model.',
required=False,
default='efficientdet_lite0.tflite')
parser.add_argument(
'--cameraId', help='Id of camera.', required=False, type=int, default=0)
parser.add_argument(
'--frameWidth',
help='Width of frame to capture from camera.',
required=False,
type=int,
default=640)
parser.add_argument(
'--frameHeight',
help='Height of frame to capture from camera.',
required=False,
type=int,
default=480)
parser.add_argument(
'--numThreads',
help='Number of CPU threads to run the model.',
required=False,
type=int,
default=4)
parser.add_argument(
'--enableEdgeTPU',
help='Whether to run the model on EdgeTPU.',
action='store_true',
required=False,
default=False)
args = parser.parse_args()
run(args.model, int(args.cameraId), args.frameWidth, args.frameHeight,
int(args.numThreads), bool(args.enableEdgeTPU))
if __name__ == '__main__':
i tried adding a simple detection system using tensorflow lite but it does not seem to appear in my screen
# Initialize dictionaries to store object information
tracked_objects_info = {}
previous_frame_time = time.time() # Record the time of the previous frame
tracked_objects = []
# Inside the loop where you update object trackers and tracked objects
for i, bbox in enumerate(tracked_objects):
if i not in tracked_objects_info:
tracked_objects_info[i] = {
"previous_position": None,
"previous_time": None,
"speed": None,
}
# Calculate the center of the bounding box (assuming bbox is in the format [x, y, width, height])
x, y, w, h = bbox
center_x = x + w // 2
center_y = y + h // 2
# Calculate the Euclidean distance between the current position and the previous position
previous_position = tracked_objects_info[i]["previous_position"]
if previous_position is not None:
displacement = ((center_x - previous_position[0]) ** 2 + (center_y - previous_position[1]) ** 2) ** 0.5
else:
displacement = 0
# Calculate the time difference between the current frame and the previous frame
current_time = time.time()
previous_time = tracked_objects_info[i]["previous_time"]
if previous_time is not None:
time_difference = current_time - previous_time
else:
time_difference = 0
# Calculate the speed (in pixels per second)
if time_difference > 0:
speed = displacement / time_difference
else:
speed = 0
# Update the tracked object's information
tracked_objects_info[i]["previous_position"] = (center_x, center_y)
tracked_objects_info[i]["previous_time"] = current_time
tracked_objects_info[i]["speed"] = speed
# Display speed information on the frame
speed_text = f'Speed {i+1}: {speed:.2f} pixels/sec'
text_location = (left_margin, row_size * (i + 2))
cv2.putText(image, speed_text, text_location, cv2.FONT_HERSHEY_PLAIN,
font_size, text_color, font_thickness)