Changeset - 79c410b194c6
[Not reviewed]
default
0 1 0
Laman - 6 years ago 2018-12-20 22:03:39

sampler takes command line arguments
1 file changed with 9 insertions and 3 deletions:
0 comments (0 inline, 0 general)
exp/color_sampler.py
Show inline comments
 
@@ -6,29 +6,33 @@ import json
 
import gzip
 
from collections.abc import MutableMapping
 
import tkinter as tk
 
from tkinter import LEFT,N,S,E,W
 
from colorsys import rgb_to_hsv
 

	
 
from PIL import Image,ImageTk
 

	
 
from analyzer.epoint import EPoint
 
from analyzer.corners import Corners
 

	
 

	
 
dirname=sys.argv[1]
 
annotations=sys.argv[2]
 

	
 

	
 
class Sampler:
 
	def __init__(self):
 
		self.dirname="../images"
 
		self.annotations=DataFile("annotations.json.gz")
 
		self.filenames=[f for f in os.listdir(self.dirname) if f.endswith(".jpg")]
 
		self.dirname=dirname
 
		self.annotations=DataFile(annotations)
 
		self.filenames=[f for f in sorted(os.listdir(self.dirname)) if f.endswith(".jpg")]
 
		self.k=0
 

	
 
		self.img=None
 
		self.photo=None
 

	
 
		self.letter="_"
 
		self.hsv=(0,0,0)
 
		self.pos=(0,0)
 
		self.corners=Corners()
 
		self._dirty=False
 

	
 
		self._createGUI()
 
@@ -120,24 +124,25 @@ class Sampler:
 
		for c in self.corners:
 
			(x,y)=(c.x,c.y)
 
			self.canvas.create_oval(x-2,y-2,x+2,y+2,fill="#00ff00",tags="mark")
 
		if self.corners.canonizeOrder():
 
			(a,b,c,d)=self.corners
 
			self.canvas.create_line(a.x,a.y,b.x,b.y,fill="#00ff00",tags="mark")
 
			self.canvas.create_line(b.x,b.y,c.x,c.y,fill="#00ff00",tags="mark")
 
			self.canvas.create_line(c.x,c.y,d.x,d.y,fill="#00ff00",tags="mark")
 
			self.canvas.create_line(d.x,d.y,a.x,a.y,fill="#00ff00",tags="mark")
 

	
 

	
 
class DataFile(MutableMapping):
 
	"""self._data: {filename: [EPoint,EPoint,EPoint,EPoint]}"""
 
	def __init__(self,filename):
 
		self.filename=filename
 
		try:
 
			with gzip.open(filename,mode="rt",encoding="utf8") as f:
 
				self._data=json.load(f,object_hook=self.deserialize)
 
		except OSError:
 
			self._data=dict()
 

	
 
	def save(self):
 
		with gzip.open(self.filename,mode="wt",encoding="utf8") as f:
 
			json.dump(self._data,f,default=self.serialize,indent="\t")
 

	
 
@@ -148,13 +153,14 @@ class DataFile(MutableMapping):
 

	
 
	def deserialize(self,obj):
 
		if obj.get("type")!="EPoint": return obj
 
		return EPoint(*obj["val"])
 

	
 
	def __getitem__(self, key): return self._data[key]
 
	def __setitem__(self, key, val): self._data[key]=val
 
	def __delitem__(self, key): del self._data[key]
 
	def __iter__(self): return iter(self._data)
 
	def __len__(self): return len(self._data)
 

	
 

	
 
if __name__=="__main__":
 
s=Sampler()
0 comments (0 inline, 0 general)