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.

live_eval.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python
  2. import os
  3. import tensorflow as tf
  4. import model
  5. import params
  6. import cv2
  7. import numpy as np
  8. import can
  9. bus = can.interface.Bus(channel='can0', bustype='socketcan_native')
  10. cap = cv2.VideoCapture(0)
  11. cap.set(3,300) # set height
  12. cap.set(4,200) # set width
  13. sess = tf.InteractiveSession()
  14. saver = tf.train.Saver()
  15. saver.restore(sess, "./weight/SSC_epoch_14_LR_0.0001.model")
  16. def get_frame():
  17. # read the frame from webcam
  18. _, frame = cap.read()
  19. tmp = cv2.resize(frame,(0,0),fx=0.8, fy=0.8)
  20. tmp = cv2.flip(tmp,0)
  21. tmp = cv2.flip(tmp,1)
  22. if params.img_channels != 3:
  23. tmp = cv2.cvtColor(tmp, cv2.COLOR_BGR2GRAY)
  24. img = (tmp[70:-5,::]).reshape(params.img_height, params.img_width, params.img_channels)
  25. return img, frame
  26. def get_angle():
  27. while(True):
  28. # get one can data
  29. can_data = str(bus.recv())
  30. # check it is steering angle, if yes then stop the loop
  31. if(can_data.find("ID: 0025") > 0):
  32. break
  33. # change the can data (HEX) to numerical data
  34. tmp = can_data
  35. hex_data = tmp[-23:-21] + tmp[-20:-18]
  36. hex_decimal = tmp[-3:-1]
  37. int_data = int(hex_data, 16)
  38. int_decimal = int(hex_decimal, 16) / 256
  39. # if the steering wheel angle in in right to the center
  40. if(int_data > 550):
  41. int_data = int_data - 4096
  42. int_decimal = 1 - int_decimal
  43. final_data = int_data - int_decimal
  44. else:
  45. # put the int and the decimal together
  46. final_data = int_data + int_decimal
  47. return final_data
  48. def main():
  49. # discard first 40 frames to give time for webcam to get the proper exposure
  50. for i in range(40):
  51. get_angle()
  52. get_frame()
  53. print(i, "discarded")
  54. while True:
  55. angle = get_angle()
  56. img, frame = get_frame()
  57. frame = cv2.flip(frame,0)
  58. frame = cv2.flip(frame,1)
  59. deg = model.y.eval(feed_dict={model.x: [img], model.keep_prob: 1.0})[0][0]
  60. #difference = ((final_data+16)-(deg+16))/(final_data+16)
  61. #if difference < 0:
  62. # difference = difference * -1
  63. #difference = difference * 100
  64. predicted = "predicted : " + str(deg)
  65. actual = "actual : " + str(angle)
  66. #percent = "% difference : " + str(difference)
  67. cv2.putText(frame,predicted,(10,30), cv2.FONT_HERSHEY_COMPLEX, 1,(255,255,255),2,cv2.LINE_AA)
  68. cv2.putText(frame,actual,(10,60), cv2.FONT_HERSHEY_COMPLEX, 1,(255,255,255),2,cv2.LINE_AA)
  69. #cv2.putText(frame,percent,(70,30), cv2.FONT_HERSHEY_COMPLEX, 1,(255,255,255),2,cv2.LINE_AA)
  70. print(deg,angle)
  71. cv2.imshow('frame', frame)
  72. if cv2.waitKey(1) & 0xFF == ord('q'):
  73. break
  74. main()
  75. cap.release()
  76. cv2.destroyAllWindows()