From 43b74ae43b16791febba231b8881511833dc8c26 Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 2 Aug 2018 16:31:45 +0800 Subject: [PATCH 1/7] add saving video --- .gitignore | 1 + config.py | 14 ++++++++++++++ dataset | 32 +++++++++++++++++++++----------- 3 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 config.py diff --git a/.gitignore b/.gitignore index 6fb1f34..6795c90 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.pyc *.h5 +.idea/ diff --git a/config.py b/config.py new file mode 100644 index 0000000..fe6ac4d --- /dev/null +++ b/config.py @@ -0,0 +1,14 @@ +import os + +FPS = 30 +FRAME = [320, 160] + +base_path = os.getcwd() +output_file_dir = 'video/' +output_file_name = 'capturedVideo.avi' + +PATH = os.path.join(base_path, output_file_dir, output_file_name) + + + + diff --git a/dataset b/dataset index df1324d..9c0479c 100644 --- a/dataset +++ b/dataset @@ -3,11 +3,16 @@ from deepgtav.messages import Start, Stop, Dataset, frame2numpy, Scenario from deepgtav.client import Client +from config import * import argparse import time import cv2 + +# fourcc = cv2.cv.CV_FOURCC('M', 'J', 'P', 'G') +videoWriter = cv2.VideoWriter(PATH, -1, FPS, (FRAME[0], FRAME[1])) + # Stores a dataset file with data coming from DeepGTAV if __name__ == '__main__': parser = argparse.ArgumentParser(description=None) @@ -19,28 +24,33 @@ if __name__ == '__main__': # 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. client = Client(ip=args.host, port=args.port, datasetPath=args.dataset_path, compressionLevel=9) - + # 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=30, frame=[320,160], throttle=True, brake=True, steering=True, vehicles=True, peds=True, reward=[15.0, 0.0], direction=None, speed=True, yawRate=True, location=True, time=True) + dataset = Dataset(rate=FPS, frame=FRAME, throttle=True, brake=True, steering=True, vehicles=True, peds=True, + reward=[15.0, 0.0], direction=None, speed=True, yawRate=True, location=True, time=True) # Send the Start request to DeepGTAV. - scenario = Scenario(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)) + scenario = Scenario(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)) # Start listening for messages coming from DeepGTAV. We do it for 80 hours - stoptime = time.time() + 80*3600 + stoptime = time.time() + 80 * 3600 while time.time() < stoptime: try: # We receive a message as a Python dictionary - message = client.recvMessage() - - # The frame is a numpy array and can be displayed using OpenCV or similar - # image = frame2numpy(message['frame'], (320,160)) - # cv2.imshow('img',image) + message = client.recvMessage() + + # The frame is a numpy array and can be displayed using OpenCV or similar + #TODO: save frames into video + image = frame2numpy(message['frame'], (FRAME[0], FRAME[1])) + videoWriter.write(image) + cv2.imshow('img',image) # cv2.waitKey(-1) except KeyboardInterrupt: break - + + videoWriter.release() # We tell DeepGTAV to stop client.sendMessage(Stop()) client.close() From ceab4e54686a850fa693b7d8059a87ab8cf91764 Mon Sep 17 00:00:00 2001 From: ShanwNew Date: Thu, 2 Aug 2018 17:36:39 +0800 Subject: [PATCH 2/7] check and remove file before open --- config.py | 13 ++++++++++++- dataset | 4 ++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/config.py b/config.py index fe6ac4d..1f19da7 100644 --- a/config.py +++ b/config.py @@ -6,8 +6,19 @@ base_path = os.getcwd() output_file_dir = 'video/' output_file_name = 'capturedVideo.avi' +DIR_PATH = os.path.join(base_path, output_file_dir) -PATH = os.path.join(base_path, output_file_dir, output_file_name) +if os.path.exists(DIR_PATH): + temp_file_path = os.path.join(DIR_PATH, output_file_name) + if os.path.exists(temp_file_path): + os.remove(temp_file_path) + PATH = temp_file_path +else: + os.mkdir(DIR_PATH) + temp_file_path = os.path.join(DIR_PATH, output_file_name) + if os.path.exists(temp_file_path): + os.remove(temp_file_path) + PATH = temp_file_path diff --git a/dataset b/dataset index 9c0479c..7fa6bfc 100644 --- a/dataset +++ b/dataset @@ -35,7 +35,7 @@ if __name__ == '__main__': client.sendMessage(Start(dataset=dataset, scenario=scenario)) # Start listening for messages coming from DeepGTAV. We do it for 80 hours - stoptime = time.time() + 80 * 3600 + stoptime = time.time() + 10 * 60 while time.time() < stoptime: try: # We receive a message as a Python dictionary @@ -45,7 +45,7 @@ if __name__ == '__main__': #TODO: save frames into video image = frame2numpy(message['frame'], (FRAME[0], FRAME[1])) videoWriter.write(image) - cv2.imshow('img',image) + # cv2.imshow('img',image) # cv2.waitKey(-1) except KeyboardInterrupt: break From ef9d6f35951e549fedef82d886f1766ac7a75126 Mon Sep 17 00:00:00 2001 From: ShawnNew Date: Thu, 2 Aug 2018 17:40:12 +0800 Subject: [PATCH 3/7] check output file before open --- .gitignore | 1 + config.py | 13 ++++++++++++- dataset | 6 +++--- 3 files changed, 16 insertions(+), 4 deletions(-) mode change 100644 => 100755 config.py mode change 100644 => 100755 dataset diff --git a/.gitignore b/.gitignore index 6795c90..728dfbf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.pyc *.h5 .idea/ +*.pz diff --git a/config.py b/config.py old mode 100644 new mode 100755 index fe6ac4d..1f19da7 --- a/config.py +++ b/config.py @@ -6,8 +6,19 @@ base_path = os.getcwd() output_file_dir = 'video/' output_file_name = 'capturedVideo.avi' +DIR_PATH = os.path.join(base_path, output_file_dir) -PATH = os.path.join(base_path, output_file_dir, output_file_name) +if os.path.exists(DIR_PATH): + temp_file_path = os.path.join(DIR_PATH, output_file_name) + if os.path.exists(temp_file_path): + os.remove(temp_file_path) + PATH = temp_file_path +else: + os.mkdir(DIR_PATH) + temp_file_path = os.path.join(DIR_PATH, output_file_name) + if os.path.exists(temp_file_path): + os.remove(temp_file_path) + PATH = temp_file_path diff --git a/dataset b/dataset old mode 100644 new mode 100755 index 9c0479c..ac52662 --- a/dataset +++ b/dataset @@ -16,7 +16,7 @@ videoWriter = cv2.VideoWriter(PATH, -1, FPS, (FRAME[0], FRAME[1])) # Stores a dataset file with data coming from DeepGTAV if __name__ == '__main__': parser = argparse.ArgumentParser(description=None) - parser.add_argument('-l', '--host', default='localhost', help='The IP where DeepGTAV is running') + parser.add_argument('-l', '--host', default='192.168.137.1', help='The IP where DeepGTAV is running') parser.add_argument('-p', '--port', default=8000, help='The port where DeepGTAV is running') parser.add_argument('-d', '--dataset_path', default='dataset.pz', help='Place to store the dataset') args = parser.parse_args() @@ -35,7 +35,7 @@ if __name__ == '__main__': client.sendMessage(Start(dataset=dataset, scenario=scenario)) # Start listening for messages coming from DeepGTAV. We do it for 80 hours - stoptime = time.time() + 80 * 3600 + stoptime = time.time() + 10 * 60 while time.time() < stoptime: try: # We receive a message as a Python dictionary @@ -45,7 +45,7 @@ if __name__ == '__main__': #TODO: save frames into video image = frame2numpy(message['frame'], (FRAME[0], FRAME[1])) videoWriter.write(image) - cv2.imshow('img',image) + # cv2.imshow('img',image) # cv2.waitKey(-1) except KeyboardInterrupt: break From 3bf1b3ddbf7498f1e34bcbf26242107038ea0cf8 Mon Sep 17 00:00:00 2001 From: ShawnNew Date: Fri, 3 Aug 2018 17:22:21 +0800 Subject: [PATCH 4/7] read data from .pz file --- config.py | 4 +--- dataset | 8 ++++---- read.py | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 read.py diff --git a/config.py b/config.py index 1f19da7..7a76174 100755 --- a/config.py +++ b/config.py @@ -7,6 +7,7 @@ output_file_dir = 'video/' output_file_name = 'capturedVideo.avi' DIR_PATH = os.path.join(base_path, output_file_dir) +GFILE_PATH = os.path.join(base_path, 'dataset.pz') if os.path.exists(DIR_PATH): temp_file_path = os.path.join(DIR_PATH, output_file_name) @@ -20,6 +21,3 @@ os.remove(temp_file_path) PATH = temp_file_path - - - diff --git a/dataset b/dataset index ac52662..f741672 100755 --- a/dataset +++ b/dataset @@ -11,14 +11,14 @@ import cv2 # fourcc = cv2.cv.CV_FOURCC('M', 'J', 'P', 'G') -videoWriter = cv2.VideoWriter(PATH, -1, FPS, (FRAME[0], FRAME[1])) +# videoWriter = cv2.VideoWriter(PATH, -1, FPS, (FRAME[0], FRAME[1])) # Stores a dataset file with data coming from DeepGTAV if __name__ == '__main__': parser = argparse.ArgumentParser(description=None) parser.add_argument('-l', '--host', default='192.168.137.1', help='The IP where DeepGTAV is running') parser.add_argument('-p', '--port', default=8000, help='The port where DeepGTAV is running') - parser.add_argument('-d', '--dataset_path', default='dataset.pz', help='Place to store the dataset') + parser.add_argument('-d', '--dataset_path', default=GFILE_PATH, help='Place to store the dataset') args = parser.parse_args() # Creates a new connection to DeepGTAV using the specified ip and port. @@ -44,13 +44,13 @@ if __name__ == '__main__': # The frame is a numpy array and can be displayed using OpenCV or similar #TODO: save frames into video image = frame2numpy(message['frame'], (FRAME[0], FRAME[1])) - videoWriter.write(image) + # videoWriter.write(image) # cv2.imshow('img',image) # cv2.waitKey(-1) except KeyboardInterrupt: break - videoWriter.release() + # videoWriter.release() # We tell DeepGTAV to stop client.sendMessage(Stop()) client.close() diff --git a/read.py b/read.py new file mode 100644 index 0000000..4c4d5ba --- /dev/null +++ b/read.py @@ -0,0 +1,19 @@ +import cv2 +import gzip +from config import * +from deepgtav.messages import frame2numpy + +if __name__ == '__main__': + # open the dataset.pz file + with open(GFILE_PATH, 'rb') as f: + gzip_handler = gzip.GzipFile(mode='rb', fileobj=f) + + # decompress the file and show the video + while gzip_handler.readable(): + item = gzip_handler.readline() + image = frame2numpy(item, (FRAME[0], FRAME[1])) + cv2.imshow('CapturedVideo', image) + if cv2.waitKey(1) & 0xFF == ord('q'): + break + + cv2.destroyAllWindows() From a2ff31fec0c95cdba60456cf7220b513b3385ad2 Mon Sep 17 00:00:00 2001 From: ShawnNew Date: Tue, 4 Sep 2018 10:41:22 +0800 Subject: [PATCH 5/7] read pnc data --- config.py | 2 +- dataset | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/config.py b/config.py index 7a76174..e868ab1 100755 --- a/config.py +++ b/config.py @@ -7,7 +7,7 @@ output_file_dir = 'video/' output_file_name = 'capturedVideo.avi' DIR_PATH = os.path.join(base_path, output_file_dir) -GFILE_PATH = os.path.join(base_path, 'dataset.pz') +GFILE_PATH = os.path.join(base_path, 'dataset_2_pnc.pz') if os.path.exists(DIR_PATH): temp_file_path = os.path.join(DIR_PATH, output_file_name) diff --git a/dataset b/dataset index f741672..33589a6 100755 --- a/dataset +++ b/dataset @@ -42,7 +42,6 @@ if __name__ == '__main__': message = client.recvMessage() # The frame is a numpy array and can be displayed using OpenCV or similar - #TODO: save frames into video image = frame2numpy(message['frame'], (FRAME[0], FRAME[1])) # videoWriter.write(image) # cv2.imshow('img',image) From 1dbb025a04439d19e100f36cb14f785281938edd Mon Sep 17 00:00:00 2001 From: ShawnNew Date: Tue, 4 Sep 2018 14:32:21 +0800 Subject: [PATCH 6/7] read gzip file --- config.py | 6 +++--- read.py | 51 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/config.py b/config.py index e868ab1..d6c8e8b 100755 --- a/config.py +++ b/config.py @@ -1,13 +1,13 @@ import os -FPS = 30 -FRAME = [320, 160] +FPS = 10 +FRAME = [480, 320] base_path = os.getcwd() output_file_dir = 'video/' output_file_name = 'capturedVideo.avi' DIR_PATH = os.path.join(base_path, output_file_dir) -GFILE_PATH = os.path.join(base_path, 'dataset_2_pnc.pz') +GFILE_PATH = os.path.join(base_path, 'dataset_2_.pz') if os.path.exists(DIR_PATH): temp_file_path = os.path.join(DIR_PATH, output_file_name) diff --git a/read.py b/read.py index 4c4d5ba..f85cbca 100644 --- a/read.py +++ b/read.py @@ -1,19 +1,54 @@ import cv2 import gzip from config import * +import pickle from deepgtav.messages import frame2numpy +def parserCommands(dct): + """ + parse the Commands from every frame and return + :param dct: dct read from the file + :return: throttle, brake and steering + """ + return dct['throttle'], dct['brake'], dct['steering'] + +def parserState(dct): + """ + return the dict including the state of the vehicle + :param dct: dct read from the file + :return: location list, speed and yawRate + """ + stateDict = {} + stateDict['location'] = dct['location'] + stateDict['speed'] = dct['speed'] + stateDict['yawRate'] = dct['yawRate'] + return stateDict + if __name__ == '__main__': + command_list = [] + state_list = [] + frame_list = [] # open the dataset.pz file - with open(GFILE_PATH, 'rb') as f: - gzip_handler = gzip.GzipFile(mode='rb', fileobj=f) + with gzip.open( + filename=GFILE_PATH, mode='rb', compresslevel=0 + ) as f: + + while True: + try: + dct = pickle.load(f) + # show frame + frame = frame2numpy(dct['frame'], (FRAME[0], FRAME[1])) + # frame_list.append(frame) + cv2.imshow('Video', frame) + if cv2.waitKey(1) & 0xFF == ord('q'): + break - # decompress the file and show the video - while gzip_handler.readable(): - item = gzip_handler.readline() - image = frame2numpy(item, (FRAME[0], FRAME[1])) - cv2.imshow('CapturedVideo', image) - if cv2.waitKey(1) & 0xFF == ord('q'): + # get command and state of the vehicle + throttle, brake, steering = parserCommands(dct) + stateDict = parserState(dct) + # command_list.append((throttle, brake, steering)) + # state_list.append(stateDict) + except EOFError: break cv2.destroyAllWindows() From b6a35f8164f0cb1b1624438c88c9c80b244414de Mon Sep 17 00:00:00 2001 From: ShawnNew Date: Wed, 5 Sep 2018 11:27:53 +0800 Subject: [PATCH 7/7] fix socket connect issue --- config.py | 4 +++- dataset | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/config.py b/config.py index 448aae4..ce6b932 100644 --- a/config.py +++ b/config.py @@ -1,7 +1,9 @@ import os +DEFAULT_IP = 'localhost' +DEFAULT_PORT = 8000 FPS = 10 -FRAME = [480, 320] +# FRAME = [480, 320] FRAME = [320, 160] base_path = os.getcwd() output_file_dir = 'video/' diff --git a/dataset b/dataset index fd910a6..858d1a8 100644 --- a/dataset +++ b/dataset @@ -16,8 +16,8 @@ import cv2 # Stores a dataset file with data coming from DeepGTAV if __name__ == '__main__': parser = argparse.ArgumentParser(description=None) - parser.add_argument('-l', '--host', default='192.168.137.1', help='The IP where DeepGTAV is running') - parser.add_argument('-p', '--port', default=8000, help='The port where DeepGTAV is running') + parser.add_argument('-l', '--host', default=DEFAULT_IP, help='The IP where DeepGTAV is running') + parser.add_argument('-p', '--port', default=DEFAULT_PORT, help='The port where DeepGTAV is running') parser.add_argument('-d', '--dataset_path', default=GFILE_PATH, help='Place to store the dataset') args = parser.parse_args()