From f3da454ee2775c8b3843631a092d346b0a7fef78 Mon Sep 17 00:00:00 2001 From: JuliusHendrix Date: Sun, 8 May 2022 14:33:04 +0200 Subject: [PATCH] * adjusting object detection parameters --- Main/rectangle.py | 14 ++++++++++---- Main/straight.py | 14 ++++++++++---- ObjectDetection/object_detection.py | 20 +++++++++++++++++--- RobotControl/bouncebot_control.py | 16 ++++++++++++---- 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/Main/rectangle.py b/Main/rectangle.py index e56dfed..55fece4 100644 --- a/Main/rectangle.py +++ b/Main/rectangle.py @@ -38,12 +38,13 @@ def rectangle(mode, rendering): state = 'straight' # parameters - time_to_drive_2_meters = distance_to_time(200) show_video = True if rendering == 1 else False # distances to travel (in cm) distances = [distance_to_time(200), distance_to_time(100), distance_to_time(200), distance_to_time(100)] + time_correction = 0 turns = 0 + has_dodged = False # initialize timer timer = Timer() @@ -51,24 +52,29 @@ def rectangle(mode, rendering): try: bb.move(1) + bb.set_camera_top_servo_angle(-20) for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): print(f'{state = }') - if mode == 2: - obj, direction = detector.check_for_object(frame=frame.array, distance_to_dodge=25, + if mode == 2 and not has_dodged: + obj, direction = detector.check_for_object(frame=frame.array, distance_to_dodge=200, show_video=show_video) if obj is not None: print("OBJECT DETECTED!") timer.pause_timer() bb.move(0) bb.around_obstacle(direction=direction) + has_dodged = True + time_correction += distance_to_time(80) timer.resume_timer() + bb.move(1) if state == 'straight': - if timer.get_timer() > distances[turns]: + if timer.get_timer() > distances[turns] - time_correction: if turns < 4: state = 'turn' turns += 1 + time_correction = 0 else: bb.move(0) break # back at start diff --git a/Main/straight.py b/Main/straight.py index 1587eef..9b3ca2e 100644 --- a/Main/straight.py +++ b/Main/straight.py @@ -32,14 +32,16 @@ def straight(mode, rendering): bb = BounceBot() # initialize object detector - detector = ObjectDetector() + detector = ObjectDetector(threshold=0.4) # initialize state state = 'straight' has_turned = False + has_dodged = False # parameters time_to_drive_2_meters = distance_to_time(200) + time_correction = 0 show_video = True if rendering == 1 else False # initialize timer @@ -48,21 +50,25 @@ def straight(mode, rendering): try: bb.move(1) + bb.set_camera_top_servo_angle(-20) for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): print(f'{state = }') - if mode == 2: - obj, direction = detector.check_for_object(frame=frame.array, distance_to_dodge=25, + if mode == 2 and not has_dodged: + obj, direction = detector.check_for_object(frame=frame.array, distance_to_dodge=200, show_video=show_video) if obj is not None: print("OBJECT DETECTED!") timer.pause_timer() bb.move(0) bb.around_obstacle(direction=direction) + has_dodged = True + time_correction += distance_to_time(80) timer.resume_timer() + bb.move(1) if state == 'straight': - if timer.get_timer() > time_to_drive_2_meters: + if timer.get_timer() > time_to_drive_2_meters - time_correction: if not has_turned: state = 'turn' has_turned = True diff --git a/ObjectDetection/object_detection.py b/ObjectDetection/object_detection.py index b7b2937..d52a9a2 100644 --- a/ObjectDetection/object_detection.py +++ b/ObjectDetection/object_detection.py @@ -8,7 +8,7 @@ class ObjectDetector: - def __init__(self, object_names=["CUP", "BOOK", "BOTTLE"], threshold=.45): + def __init__(self, object_names=["CUP", "BOOK", "BOTTLE", "BED"], threshold=.45): self.class_names = [] file_path = os.path.realpath(__file__) @@ -35,9 +35,10 @@ def check_for_object(self, frame, distance_to_dodge=320, show_video=False): for classId, confidence, box in zip(classIds.flatten(), confs.flatten(), bbox): if self.class_names[classId - 1].upper() in self.object_names: color = (0, 255, 0) - found_objects.append((classId, confidence, box)) + # found_objects.append((classId, confidence, box)) else: color = (255, 0, 0) + found_objects.append((classId, confidence, box)) cv2.rectangle(frame, box, color=color, thickness=2) cv2.putText(frame, self.class_names[classId - 1].upper(), (box[0] + 10, box[1] + 30), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2) @@ -49,7 +50,20 @@ def check_for_object(self, frame, distance_to_dodge=320, show_video=False): dodge_directions = [] for object in found_objects: lower_bounding_line = object[2][1] + object[2][3] - if lower_bounding_line > distance_to_dodge: + upper_bounding_line = object[2][1] + + width = object[2][2] + height = object[2][3] + + if lower_bounding_line > distance_to_dodge\ + and 250 > width > 120 \ + and 250 > height > 120: + + print(f'{lower_bounding_line = }') + print(f'{upper_bounding_line = }') + print(f'{width = }') + print(f'{height = }') + close_objects.append(object) left_edge = object[2][0] diff --git a/RobotControl/bouncebot_control.py b/RobotControl/bouncebot_control.py index 8412542..db20e1d 100644 --- a/RobotControl/bouncebot_control.py +++ b/RobotControl/bouncebot_control.py @@ -15,7 +15,6 @@ def __init__(self): super().__init__() # initialize servo control self.turret_servo = Servo(PWM('P3')) - self.turret_servo.angle(0) # initialize solenoid control self.turret_solenoid_1 = Pin("D0") @@ -37,9 +36,15 @@ def __init__(self): self.camera_servo2_angular_speed = 3 # deg / frame self.turret_servo_current_angle = 0 - self.turret_servo_max_angle = 45 + self.turret_servo_max_angle = 35 self.turret_servo_angular_speed = 3 # deg / frame + # set all servo angles to 0 + self.set_camera_bottom_servo_angle(0) + self.set_camera_top_servo_angle(0) + self.set_turret_servo_angle(0) + self.set_direction_servo_angle(0) + def reset_servo_angles(self): self.set_camera_bottom_servo_angle(0) self.set_camera_top_servo_angle(0) @@ -155,6 +160,7 @@ def turn_90(self, direction='right'): time.sleep(1.4) self.move(0) self.set_direction_servo_angle(0) + time.sleep(0.5) def turn_270(self, direction='right'): """ @@ -188,6 +194,7 @@ def turn_270(self, direction='right'): self.move(1) time.sleep(0.2) self.move(0) + time.sleep(0.5) def u_turn(self, direction='right'): if direction == 'right': @@ -202,13 +209,14 @@ def u_turn(self, direction='right'): self.set_direction_servo_angle(angle) time.sleep(1) self.move(1) - time.sleep(3.9) + time.sleep(4.0) self.move(0) self.set_direction_servo_angle(0) time.sleep(0.5) self.move(1) - time.sleep(0.4) + time.sleep(0.6) self.move(0) + time.sleep(0.5) def around_obstacle(self, direction='right'): """