Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- from pprint import pprint
- from bson.objectid import ObjectId
- from pymongo import MongoClient
- from PIL import Image
- import gridfs, os
- import collections
- import cv2
- import numpy as np
- from matplotlib import pyplot as plt
- from matplotlib import image as mpimg
- #/ creating connections for communicating with Mongo DB
- client = MongoClient('localhost', 27017)
- db = client.knots
- fs = gridfs.GridFS(db)
- fm = db.marked_up_image
- data_dict = dict.fromkeys(['image', 'type', 'coordinates'])
- data = np.zeros(50,)
- for gridout in fm.find({"users_polygons.polygons.type": "knot_defect"}).limit(20):
- images = []
- plist = gridout['users_polygons']
- data_dict['image'] = gridout['image']
- for usrpoly in plist:
- poly = usrpoly['polygons']
- for p in poly:
- points = p['points']
- data_dict['type'] = p['type']
- for point in points:
- data_dict['coordinates'] = [point['x'], point['y']]
- try:
- with open('tmp.png', 'wb') as fi:
- fi.write(fs.get( ObjectId(gridout['image']) ).read() )
- img = cv2.imread('tmp.png', 0)
- hist, bins = np.histogram(img.ravel(), 50, [0,50], density=True)
- width = np.diff(bins)
- center = (bins[:-1] + bins[1:])/2
- tmp = data
- data += hist
- tmp += hist
- pprint(np.array_equal(data, tmp)) #True
- pprint ('========================')
- data += hist
- tmp = tmp + hist
- pprint(np.array_equal(data, tmp)) # False
- pprint('========================')
- data = data + hist
- tmp += hist
- pprint(np.array_equal(data, tmp)) # True
- pprint('========================')
- data = data + hist
- tmp = tmp + hist
- pprint(np.array_equal(data, tmp)) # True
- pprint('========================')
- #pprint (data)
- #pprint (tmp)
- if img is not None:
- images.append(img)
- except StopIteration:
- print("zero cursor")
- #pprint (data)
- meanhist = np.zeros([50,])
- #for items in np.nditer(data, op_flags=['readwrite']):
- #meanhist.append([sum(i) for i in zip(*data)])
- #meanhist = np.array(meanhist)/50
- for gridout in fm.find({"users_polygons.polygons.type": "knot_defect"}).limit(20):
- images = []
- plist = gridout['users_polygons']
- data_dict['image'] = gridout['image']
- for usrpoly in plist:
- poly = usrpoly['polygons']
- for p in poly:
- points = p['points']
- data_dict['type'] = p['type']
- for point in points:
- data_dict['coordinates'] = [point['x'], point['y']]
- try:
- with open('tmp.png', 'wb') as fi:
- fi.write(fs.get(ObjectId(gridout['image'])).read())
- img = cv2.imread('tmp.png', 0)
- hist, bins = np.histogram(img.ravel(), 50, [0, 50], density=True)
- width = np.diff(bins)
- center = (bins[:-1] + bins[1:]) / 2
- #plt.hist(meanhist, bins, alpha=0.5, label='mean')
- #plt.hist(hist, bins, alpha=0.5, label='current')
- #plt.legend(loc='upper right')
- #plt.show()
- if img is not None:
- images.append(img)
- except StopIteration:
- print("zero cursor")
- #pprint (hist)
- #pprint (meanhist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement