No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

collect_raw_data.py 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import numpy as np
  2. import cv2
  3. import time
  4. import os
  5. import can
  6. import signal
  7. import sys
  8. bus = can.interface.Bus(channel='can0', bustype='socketcan_native')
  9. cap = cv2.VideoCapture(0)
  10. cap.set(3,300) # set height
  11. cap.set(4,200) # set width
  12. raw_data = []
  13. def signal_handler(signal, frame):
  14. name = str(time.time())
  15. file_name = "/raw_data/" + name + ".npy"
  16. np.save(file_name,raw_data)
  17. print("saved {} frame, name : {}".format(len(raw_data),name))
  18. sys.exit(0)
  19. signal.signal(signal.SIGINT, signal_handler)
  20. def get_frame():
  21. # read the frame from webcam
  22. _, frame = cap.read()
  23. # change the color to gray
  24. #frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  25. # change the resolution (256*141), later cut the sky
  26. frame = cv2.resize(frame,(0,0),fx=0.8, fy=0.8)
  27. return frame
  28. def get_angle():
  29. while(True):
  30. # get one can data
  31. can_data = str(bus.recv())
  32. # check it is steering angle, if yes then stop the loop
  33. if(can_data.find("ID: 0025") > 0):
  34. break
  35. return can_data
  36. def main():
  37. global raw_data
  38. # discard first 40 frames to give time for webcam to get the proper exposure
  39. for i in range(40):
  40. get_angle()
  41. get_frame()
  42. print(i, "discarded")
  43. while True:
  44. angle = get_angle()
  45. frame = get_frame()
  46. raw_data.append([frame, angle])
  47. if len(raw_data) % 1000 == 0:
  48. print(len(raw_data))
  49. '''
  50. if len(raw_data) == 5000:
  51. pid = os.fork()
  52. if pid == 0:
  53. name = str(time.time())
  54. file_name = "/raw_data/" + name + ".npy"
  55. np.save(file_name,raw_data)
  56. print("saved {} frame, name : {}".format(len(raw_data),name))
  57. exit(0)
  58. else:
  59. raw_data = []
  60. '''
  61. main()
  62. cap.release()
  63. # cv2.destroyAllWindows()