Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. dataPath = "indian_dataset/"
  2. corrDataPath = "indian_dataset/corr/"
  3. fileNamePrefix = "circuit2_x264.mp4 "
  4.  
  5. #read data.txt
  6. xs = []
  7. ys = []
  8. accels = []
  9. brakes = []
  10. opticalFlow = []
  11. gears = []
  12. gearFeatures = []
  13.  
  14. predictedGears = []
  15. considerPreviousGears = 10
  16.  
  17. with open(dataPath+"data.txt") as f:
  18. for line in f:
  19. xs.append(dataPath + fileNamePrefix + str(int(line.split()[0])).zfill(5)+".jpg")
  20. # No need to convert to radians as here we dont use for training.
  21. steer_value = float(line.split()[1])
  22. accel_value = float(line.split()[2])
  23. brake_value = float(line.split()[3])
  24. gear_value = float(line.split()[4])
  25. ys.append(steer_value)
  26. accels.append(accel_value)
  27. brakes.append(brake_value)
  28. gears.append(gear_value)
  29. gearFeatures.append([steer_value, accel_value, brake_value])
  30.  
  31. i = 0
  32. with open(corrDataPath+"optFlow.txt") as f:
  33. # with open("driving_dataset/data.txt") as f:
  34. for line in f:
  35. # xs.append("driving_dataset/" + line.split()[0])
  36. opticalFlow.append(float(line.split()[0]))
  37. gearFeatures[i].append(float(line.split()[0]))
  38. i += 1
  39.  
  40. gearModel = RandomForestClassifier()
  41.  
  42. gearModel.fit(np.array(gearFeatures), np.array(gears))
  43.  
  44.  
  45. while(cv2.waitKey(10) != ord('q') and i < num_images-1):
  46.  
  47. predictedGear = gearModel.predict(np.array(gearFeatures[i]).reshape(1, -1))
  48. predictedGears.append(predictedGear)
  49.  
  50. # lazy check to see whether all gear predictions in previous 'x' frames same as current prediction
  51. # if same then take the gear value seriously.
  52. previousGears = predictedGears[-considerPreviousGears:]
  53. if (sum(previousGears)/len(previousGears) == predictedGear):
  54. takeGearSeriously = True
  55. else:
  56. takeGearSeriously = False
  57.  
  58. # if repeated frames predict a different gear then change the gear.
  59. if (predictedGear[0] != gear and abs(gear - predictedGear[0]) == 1 and takeGearSeriously): # if gear shift
  60. gearShift = int(predictedGear[0] - gear)
  61. gear = int(predictedGear[0])
  62. print("GEAR CHANGED!!!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement