# We set the scenario to be in manual driving, and everything else random (time, weather and location). # See deepgtav/messages.py to see what options are supported scenario = Scenario(drivingMode=-1) #manual driving # Send the Start request to DeepGTAV. Dataset is set as default, we only receive frames at 10Hz (320, 160) client.sendMessage(Start(scenario=scenario)) # Dummy agent model = Model() # Start listening for messages coming from DeepGTAV. We do it for 80 hours stoptime = time.time() + 80 * 3600 while time.time() < stoptime: try: # We receive a message as a Python dictionary message = client.recvMessage() print(message) # The frame is a numpy array that can we pass through a CNN for example image = frame2numpy(message['frame'], (320, 160)) commands = model.run(image) # We send the commands predicted by the agent back to DeepGTAV to control the vehicle client.sendMessage(Commands(commands[0], commands[1], commands[2])) except KeyboardInterrupt: break # We tell DeepGTAV to stop client.sendMessage(Stop()) client.close()
weathers = ["CLEAR", "EXTRASUNNY", "CLOUDS", "OVERCAST", "RAIN", "CLEARING", "THUNDER", "SMOG", "FOGGY", "XMAS", "SNOWLIGHT", "BLIZZARD", "NEUTRAL", "SNOW" ] hours = [0,4,8,12,16,20] ## CREATE A REAL DATASET WITH WEATHER CONDITIONS IN DIRECTORY. for weather in weathers: # Creates a new connection to DeepGTAV using the specified ip and port. # If desired, a dataset path and compression level can be set to store in memory all the data received in a gziped pickle file. for hour in hours: client = Client(ip=args.host, port=args.port, datasetPath=args.dataset_path, compressionLevel=9) infos = {} infos['weather'] = weather infos['hour'] = hour infos['time'] = int(time.time()) # Configures the information that we want DeepGTAV to generate and send to us. # See deepgtav/messages.py to see what options are supported dataset = Dataset(rate=4, frame=[1280,640], throttle=True, brake=True, steering=True, vehicles=True, peds=True, trafficSigns=True, reward=[15.0, 0.0], direction=None, speed=True, yawRate=True, location=True, time=True) # Send the Start request to DeepGTAV. scenario = Scenario(time=[hour,0],weather=weather,drivingMode=[786603,15.0]) # Driving style is set to normal, with a speed of 15.0 mph. All other scenario options are random. client.sendMessage(Start(dataset=dataset,scenario=scenario)) stoptime = time.time() + 2*60 while time.time() < stoptime: try: message = client.recvMessage(infos) except KeyboardInterrupt: # We tell DeepGTAV to stop break # We tell DeepGTAV to stop client.sendMessage(Stop()) client.close()
port=args.port, datasetPath=args.dataset_path, compressionLevel=9, frame_capture_size=frame_capture_size, frame_save_size=frame_save_size) reset(weatherCount) print('Data to collect per weather:', data_to_collect_per_weather) print('Reset every:', reset_every, 'frames in each weather\n') print('{:>15} | {:^16} | {:^21} | {:^8} | {}'.format( "Weather", "Progress", "[str, thtl, brk]", "Speed", "Direction")) while True: # Main loop try: # Message recieved as a Python dictionary message = client.recvMessage(pre_count + 1 >= 0) if message is None: print('Message Error') continue if pre_count < 0: pre_count += 1 if pre_count == 0: count += 1 no_of_images += 1 new_location = message['location'] print( '{:>15}: {:>8d}/{:<8d} | [{: 3.2f}, {: 3.2f}, {: 3.2f}] | {:5.2f}/{:>2} | {} {:6.2f} | {:9}/{:< 4}' .format(weatherList[weatherCount], no_of_images, data_to_collect_per_weather, message['steering'],
class Data_Generator: def __init__(self, args_): self.__args = args_ self.__driving_mode = [786603, 55.0] self.__driving_vehicle = 'Blista' self.__starting_time = 5 self.__starting_weather = 4 self.__starting_location = [-2730, 2300] self.__frame_size = [650, 418] self.__client, self.__scenario, self.__dataset, self.__detection_pickleFile = None, None, None, None self.__total_written_images = 0 self.__images_to_capture = self.__args['images_to_capture'] self.__save_dir = self.__args['save_dir'] self.__target_shape = (200,88) self.__labels_csv = None self.__init_client() self.__init_scenairo() self.__prepare_datasets() self.__client.sendMessage(Start(scenario=self.__scenario, dataset=self.__dataset)) self.__old_location = [] def __build_env(self): self.__init_scenairo() self.__prepare_datasets(open_=False) self.__client.sendMessage(Config(scenario=self.__scenario, dataset=self.__dataset)) def __init_scenairo(self): self.__scenario = Scenario(drivingMode=self.__driving_mode, vehicle=self.__driving_vehicle, time=self.__starting_time, weather=self.__starting_weather, location=self.__starting_location) def __init_client(self): self.__client = Client( ip=self.__args['host'], port=self.__args['port']) def __prepare_datasets(self, open_=True, fps=20, throttle_=True, brake_=True, steering_=True, vehicles_=True, peds_=True, speed_=True, location_=True, detection_file_name_='detection1.pickle'): self.__dataset = Dataset(rate=fps, frame=self.__frame_size, throttle=throttle_, brake=brake_, steering=steering_, vehicles=vehicles_, time=True, peds=peds_, speed=speed_, location=location_) if open_: self.__detection_pickleFile = open(detection_file_name_, mode='wb') self.__labels_csv = open('labels.csv', 'wb') def __save_image(self, image): cv2.imwrite(self.__save_dir +str(self.__total_written_images)+'.png', image) def __process_image(self, image, up_crop=110, down_crop=40 ): return cv2.resize(image[up_crop:-down_crop, :], dsize=self.__target_shape) def __recv_message(self): message = self.__client.recvMessage() image = frame2numpy(message['frame'], (self.__frame_size[0], self.__frame_size[1])) detect = dict() detect['peds'] = message['peds'] detect['vehicles'] = message['vehicles'] recv_labels = [message['steering'], message['throttle'], message['brake'], message['speed'], message['time']] return message, self.__process_image(image= image), recv_labels, detect def __close_all(self): self.__detection_pickleFile.close() self.__labels_csv.close() self.__client.sendMessage(Stop()) self.__client.close() def __dump_all(self, labels, detect_list): np.savetxt(self.__labels_csv, labels, delimiter=',') for object_ in detect_list: pickle.dump(object_, self.__detection_pickleFile) def generate_dataset(self): reset_images = 0 counter = 0 reset_threshold = 6000 labels = np.zeros((1000,5), dtype=np.float32) detect_list = [] for i in tqdm(np.arange(self.__images_to_capture - self.__total_written_images)): try: message, image, labels[counter], detect = self.__recv_message() self.__save_image(image) detect_list.append(detect) self.__total_written_images += 1 reset_images += 1 counter += 1 if ((counter) % 1000 == 0 and counter > 0) or ((counter) % 1000 == 0 and counter > 0 and reset_images >= reset_threshold): if (counter) % 1000 == 0 or reset_images >= reset_threshold: # print ('Saved : ',total_images) self.__dump_all(labels, detect_list) else: self.__total_written_images -= 1000 self.__build_env() if reset_images >= reset_threshold: reset_images = 0 self.__build_env() counter = 0 detect_list = [] # We send the commands predicted by the agent back to DeepGTAV to control the vehicle except KeyboardInterrupt: self.__close_all() break self.__close_all() print(self.__total_written_images)
class Worker(QObject): """ Must derive from QObject in order to emit signals, connect slots to other signals, and operate in a QThread. """ sig_step = pyqtSignal( int, str ) # worker id, step description: emitted every step through work() loop sig_done = pyqtSignal(int) # worker id: emitted at end of work() sig_msg = pyqtSignal(str) # message to be shown to user sig_image = pyqtSignal(list) def __init__(self, id: int, args): super().__init__() self.__id = id self.__abort = False self.args = args @pyqtSlot() def work(self): """ Pretend this worker method does work that takes a long time. During this time, the thread's event loop is blocked, except if the application's processEvents() is called: this gives every thread (incl. main) a chance to process events, which in this sample means processing signals received from GUI (such as abort). """ thread_name = QThread.currentThread().objectName() thread_id = int( QThread.currentThreadId()) # cast to int() is necessary self.sig_msg.emit('Running worker #{} from thread "{}" (#{})'.format( self.__id, thread_name, thread_id)) # Creates a new connection to DeepGTAV using the specified ip and port. # If desired, a dataset path and compression level can be set to store in memory all the data received in a gziped pickle file. # We don't want to save a dataset in this case self.client = Client(ip=self.args.host, port=self.args.port) # self.client = Client(ip="127.0.0.1", port=8000) # We set the scenario to be in manual driving, and everything else random (time, weather and location). # See deepgtav/messages.py to see what options are supported scenario = Scenario(drivingMode=-1) #manual driving # Send the Start request to DeepGTAV. Dataset is set as default, we only receive frames at 10Hz (320, 160) self.client.sendMessage(Start(scenario=scenario)) # Dummy agent model = Model() # Start listening for messages coming from DeepGTAV. We do it for 80 hours stoptime = time.time() + 80 * 3600 while (time.time() < stoptime and (not self.__abort)): # We receive a message as a Python dictionary app.processEvents() message = self.client.recvMessage() # The frame is a numpy array that can we pass through a CNN for example image = frame2numpy(message['frame'], (320, 160)) commands = model.run(image) self.sig_step.emit(self.__id, 'step ' + str(time.time())) self.sig_image.emit(image.tolist()) # We send the commands predicted by the agent back to DeepGTAV to control the vehicle self.client.sendMessage( Commands(commands[0], commands[1], commands[2])) # We tell DeepGTAV to stop self.client.sendMessage(Stop()) self.client.close() self.sig_done.emit(self.__id) def abort(self): self.sig_msg.emit('Worker #{} notified to abort'.format(self.__id)) self.__abort = True
def main(): global client, scenario client = Client(ip='localhost', port=8000) # Default interface scenario = Scenario( weather='EXTRASUNNY', vehicle='blista', time=[12, 0], drivingMode=-1, location=[-2583.6708984375, 3501.88232421875, 12.7711820602417]) client.sendMessage(Start(scenario=scenario, dataset=dataset)) print("load deepGTAV successfully! \nbegin") # load yolo v3 classes = yolo_util.read_coco_names('./files/coco/coco.names') num_classes = len(classes) input_tensor, output_tensors = yolo_util.read_pb_return_tensors( tf.get_default_graph(), "./files/trained_models/yolov3.pb", ["Placeholder:0", "concat_9:0", "mul_6:0"]) print("load yolo v3 successfully!") with tf.Session() as sess: model = load_model("files/trained_models/main_model.h5") print("load main_model successfully!") while True: fo = open(config_position, "r") # 配置1 txt = fo.read() fo.close() if txt == '0': set_gamepad(-1, -1, 0) time.sleep(0.7) print('=====================end=====================') exit(0) elif txt == '1': message = client.recvMessage() frame = frame2numpy(message['frame'], (CAP_IMG_W, CAP_IMG_H)) image_obj = Image.fromarray(frame) speed = message['speed'] boxes, scores = sess.run(output_tensors, feed_dict={ input_tensor: np.expand_dims( yolo_img_process(frame), axis=0) }) boxes, scores, labels = yolo_util.cpu_nms(boxes, scores, num_classes, score_thresh=0.4, iou_thresh=0.1) image, warning = yolo_util.draw_boxes(image_obj, boxes, scores, labels, classes, (IMAGE_H, IMAGE_W), show=False) control, throttle, breakk = drive(model=model, image=frame, speed=speed, warning=warning) print(warning) set_gamepad(control, throttle, breakk)