Beispiel #1
0
    def ctrl_z(self):

        print '\n< < < CTRL-Z MODE ACTIVATED! > > >'

        last_dir = dir_log[-1]

        # Forward -> Reverse
        if last_dir == 'Forward':
            car.reverse(200)
            car.pause(500)
            print '< REVERSE >'

        # Left -> Reverse-Left
        elif last_dir == 'Left':
            car.left(300)
            car.reverse_left(250)
            car.left(700)
            car.pause(500)
            print '< REVERSE-LEFT >'

        # Right -> Reverse-Right
        elif last_dir == 'Right':
            car.right(300)
            car.reverse_right(250)
            car.right(700)
            car.pause(500)
            print '< REVERSE-RIGHT >'

        return
Beispiel #2
0
    def make_decision(self):

        # Calculate the percent_difference to decide whether to act on 'difference'

        print 'make_decision1'

        calc_difference = np.sum(self.difference)
        max_difference = np.sum(255 * self.difference.shape)
        percent_difference = float(calc_difference) / max_difference

        print 'make_decision2'

        # If percent_difference is below ctrlz_thresh (i.e. the two images are < 5% different), then commence ctrl-z protocol.
        if percent_difference <= self.ctrlz_thresh:

            # Activate ctrl-z mode
            print '< < < CTRL-Z MODE ACTIVATED! > > >'

            # Get the last 10 directions executed from the log (or however many you specified for self.ctrlz_iter)
            recent_dirs = dir_log[-self.ctrlz_iter:]
            recent_dirs_reversed = recent_dirs.reverse()

            for each in recent_dirs_reversed:

                # Forward -> Reverse
                if each == 'Forward':
                    car.reverse(100)
                    car.pause(300)
                    print '< REVERSE >'

                # Left -> Right
                elif each == 'Left':
                    car.right(300)
                    car.reverse_right(200)
                    car.right(700)
                    car.pause(200)
                    print '< REVERSE-RIGHT >'

                # FORWARD-RIGHT
                elif each == 'Right':
                    car.left(300)
                    car.reverse_left(200)
                    car.left(700)
                    car.pause(200)
                    print '< REVERSE-LEFT >'
Beispiel #3
0
    def steer(self):

        while self.send_inst:
            for event in pygame.event.get():
                if event.type == KEYDOWN:
                    key_input = pygame.key.get_pressed()

                    if key_input[pygame.K_UP]:
                        car.forward(self.duration)

                    elif key_input[pygame.K_DOWN]:
                        car.reverse(self.duration)

                    elif key_input[pygame.K_RIGHT]:
                        car.forward_right(self.duration)
                        car.right(700)

                    elif key_input[pygame.K_LEFT]:
                        car.forward_left(self.duration)
                        car.left(700)

                    # These combination key press events don't work for some reason...
                    elif key_input[pygame.K_DOWN] and key_input[
                            pygame.K_RIGHT]:
                        car.reverse_right(self.duration)
                        car.right(700)

                    elif key_input[pygame.K_DOWN] and key_input[pygame.K_LEFT]:
                        car.reverse_left(self.duration)
                        car.left(700)

                    # exit
                    elif key_input[pygame.K_x] or key_input[pygame.K_q]:
                        print 'Exit'
                        self.send_inst = False
                        # self.ser.write('00000000\n')
                        # self.ser.close()
                        break
Beispiel #4
0
import car
''' Move forward for 1000ms (1s) and pause. '''
car.forward(1000)
car.pause(1000)

car.forward(1000)
car.pause(1000)
''' Move backward for 1000ms (1s) and pause. '''
car.reverse(1000)
car.pause(1000)

car.reverse(1000)
car.pause(500)
Beispiel #5
0
import car

print(car.forward())
print(car.reverse())
Beispiel #6
0
    def collect_image(self):

        total_frame = 0

        # collect images for training
        print 'Start collecting NEGATIVE images...'
        e1 = cv2.getTickCount()

        # stream video frames one by one
        try:
            stream_bytes = ' '
            frame = 1
            while self.send_inst:
                stream_bytes += self.connection.read(1024)
                first = stream_bytes.find(
                    '\xff\xd8'
                )  # ? What is this string and where did it come from?
                last = stream_bytes.find('\xff\xd9')
                if first != -1 and last != -1:
                    jpg = stream_bytes[first:last + 2]
                    stream_bytes = stream_bytes[last + 2:]

                    # image is a np matrix.
                    image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),
                                         cv2.IMREAD_GRAYSCALE)
                    image_framecount = cv2.imdecode(
                        np.fromstring(jpg, dtype=np.uint8),
                        cv2.IMREAD_GRAYSCALE)

                    # overlay click counts: cv2.putText(clicks_*)
                    cv2.putText(image_framecount,
                                "Total frames: {}".format(total_frame),
                                (10, 30), cv2.FONT_HERSHEY_SIMPLEX, .45,
                                (255, 255, 0), 1)

                    # save streamed images
                    cv2.imwrite(
                        'negative_images/frame{:>05}.jpg'.format(frame), image)

                    # Display feeds on host (laptop)
                    cv2.imshow('image', image)
                    cv2.imshow('image_framecount', image_framecount)

                    frame += 1
                    total_frame += 1

                    # get input from human driver
                    for event in pygame.event.get():
                        if event.type == KEYDOWN:  #''' 'KEYDOWN' : Does this just indicate when a key is pressed DOWN? '''
                            key_input = pygame.key.get_pressed()

                            # FORWARD
                            if key_input[pygame.K_UP]:
                                car.forward(300)

                            # FORWARD_RIGHT
                            elif key_input[pygame.K_RIGHT]:
                                car.right(300)
                                car.forward_right(400)
                                car.right(700)

                            # FORWARD_LEFT
                            elif key_input[pygame.K_LEFT]:
                                car.left(300)
                                car.forward_left(400)
                                car.left(700)

                            # REVERSE
                            elif key_input[pygame.K_DOWN]:
                                car.reverse(300)

                            # Quit
                            elif key_input[pygame.K_x] or key_input[
                                    pygame.K_q]:
                                print 'exit'
                                self.send_inst = False
                                break

                        elif event.type == pygame.KEYUP:  #'''Key is lifted UP?'''
                            break

            e2 = cv2.getTickCount()
            # calculate streaming duration
            time0 = (e2 - e1) / cv2.getTickFrequency()
            print 'Streaming duration:', time0

            print 'Total NEGATIVE frames collected:', total_frame

        finally:
            self.connection.close()
            self.server_socket.close
            print 'Connection closed'
            print 'Socket closed'
Beispiel #7
0
import car

while True:

    car.forward(2000)
    car.pause(1000)
    print ''

    car.reverse(2000)
    car.pause(1000)
    print ''

    car.left(3000)
    car.pause(1000)
    print ''

    car.right(3000)
    car.pause(1000)
    print ''
Beispiel #8
0
    def collect_images(self):

        saved_frame = 0
        total_frame = 0

        clicks_forward = 0
        clicks_forward_left = 0
        clicks_forward_right = 0

        print('Start collecting images...')

        label_array = np.zeros((1, 3), 'float')

        # Stream video frames one by one.
        try:
            stream_bytes = ' '
            frame = 0
            while self.send_inst:
                stream_bytes += self.connection.read(1024)
                first = stream_bytes.find(
                    '\xff\xd8'
                )  # ? What is this string and where did it come from?
                last = stream_bytes.find('\xff\xd9')
                if first != -1 and last != -1:
                    jpg = stream_bytes[first:last + 2]
                    stream_bytes = stream_bytes[last + 2:]

                    # 'image' (original, in grayscale)
                    image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),
                                         cv2.IMREAD_GRAYSCALE)

                    # Overlay click counts: cv2.putText(clicks_*)
                    clicks_total = clicks_forward + clicks_forward_left + clicks_forward_right
                    cv2.putText(
                        image, "FW: {}, LT: {}, RT: {}, TOTAL: {}".format(
                            clicks_forward, clicks_forward_left,
                            clicks_forward_right, clicks_total), (10, 30),
                        cv2.FONT_HERSHEY_SIMPLEX, .45, (255, 255, 0), 1)

                    # Display feeds on host (laptop)
                    cv2.imshow('image', image)

                    # Get input from human driver
                    for event in pygame.event.get():
                        if event.type == KEYDOWN:
                            key_input = pygame.key.get_pressed()

                            # FORWARD
                            if key_input[pygame.K_UP]:
                                # save streamed images
                                cv2.imwrite(
                                    'training_images/frame{:>05}.jpg'.format(
                                        frame), image)
                                label_array = np.vstack(
                                    (label_array,
                                     self.k[2]))  # self.k[2] = [ 0.,  0.,  1.]
                                car.forward(200)
                                clicks_forward += 1
                                frame += 1
                                total_frame += 1
                                saved_frame += 1

                            # FORWARD_RIGHT
                            elif key_input[pygame.K_RIGHT]:
                                # save streamed images
                                cv2.imwrite(
                                    'training_images/frame{:>05}.jpg'.format(
                                        frame), image)
                                label_array = np.vstack(
                                    (label_array,
                                     self.k[1]))  # self.k[1] = [ 0.,  1.,  0.]
                                car.right(300)
                                car.forward_right(300)
                                car.right(700)
                                clicks_forward_right += 1
                                frame += 1
                                total_frame += 1
                                saved_frame += 1

                            # FORWARD_LEFT
                            elif key_input[pygame.K_LEFT]:
                                # save streamed images
                                cv2.imwrite(
                                    'training_images/frame{:>05}.jpg'.format(
                                        frame), image)
                                label_array = np.vstack(
                                    (label_array,
                                     self.k[0]))  # self.k[0] = [ 1.,  0.,  0.]
                                car.left(300)
                                car.forward_left(300)
                                car.left(700)
                                clicks_forward_left += 1
                                frame += 1
                                total_frame += 1
                                saved_frame += 1

                            # REVERSE; not saving images for this
                            elif key_input[pygame.K_DOWN]:
                                car.reverse(200)

                            elif key_input[pygame.K_x] or key_input[
                                    pygame.K_q]:
                                print('EXIT')
                                self.send_inst = False
                                break

                        elif event.type == pygame.KEYUP:  #'''Key is lifted UP?'''
                            break

            train_labels = label_array[1:, :]
            np.savez('training_images/label_array_ORIGINALS.npz',
                     train_labels=train_labels)

            with open('./logs/log_img_collect.txt', 'a') as f:
                f.write('Date: ' + time.strftime('%x') + '\n')
                f.write('Time: ' + time.strftime('%X') + '\n')
                f.write('Total frames: ' + str(total_frame) + '\n')
                f.write('Saved frames: ' + str(saved_frame) + '\n')
                f.write('Dropped frames: ' + str(total_frame - saved_frame) +
                        '\n')
                f.write('Forward clicks: ' + str(clicks_forward) + '\n')
                f.write('Forward-left clicks: ' + str(clicks_forward_left) +
                        '\n')
                f.write('Forward-right clicks: ' + str(clicks_forward_right) +
                        '\n')
                f.write('-----------------------------\n')

            print('Forward clicks: '), clicks_forward
            print('Forward-left clicks: '), clicks_forward_left
            print('Forward-right clicks: '), clicks_forward_right

            print('Total frame:'), total_frame
            print('Saved frame:'), saved_frame
            print('Dropped frame'), total_frame - saved_frame

        finally:
            self.connection.close()
            self.server_socket.close
            print('Connection closed')
            print('Socket closed')