Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from pprint import pprint
  4. from bson.objectid import ObjectId
  5. from pymongo import MongoClient
  6. from PIL import Image
  7. import gridfs, os
  8. import collections
  9. import cv2
  10. import numpy as np
  11. from matplotlib import pyplot as plt
  12. from matplotlib import image as mpimg
  13.  
  14. #/ creating connections for communicating with Mongo DB
  15. client = MongoClient('localhost', 27017)
  16. db = client.knots
  17. fs = gridfs.GridFS(db)
  18. fm = db.marked_up_image
  19. data_dict = dict.fromkeys(['image', 'type', 'coordinates'])
  20. data = np.zeros(50,)
  21.  
  22.  
  23. for gridout in fm.find({"users_polygons.polygons.type": "knot_defect"}).limit(20):
  24.  
  25. images = []
  26. plist = gridout['users_polygons']
  27. data_dict['image'] = gridout['image']
  28. for usrpoly in plist:
  29. poly = usrpoly['polygons']
  30. for p in poly:
  31. points = p['points']
  32. data_dict['type'] = p['type']
  33. for point in points:
  34. data_dict['coordinates'] = [point['x'], point['y']]
  35.  
  36.  
  37.  
  38. try:
  39. with open('tmp.png', 'wb') as fi:
  40. fi.write(fs.get( ObjectId(gridout['image']) ).read() )
  41. img = cv2.imread('tmp.png', 0)
  42. hist, bins = np.histogram(img.ravel(), 50, [0,50], density=True)
  43. width = np.diff(bins)
  44. center = (bins[:-1] + bins[1:])/2
  45. tmp = data
  46.  
  47.  
  48. data += hist
  49. tmp += hist
  50. pprint(np.array_equal(data, tmp)) #True
  51. pprint ('========================')
  52.  
  53. data += hist
  54. tmp = tmp + hist
  55. pprint(np.array_equal(data, tmp)) # False
  56. pprint('========================')
  57.  
  58. data = data + hist
  59. tmp += hist
  60. pprint(np.array_equal(data, tmp)) # True
  61. pprint('========================')
  62.  
  63. data = data + hist
  64. tmp = tmp + hist
  65. pprint(np.array_equal(data, tmp)) # True
  66. pprint('========================')
  67.  
  68. #pprint (data)
  69.  
  70. #pprint (tmp)
  71.  
  72.  
  73. if img is not None:
  74. images.append(img)
  75.  
  76.  
  77.  
  78.  
  79. except StopIteration:
  80. print("zero cursor")
  81. #pprint (data)
  82. meanhist = np.zeros([50,])
  83.  
  84. #for items in np.nditer(data, op_flags=['readwrite']):
  85.  
  86. #meanhist.append([sum(i) for i in zip(*data)])
  87.  
  88. #meanhist = np.array(meanhist)/50
  89.  
  90.  
  91. for gridout in fm.find({"users_polygons.polygons.type": "knot_defect"}).limit(20):
  92.  
  93. images = []
  94. plist = gridout['users_polygons']
  95. data_dict['image'] = gridout['image']
  96. for usrpoly in plist:
  97. poly = usrpoly['polygons']
  98. for p in poly:
  99. points = p['points']
  100. data_dict['type'] = p['type']
  101. for point in points:
  102. data_dict['coordinates'] = [point['x'], point['y']]
  103.  
  104. try:
  105. with open('tmp.png', 'wb') as fi:
  106. fi.write(fs.get(ObjectId(gridout['image'])).read())
  107. img = cv2.imread('tmp.png', 0)
  108. hist, bins = np.histogram(img.ravel(), 50, [0, 50], density=True)
  109. width = np.diff(bins)
  110. center = (bins[:-1] + bins[1:]) / 2
  111.  
  112.  
  113.  
  114. #plt.hist(meanhist, bins, alpha=0.5, label='mean')
  115. #plt.hist(hist, bins, alpha=0.5, label='current')
  116. #plt.legend(loc='upper right')
  117. #plt.show()
  118.  
  119. if img is not None:
  120. images.append(img)
  121.  
  122.  
  123. except StopIteration:
  124. print("zero cursor")
  125.  
  126. #pprint (hist)
  127. #pprint (meanhist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement