Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 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.  
  46. data += hist
  47.  
  48. pprint (data)
  49.  
  50.  
  51.  
  52. if img is not None:
  53. images.append(img)
  54.  
  55.  
  56.  
  57.  
  58. except StopIteration:
  59. print("zero cursor")
  60. #pprint (data)
  61. meanhist = np.zeros([50,])
  62.  
  63. #for items in np.nditer(data, op_flags=['readwrite']):
  64.  
  65. #meanhist.append([sum(i) for i in zip(*data)])
  66.  
  67. #meanhist = np.array(meanhist)/50
  68.  
  69.  
  70. for gridout in fm.find({"users_polygons.polygons.type": "knot_defect"}).limit(20):
  71.  
  72. images = []
  73. plist = gridout['users_polygons']
  74. data_dict['image'] = gridout['image']
  75. for usrpoly in plist:
  76. poly = usrpoly['polygons']
  77. for p in poly:
  78. points = p['points']
  79. data_dict['type'] = p['type']
  80. for point in points:
  81. data_dict['coordinates'] = [point['x'], point['y']]
  82.  
  83. try:
  84. with open('tmp.png', 'wb') as fi:
  85. fi.write(fs.get(ObjectId(gridout['image'])).read())
  86. img = cv2.imread('tmp.png', 0)
  87. hist, bins = np.histogram(img.ravel(), 50, [0, 50], density=True)
  88. width = np.diff(bins)
  89. center = (bins[:-1] + bins[1:]) / 2
  90.  
  91.  
  92.  
  93. #plt.hist(meanhist, bins, alpha=0.5, label='mean')
  94. #plt.hist(hist, bins, alpha=0.5, label='current')
  95. #plt.legend(loc='upper right')
  96. #plt.show()
  97.  
  98. if img is not None:
  99. images.append(img)
  100.  
  101.  
  102. except StopIteration:
  103. print("zero cursor")
  104.  
  105. #pprint (hist)
  106. #pprint (meanhist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement