document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import sys
  2. import serial
  3. import cv2
  4. import os
  5.  
  6. #List of file in .jpg format, extensions can be change
  7.  
  8. def main():
  9.  
  10. File_Lst =[]
  11.  
  12.  
  13. #Host Operating system name, as file extension and serial port differs
  14.  
  15. plat = sys.platform
  16. print plat
  17.  
  18. if plat == 'win32': #for windows operating system
  19.  
  20. import serial.tools.list_ports
  21.  
  22. print list(serial.tools.list_ports.comports())
  23.  
  24. com_port = 'COM2'
  25. baud_rate = 9600
  26. File_dir = "C:\\\\Users\\\\user\\\\Desktop\\\\fruit\\\\"
  27.  
  28.  
  29. elif plat == 'cygwin': # for cygwin
  30.  
  31. import serial.tools.list_ports_linux
  32.  
  33. print list(serial.tools.list_ports_linux.comports())
  34. com_port = '/dev/com2'
  35. baud_rate = 9600
  36.  
  37.  
  38. elif plat == 'linux2': # for linux/ubuntu
  39.  
  40. import serial.tools.list_ports
  41.  
  42. print list(serial.tools.list_ports.comports())
  43. com_port = '/dev/ttyACM0'
  44. baud_rate = 9600
  45. File_dir = "/host/Users/user/Desktop/fruit/"
  46.  
  47.  
  48. print "Attempting to open your com port..."
  49.  
  50. try:
  51.  
  52. # Store files in list
  53.  
  54. for file in os.listdir(File_dir):
  55.  
  56. File_Lst.append(file)
  57.  
  58. print File_Lst
  59.  
  60.  
  61. welcome_index = File_Lst.index('welcome.jpg')
  62. welcome_str = File_Lst[welcome_index]
  63.  
  64. print welcome_str
  65.  
  66. orange_index = File_Lst.index('orange.jpg')
  67. orange_str = File_Lst[orange_index]
  68. print orange_str.split('.')[0]
  69.  
  70. print orange_str
  71.  
  72. apple_index = File_Lst.index('apple.jpg')
  73. apple_str = File_Lst[apple_index]
  74.  
  75. print apple_str
  76.  
  77. banana_index = File_Lst.index('banana.jpg')
  78. banana_str = File_Lst[banana_index]
  79.  
  80. print banana_str
  81.  
  82. doughnuts_index = File_Lst.index('doughnuts.jpg')
  83. doughnuts_str = File_Lst[doughnuts_index]
  84.  
  85. # check the device com number
  86.  
  87.  
  88. ser = serial.Serial(com_port, baud_rate)
  89. print ser
  90. print "Successfully opened the com port."
  91. print "Your com port returned the following information:", com_port, baud_rate
  92.  
  93.  
  94. print "Listening for key presses..."
  95.  
  96. #Welcome Screen
  97.  
  98. img = cv2.imread(File_dir + welcome_str )
  99. cv2.destroyAllWindows()
  100. cv2.imshow("Press KEYS to know which food is good or bad", img)
  101.  
  102. while True:
  103.  
  104. try:
  105.  
  106. k = cv2.waitKey(0)
  107.  
  108. if k == ord('w'): # wait for 'w' key to upload orange nutrition information
  109.  
  110. img = cv2.imread(File_dir + orange_str)
  111. newx,newy = img.shape[1]/2,img.shape[0]/2 #new size (w,h)
  112. img = cv2.resize(img,(newx,newy))
  113. cv2.destroyAllWindows()
  114. cv2.imshow("Orange Nutritional Information", img)
  115.  
  116. elif k == ord('a'): # wait for 'w' key to upload apple nutrition information
  117.  
  118. img = cv2.imread(File_dir + apple_str)
  119. newx,newy = img.shape[1]/2,img.shape[0]/2 #new size (w,h)
  120. img = cv2.resize(img,(newx,newy))
  121. cv2.destroyAllWindows()
  122. cv2.imshow("Apple Nutritional Information", img)
  123.  
  124. elif k == ord('s'): # wait for 'w' key to upload apple nutrition information
  125.  
  126. img = cv2.imread(File_dir + banana_str)
  127. newx,newy = img.shape[1]/2,img.shape[0]/2 #new size (w,h)
  128. img = cv2.resize(img,(newx,newy))
  129. cv2.destroyAllWindows()
  130. cv2.imshow("Banana Nutritional Information", img)
  131.  
  132. elif k == 32:
  133.  
  134. break
  135. cv2.destroyAllWindows()
  136.  
  137. else:
  138.  
  139. img = cv2.imread(File_dir + doughnuts_str)
  140. cv2.destroyAllWindows()
  141. cv2.imshow("Bad, Have good eating habits CHUMP", img)
  142.  
  143.  
  144. except cv2.error:
  145. print "Image file not found"
  146. sys.exit(-1)
  147.  
  148. ser.close()
  149.  
  150. except serial.serialutil.SerialException:
  151.  
  152. print("Kindly connect an USB device")
  153. sys.exit(-1)
  154.  
  155. main()
');