Skip to content

Instantly share code, notes, and snippets.

def drive(self, speed=__INITIAL_SPEED):
self.back_wheels.speed = speed
while self.camera.isOpened():
_, image_lane = self.camera.read()
image_objs = image_lane.copy()
image_objs = self.process_objects_on_road(image_objs)
image_lane = self.follow_lane(image_lane)
class ObjectsOnRoadProcessor(object):
"""
This class 1) detects what objects (namely traffic signs and people) are on the road
and 2) controls the car navigation (speed/steering) accordingly
"""
def __init__(self,
car=None,
speed_limit=40,
model='/home/pi/DeepPiCar/models/object_detection/data/model_result/road_signs_quantized_edgetpu.tflite',
class SpeedLimit(TrafficObject):
def __init__(self, speed_limit):
self.speed_limit = speed_limit
def set_car_state(self, car_state):
logging.debug('speed limit: set limit to %d' % self.speed_limit)
car_state['speed_limit'] = self.speed_limit
class RedTrafficLight(TrafficObject):
def set_car_state(self, car_state):
logging.debug('red light: stopping car')
car_state['speed'] = 0
class Pedestrian(TrafficObject):
def set_car_state(self, car_state):
logging.debug('pedestrian: stopping car')
car_state['speed'] = 0
class TrafficObject(object):
def set_car_state(self, car_state):
pass
@staticmethod
def is_close_by(obj, frame_height, min_height_pct=0.05):
# default: if a sign is 10% of the height of frame
obj_height = obj.bounding_box[1][1]-obj.bounding_box[0][1]
return obj_height / frame_height > min_height_pct
# creates the frozen inference graph in fine_tune_model
# there is an "Incomplete shape" message. but we can safely ignore that.
!python /content/models/research/object_detection/export_inference_graph.py \
--input_type=image_tensor \
--pipeline_config_path={pipeline_fname} \
--output_directory='{output_directory}' \
--trained_checkpoint_prefix='{last_model_path}'
# create the tensorflow lite graph
!python /content/models/research/object_detection/export_tflite_ssd_graph.py \
num_steps = 2000
num_eval_steps = 50
model_dir = '/content/gdrive/My Drive/Colab Notebooks/TransferLearning/Training'
pipeline_file = 'ssd_mobilenet_v2_quantized_300x300_coco.config'
!python /content/models/research/object_detection/model_main.py \
--pipeline_config_path={pipeline_fname} \
--model_dir='{model_dir}' \
--alsologtostderr \
--num_train_steps={num_steps} \
MODEL_FILE = MODEL + '.tar.gz'
DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
DEST_DIR = '/content/models/research/pretrained_model'
if not (os.path.exists(MODEL_FILE)):
urllib.request.urlretrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
tar = tarfile.open(MODEL_FILE)
tar.extractall()
tar.close()
repo_dir_path = '/content/DeepPiCar'
%cd {repo_dir_path}/models/object_detection
# Convert train folder annotation xml files to a single csv file,
# generate the `label_map.pbtxt` file to `data/` directory as well.
!python code/xml_to_csv.py -i data/images/train -o data/annotations/train_labels.csv -l data/annotations
# Convert test folder annotation xml files to a single csv.
!python code/xml_to_csv.py -i data/images/test -o data/annotations/test_labels.csv
# If you forked the repository, you can replace the link.
repo_url = 'https://github.com/dctian/DeepPiCar'
# Number of training steps.
num_steps = 1000 # 200000
# Number of evaluation steps.
num_eval_steps = 50
# model configs are from Model Zoo github: