PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/016/Reaction_Monitoring/RealTime/TitrationCode_15.py

https://bitbucket.org/doane-divas/divas
Python | 232 lines | 82 code | 81 blank | 69 comment | 6 complexity | ef23ec9747142e24237915709a7d07cd MD5 | raw file
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Mon Jun 26 14:29:07 2017
  5. @author: Chris and Louise
  6. """
  7. # import the necessary packages
  8. import cv2
  9. import pandas as pd
  10. import numpy as np
  11. import glob
  12. import sys
  13. import matplotlib.pyplot as plt
  14. import statistics as stats
  15. #uses a command line argument to define what the directory being used is called
  16. path_to_file = sys.argv[1]
  17. ################################
  18. #uses commandline argument to know what directory to go into
  19. def filepull():
  20. #defines location of directory
  21. #function that reads images from folder designated on command line
  22. #path_to_file is the folder defining argument
  23. #def readFolder(path_to_file):
  24. path_to_file = sys.argv[1]
  25. #use the glod tool to go into folder and look at the files/images in the folder
  26. #places all of the images into a list calle 'filenames'
  27. filenames = [img for img in glob.glob(path_to_file)]
  28. #this lines sorts all of the images in alphabetical order,
  29. #therefore also in chronological order
  30. #added this when the code was reading the images in a random order
  31. filenames.sort()
  32. return filenames
  33. def fileread(filenames):
  34. image = filenames
  35. #empty list to store the images in
  36. read_image = cv2.imread(image)
  37. #returns wanted file
  38. return read_image
  39. #############################################################################
  40. #function to perform RGB image analysis on image 'n'
  41. def RGBanalysis(n):
  42. #modifiable pixel coordinates for center of sampling area
  43. x_coord = 400
  44. y_coord = 500
  45. #this will take the RGB data from the specified region
  46. #talk to Louise to run atleast 3-4 titrations in a marked position
  47. #on the stirrer to find an average position range to for general use
  48. b = n[x_coord - 100 : x_coord + 100 ,y_coord - 100 : y_coord + 100, 0]
  49. g = n[x_coord - 100 : x_coord + 100 ,y_coord - 100 : y_coord + 100, 1]
  50. r = n[x_coord - 100 : x_coord + 100 ,y_coord - 100 : y_coord + 100, 2]
  51. #averages the RGB values of all the pixels in the above region
  52. bAvg = np.mean(b)
  53. gAvg = np.mean(g)
  54. rAvg = np.mean(r)
  55. #defines averages a their color values
  56. blue = bAvg
  57. green = gAvg
  58. red = rAvg
  59. #returns the three values that make up an RGB code
  60. return blue,green,red
  61. ################################################################
  62. ################################################################
  63. def initial_analysis(green_list):
  64. #list of values to look at, this will be the list of 20 first green values
  65. #on the plot to find the avg and stdev
  66. average_knot = stats.mean(green_list[0:20])
  67. stdev_knot = stats.stdev(green_list[0:20])
  68. return average_knot, stdev_knot
  69. #################################################################
  70. #################################################################
  71. def comparison_analysis(green_list):
  72. new_Avg = stats.mean(green_list[-20])
  73. new_stdev = stats.stdev(green_list[-20])
  74. return new_Avg, new_stdev
  75. ###################################################################
  76. ###################################################################
  77. #function of code
  78. def maincode():
  79. #creates a plot with y-axis from 0 to 300
  80. #and x-axis from 0 to 100
  81. #axis can be changed to accomodate metrics
  82. fig = plt.figure()
  83. plt.axis([0,1000,0,300])
  84. plt.ion()
  85. plt.show()
  86. #empty lists to hold data after analysis
  87. #used to create a datafram with all of the data
  88. #datafram will then be exported into csv
  89. images = []
  90. blue_list = []
  91. green_list = []
  92. red_list = []
  93. #list of averages and stdevs calculated as code progresses
  94. #these are values used to compare to fist calculated avg and stdev
  95. #to see if values have reached expected threshold value
  96. New_avg_list = []
  97. New_stdev_list = []
  98. #starts loop to read and analyze images
  99. #set range to be number of images intended to capture plus 100
  100. #make sure range value is smaller than amount of image files in directory
  101. for (i,cap) in enumerate(range(900)):
  102. #images are capture one a second
  103. #index starts at 0 so add 1
  104. second = i + 1
  105. #defines file finding function
  106. filenames = filepull()
  107. print(filenames[i])
  108. #file reading fucntion
  109. image = fileread(filenames[i])
  110. #stores RGB values from analysis function
  111. blue,green,red = RGBanalysis(image)
  112. #uses empty lists to store data as they're obtained
  113. images.append(second)
  114. blue_list.append(blue)
  115. green_list.append(green)
  116. red_list.append(red)
  117. #defines the green value
  118. Green_point = green
  119. #begin analysis
  120. if i == 20:
  121. average_knot, stdev_knot = initial_analysis(green_list)
  122. print(average_knot, stdev_knot)
  123. elif i > 20:
  124. new_Avg, new_stdev = comparison_analysis(green_list)
  125. New_avg_list.append(new_Avg)
  126. New_stdev_list.append(new_stdev)
  127. if new_Avg > (average_knot - stdev_knot):
  128. print("Threshold not reached")
  129. elif new_Avg <= (average_knot - stdev_knot):
  130. print("Threshold reached")
  131. #####add alert stuff
  132. #plots the time(s) vs. the green color channel value
  133. #green circles for the points
  134. plt.plot([second],[Green_point],'g.')
  135. #draws the plot
  136. plt.draw()
  137. #pause added, needs to be in time with camera
  138. plt.pause(.5)
  139. fig.savefig('titration_curve.png')
  140. #after analysis is done, data is placed into dataframe
  141. #esports data
  142. dataframe = pd.DataFrame(images, columns = ['Image #'])
  143. dataframe['blue'] = blue_list
  144. dataframe['green'] = green_list
  145. dataframe['red'] = red_list
  146. print(dataframe)
  147. #export datafram onto as csv
  148. dataframe.to_csv('_data.csv')
  149. #calls code
  150. maincode()