コード例 #1
0
ファイル: browser.py プロジェクト: JKatzwinkel/python-misc
	def compute_sim(self, key):
		print 'compute similarities for', self.img
		self.message('Compute similarities:\n\n{}\nx{} imgs'.format(
			self.img,
			len(picture.pictures())))
		sims = {}
		for p in picture.pictures():
			if p != self.img:
				sim = self.img.similarity(p)
				sims[p] = sim
		minsim = min(sims.values())
		maxsim = max(sims.values())
		thresh = maxsim - (maxsim-minsim)/3.
		res = []
		for p,s in sims.items():
			if s > thresh:
				picture.connect(self.img, p, s)
				res.append(p)
					#x=0
					#for q in res[:10]:
						#img = self.load_thmb(q)
						#self.cnv.create_image((x,780),
							#anchor=tk.SW, image=img)
						#self.cur_imgs.append(img)
						#x += img.width()
		print 'done. found {} images.'.format(len(res))
		self.merge_candidates = self.merge_cand()
		self.redraw=True
		return res
コード例 #2
0
ファイル: browser.py プロジェクト: JKatzwinkel/python-misc
	def cluster_mode(self, key):
		if self.mode in [Browser.BROWSE, Browser.BLOG, Browser.POPULAR]:
			#if len(self.clusters)<1:
			self.message('Clustering...')
			self.clusters = index.clustering(picture.pictures(),
				len(picture.pictures())-100)
			self.mode='cluster'
			for c in self.clusters:
				if self.img in c:
					self.pool = c
			self.redraw=True
		else:
			self.mode = Browser.BROWSE
			self.repool()
			self.redraw=True
コード例 #3
0
ファイル: index.py プロジェクト: JKatzwinkel/python-misc
def simpairs():
	res=[]
	imgs=picture.pictures()
	print 'Begin computing similarities between {} images'.format(
		len(imgs))
	# BEGIN
	for i in range(len(imgs)):
		p=imgs[i]
		for j in range(i+1,len(imgs)):
			q=imgs[j]
			sim=p.similarity(q)
			if sim>.5:
				res.append((p,q,sim))
				picture.connect(p,q,sim)
		#if i%100==0:
			#print '\t{}\t/\t{}'.format(i, len(imgs)),
	print '\tDone!\t\t\t'
	f=open('html/.twins.html','w')
	f.write('<html>\n<body>')
	res.sort(key=lambda t:t[2], reverse=True)
	for p,q,sim in res[:500]:
		if sim > .9:
			f.write('<h4>{} and {}: {}</h4>\n'.format(p.name,q.name,sim))
			f.write('<b>{} versus {}: </b><br/>\n'.format(p.info,q.info,))
			if len(p.sources)>0 and len(q.sources)>0:
				f.write('<i>{} and {}: </i><br/>\n'.format(p.origin.name,q.origin.name,))
			f.write('<img src="../{}"/><img src="../{}"/><br/>\n'.format(
				p.location, q.location))
	f.write('</body>\n</html>\n')
	f.close()
	print 'Done'
	return res
コード例 #4
0
ファイル: browser.py プロジェクト: JKatzwinkel/python-misc
	def __init__(self, root):
		self.cur_imgs = [] # backup references against garbage coll.
		self.img_reg = {} # registry to avoid disk access
		self.thmb_reg = {} # thumbnail registry to avoid resize
		self.preview_reg = {} # preview registry to avoid resize
		self.mode = Browser.BROWSE
		self.redraw = False # set true to redraw gui
		self.changes = False # changes to be saved?
		self.new_votes = set() # keep track of new ratings
		self.pool=[]
		# TODO: selection: GUI indicator must be present for selection state,
		# selection must be viewable in its entirety in dedicated view mode,
		# selection should probably be of type set.
		self.selection=[] # selection for exports or queries
		# compare new pictures with favorites
		favs = picture.starred()[:10]
		newsies = [p for p in picture.pictures() if p.reviewed < 1]
		if len(newsies)>0 and len(favs)>0:
			print 'calculating similarities between {} new images and highest rated'.format(
				len(newsies))
			self.new_votes = set(favs)
			for n in newsies:
				for p in favs:
					if n != p:
						sim = p.similarity(n)
						if sim > .6:
							picture.connect(n, p, sim)
			print 'done doing that.'
		# merge candidates for currentlt selected image
		self.merge_candidates=[]
		# img clusters
		self.clusters=[]
		# init img tracking
		self.repool()
		#pics = sorted(pics, key=lambda p:p.rating)
		# repopulate history
		self.hist = []
		for p in picture.last_reviewed()[:50]:
			if util.days_since(p.reviewed)<1.5:
				self.hist.append(p)
				if p in self.pool:
					self.pool.remove(p)
		# choose image to displ
		if len(self.hist)<1 and len(self.pool)>0:
			self.choose(self.pool.pop()) # current image as Pict object
			self.hist = [self.img] # history of recent images
		elif len(self.hist)>0:
			self.img = self.hist[0]
		# self.trash
		# key: pict instance, value: (path, image)
		self.trash = {}
		# canvas
		self.cnv = tk.Canvas(root, bg="black")
		self.cnv.pack(side='top', fill='both', expand='yes')
		# put a button on the image panel to test it
		#self.button = tk.Button(self.cnv, text='button2')
		#self.button.pack(side='top')
		self.merge_candidates = self.merge_cand()
		self.display()
コード例 #5
0
ファイル: browser.py プロジェクト: JKatzwinkel/python-misc
	def import_dump(self, key):
		if not os.path.exists('exports'):
			os.mkdir('exports')
		imgs=[]
		srcimgs=[]
		blgs=[]
		#TODO: find all xml files in folder
		if os.path.exists('exports/images.xml'):
			#TODO: download image from original location
			imgrec = util.inout.loadImages('exports/images.xml')
			srcimgs = [picture.opendump(rec) for rec in imgrec]
			# FIXME: workaround wegen offline: bilder werden komplett mitkopiert
			for p in srcimgs:
				if not os.path.exists('images/{}'.format(p.filename)):
					os.rename('exports/{}'.format(p.filename), 'images/{}'.format(p.filename))
					imgs.append(p)
			# remove images xml records so that missing images cannot be imported again
			os.remove('exports/images.xml')
		if os.path.exists('exports/blogs.xml'):
			blgrec = util.inout.loadBlogs('exports/blogs.xml')
			blgs = [tumblr.opendump(rec) for rec in blgrec]
			# reify image references made by blogs
			for t in blgs:
				index.clean_img_refs(t)
			# remove xml file because import is successful
			os.remove('exports/blogs.xml')
		# now that we have our blogs imported, we can reify blog/img references
		# in img instances
		# TODO: reification of source blogs, interblog references, interimg links!!
		for p in srcimgs:
			index.clean_sources(p)
			p.clean_links()
		self.message('imported {} images and {} blogs.'.format(
			len(srcimgs), len(blgs)))
 		# compute similarities with present images
 		self.message('compute similarities with present images..')
		bestsim=0 # sim stat
 		for p in imgs:
			sims = {}
			for q in picture.pictures():
				if q != p:
					sims[q] = p.similarity(q)
			minsim,maxsim = (min(sims.values()), max(sims.values()))
			p.relates.update({q:s for q,s in sims.items()
				if s > maxsim-(maxsim-minsim)/3})
			# keep track of best match
			bestsim = max(bestsim, maxsim)
		# remove xml files (now left without their actual images...)
		# and repool
		if len(imgs)>0:
			self.pool = imgs
		self.redraw = True
 		self.message('\n'.join(['Imported {} image records with {} new images'.format(
			len(srcimgs), len(imgs)),
			'featured by {} blogs.'.format(len(blgs)),
			'Highest similarity between old and new image was {:.2f}'.format(bestsim)]),
			confirm=True)
コード例 #6
0
ファイル: test.py プロジェクト: JKatzwinkel/python-misc
#!/usr/bin/python

import index
import weave.picture as picture
import weave.tumblr as tumblr
import util.cluster as clustering

index.load()

index.simpairs()
index.save()

pics = picture.pictures()
pics = sorted(pics, key=lambda p: len(p.relates))
index.stumblr(pics[-1], "walk.html")


clusters = clustering.linkage(pics, len(pics) / 10, mode=clustering.LINK_NORM)
groups = [c.members for c in clusters]
index.savegroups(groups, "cluster.html")
コード例 #7
0
ファイル: index.py プロジェクト: JKatzwinkel/python-misc
def pictures():
	return picture.pictures()