Beispiel #1
0
def main(_argv):
    # Preparing the gpu for use
    physical_devices = tf.config.list_physical_devices('GPU')
    if len(physical_devices) > 0:
        tf.config.experimental.set_memory_growth(physical_devices[0], True)

    # Choice of model, NN, used by Yolo. Load of weights and classes used.
    if FLAGS.tiny:
        yolo = YoloV3Tiny(classes=FLAGS.num_classes)
    else:
        yolo = YoloV3(classes=FLAGS.num_classes)

    yolo.load_weights(FLAGS.weights).expect_partial()
    logging.info('weights loaded')

    class_names = [c.strip() for c in open(FLAGS.classes).readlines()]
    logging.info('classes loaded')

    # Link is established with the server
    mySocket = bind2server()

    # Image processing
    try:
        ind = 0
        t_timelose = 0
        while os.path.exists(f'{FLAGS.image}/RGB/{ind}.jpg'):
            time_step = t.time()
            # Preparation of the images to be processed
            img_raw = tf.image.decode_image(open(f'{FLAGS.image}/RGB/{ind}.jpg', 'rb').read(), channels=3)
            img = tf.expand_dims(img_raw, 0)
            img = transform_images(img, FLAGS.size)
            imgDepths = cv2.imread(f'{FLAGS.image}/depths/{ind}.jpg')

            t1 = time.time()
            # Yolo results for the processed image
            boxes, scores, classes, nums = yolo(img)
            img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR)

            # Information processing
            listObjects = yoloT.get_objects(img, imgDepths, boxes, scores, classes, nums, class_names,
                                            FLAGS.x_resolution, FLAGS.y_resolution)
            if len(listObjects) > 0:
                orientationT.add_orientations(img, imgDepths, listObjects)
                msg = occlusionT.solve_occlusion(img, listObjects)
            else:
                msg = 'empty'
            t2 = time.time()
            logging.info('time: {}'.format(t2 - t1))

            # Sending the message
            mySocket.sendall(msg.encode())

            # Painting of the image processed by yolo
            cv2.imshow('output', img)
            if cv2.waitKey(1) == ord('q'):
                break

            # Wait for the message from the server, to process the following image
            mySocket.recv(1024)

            time_step = t.time() - time_step
            f_step = FLAGS.fps_record * time_step
            i_step = int(f_step)
            t_timelose += f_step - i_step
            i_timelose = int(t_timelose)
            t_timelose -= i_timelose
            ind += i_step + i_timelose
        mySocket.close()
    finally:
        mySocket.close()
Beispiel #2
0
import time, cv2
import numpy as np
from PIL import Image
from myTools.my_client import bind2server
import agent
'''
Main script, where messages are received from the server and agent actions are sent.
'''

if __name__ == '__main__':
    # Link is established with the server
    mySocket = bind2server()
    msg_size, channels, parameters_size = 8, 3, 29
    verbose = 1  # 0 = no output, 1 = only telemetry, 2 = all message and paint images

    # Message format [imageWidth, imageHeigth, numberofCameras, decimalAccuracy, throttle, speed, steer, brake, image]
    index = [4, 8, 12, 16, 20, 24, 28, 29]

    # Image processing
    try:
        mySocket.sendall("ready".encode())
        print("Established connection")
        while True:
            # Message reconstruction
            msg = mySocket.recv(msg_size)
            imageWidth = int.from_bytes(msg[:index[0]],
                                        byteorder='little',
                                        signed=False)
            imageHeigth = int.from_bytes(msg[index[0]:index[1]],
                                         byteorder='little',
                                         signed=False)