Exemplo n.º 1
0
	def update(self):
		if isinstance(self.line,matplotlib.image.AxesImage):
			# image name
			try:
				if len(self.xdata)==0 and self.dataName: self.xdata=self.imgData[self.dataName]  # empty list reference an empty singleton, not the list we want; adjust here
				import Image
				if self.xdata[current]==None: img=Image.new('RGBA',(1,1),(0,0,0,0))
				else: img=Image.open(self.xdata[current])
				self.line.set_data(img)
			except IndexError: pass
		else:
			# regular data
			import numpy
			# current==-1 avoids copy slicing data in the else part
			if current==None or current==-1 or afterCurrentAlpha==1:
				self.line.set_xdata(self.xdata); self.line.set_ydata(self.ydata)
				self.line2.set_xdata([]); self.line2.set_ydata([])
			else:
				try: # try if we can extend the first part by one so that lines are connected
					self.xdata[:current+1]; preCurrEnd=current+1
				except IndexError: preCurrEnd=current
				preCurrEnd=current+(1 if len(self.xdata)>current else 0)
				self.line.set_xdata(self.xdata[:preCurrEnd]); self.line.set_ydata(self.ydata[:preCurrEnd])
				self.line2.set_xdata(self.xdata[current:]); self.line2.set_ydata(self.ydata[current:])
			try:
				x,y=self.xdata[current],self.ydata[current]
			except IndexError: x,y=0,0
			# this could be written in a nicer way, very likely
			try:
				pt=numpy.ndarray((2,),buffer=numpy.array([float(x),float(y)]))
				if self.scatter:
					self.scatter.set_offsets(pt)
					# change rotation of the marker (possibly incorrect)
					try:
						dx,dy=self.xdata[current]-self.xdata[current-1],self.ydata[current]-self.ydata[current-1]
						# smoothing from last n values, if possible
						# FIXME: does not show arrow at all if less than window values
						#try:
						#	window=10
						#	dx,dy=[numpy.average(numpy.diff(dta[current-window:current])) for dta in self.xdata,self.ydata]
						#except IndexError: pass
						# there must be an easier way to find on-screen derivative angle, ask on the matplotlib mailing list
						axes=self.line.get_axes()
						p=axes.patch; xx,yy=p.get_verts()[:,0],p.get_verts()[:,1]; size=max(xx)-min(xx),max(yy)-min(yy)
						aspect=(size[1]/size[0])*(1./axes.get_data_ratio())
						angle=math.atan(aspect*dy/dx)
						if dx<0: angle-=math.pi
						self.scatter.set_transform(matplotlib.transforms.Affine2D().rotate(angle))
					except IndexError: pass
				if self.annotation:
					if math.isnan(x) or math.isnan(y):
						if hasattr(self.annotation,'xyann'): self.annotation.xyann=(x,y)
						else: self.annotation.xytext=(0,0)
						self.annotation.set_text('') # make invisible, place anywhere
					else:
						#
						if hasattr(self.annotation,'xyann'): self.annotation.xyann=(x,y) # newer MPL versions (>=1.4)
						else: self.annotation.xyann=(x,y)
						self.annotation.set_text(self.annotation.annotateFmt.format(xy=(float(x),float(y))))
			except TypeError: pass # this happens at i386 with empty data, saying TypeError: buffer is too small for requested array
Exemplo n.º 2
0
def processOneImage(imageFile, imageLabel):

    image  = Image.open(imageFile, "rb");

    buffer = np.array(image);

    IMAGE_WIDTH   = string.atoi(G_OPTION.getValue("width"));
    IMAGE_HEIGHT  = string.atoi(G_OPTION.getValue("height"));
    IMAGE_CHANNEL = string.atoi(G_OPTION.getValue("channel"));

    buffer = buffer.reshape([IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNEL])

    mode = G_OPTION.getValue("mode")

    for char in mode:
        if(char == 'm'):
            #substract
            buffer=subtractmean(buffer)
            return
        else:
            #normalize
            if(char == 'n'):
                return
            else:
                #whitening
                if(char == "w"):
                    return
                else:
                    if(char == "c"):
                        return
	def calculate(self, imgs):
		try:
			img1 = Image.open(str(imgs[0]))
			img2 = Image.open(str(imgs[1]))


			if img1.size != img2.size or img1.getbands() != img2.getbands():
				return -1

			s = 0
			for band_index, band in enumerate(img1.getbands()):
				m1 = np.array([p[band_index] for p in img1.getdata()]).reshape(*img1.size)
				m2 = np.array([p[band_index] for p in img2.getdata()]).reshape(*img2.size)
				s += np.sum(np.abs(m1-m2))
			s = s/1000000

			(filepath1, filename1) = os.path.split(str(imgs[0]))
			(filepath2, filename2) = os.path.split(str(imgs[1]))
			label = filename1 + "-" + filename2

			return (label, s)
		except Exception, e:
			QMessageBox.warning(self, "Messaggio", str(e), QMessageBox.Ok)
Exemplo n.º 4
0
def generateMaps(mapFileName, numMaps, start_state, goal_state):
    # Inputs:
    #       mapFileName - string of the file name (including local path)
    #       numMaps - number of maps to be generated
    #
    # Outputs:
    #       generatedMap_cell - cell array containing numMaps+1 maps
    #                           the first element contains the seed map
    #

    import PIL 
    import matplotlib.plyplot as plt

    map_seed = PIL.open(mapFileName).convert('L')

    bridge_ind = numpy.where(map_seed != 0 & map_seed != 1)  # locations in the map where a bridge exists
    bridge_probs = map_seed[bridge_ind]                # probability for those bridges
    N = bridge_ind.shape[0]                             # Number of bridges

    # Holds the output for the generated bridges
    generatedMap_cell = []                

    for i in range(0,numMaps):
       temp_map = map_seed
       rand_vals = np.rand(N,1)   # generate a random value for each bridge
       bridge_vals = rand_vals < bridge_probs  # are the bridges open?
       temp_map[bridge_ind] = bridge_vals  # create the maps
       generatedMap_cell[i] = temp_map # store the maps in the cell array
       
       #plot the maps for testing
       plt.figure(i)
       plt.imshow(scipy.misc.imresize(temp_map,5, interp = 'nearest'))
       
    (N, M) = map_seed.shape
    (x, y) = numpy.meshgrid(numpy.arange(1,N+1), numpy.arange(1,M+1)) 

    map_struct['map_name'] = mapFileName
    map_struct['bridge_locations'] = np.array([x(bridge_ind).T, y(bridge_ind).T])
    map_struct['bridge_probabilities'] = bridge_probs
    map_struct['seed_map'] = map_seed
    map_struct['map_samples'] = generatedMap_cell
    map_struct['start'] = start_state
    map_struct['goal'] = goal_state
    def augment(self, img: PIL):
        augmented_images = []
        img = img.convert('RGB')
        augmented_images.append(img)  # append original images

        if self.augmentation_params.get('flip_left_right'):
            augmented_images.append(self.flip_left_right(img))

        if self.augmentation_params.get('flip_up_down'):
            augmented_images.append(self.flip_up_down(img))

        if self.augmentation_params.get('rotate_random_25'):
            augmented_images = augmented_images + self.rotate_random_25(img)

        if self.augmentation_params.get('blur'):
            augmented_images = augmented_images + self.blur(img)

        assert len(
            augmented_images
        ) == self.total_augmented_images + 1, "Augment error returning wrong values"
        return augmented_images
Exemplo n.º 6
0
def savePlotSequence(P,fileBase,stride=1,imgRatio=(5,7),title=None,titleFrames=20,lastFrames=30):
    '''Save sequence of plots, each plot corresponding to one line in history. It is especially meant to be used for :obj:`woo.utils.makeVideo`.

    :param stride: only consider every stride-th line of history (default creates one frame per each line)
    :param title: Create title frame, where lines of title are separated with newlines (``\\n``) and optional subtitle is separated from title by double newline. 
    :param int titleFrames: Create this number of frames with title (by repeating its filename), determines how long the title will stand in the movie.
    :param int lastFrames: Repeat the last frame this number of times, so that the movie does not end abruptly.
    :return: List of filenames with consecutive frames.
    '''
    data,imgData,plots=P.data,P.imgData,P.plots
    fig=createPlots(P,noShow=True,replace=True,subPlots=True,scatterSize=60,wider=True)[0]
    sqrtFigs=math.sqrt(len(plots))
    fig.set_size_inches(8*sqrtFigs,5*sqrtFigs) # better readable
    fig.subplots_adjust(left=.05,right=.95,bottom=.05,top=.95) # make it more compact
    if len(plots)==1 and plots[list(plots.keys())[0]]==None: # only pure snapshot is there
        fig.set_size_inches(5,5)
        fig.subplots_adjust(left=0,right=1,bottom=0,top=1)
    #if not data.keys(): raise ValueError("plot.data is empty.")
    pltLen=max(len(data[list(data.keys())[0]]) if data else 0,len(imgData[list(imgData.keys())[0]]) if imgData else 0)
    if pltLen==0: raise ValueError("Both plot.data and plot.imgData are empty.")
    global current
    ret=[]
    print('Saving %d plot frames, it can take a while...'%(pltLen))
    for i,n in enumerate(range(0,pltLen,stride)):
        current=n
        for l in P.currLineRefs: l.update()
        out=fileBase+'-%03d.png'%i
        fig.savefig(out)
        ret.append(out)
        sys.stderr.write('[%d]'%i)
    if len(ret)==0: raise RuntimeError("No images created?!")
    if title:
        import Image
        titleImgName=fileBase+'-title.png'
        createTitleFrame(titleImgName,Image.open(ret[-1]).size,title)
        ret=titleFrames*[titleImgName]+ret
    if lastFrames>1: ret+=(lastFrames-1)*[ret[-1]]
    return ret
Exemplo n.º 7
0
def savePlotSequence(P,fileBase,stride=1,imgRatio=(5,7),title=None,titleFrames=20,lastFrames=30):
	'''Save sequence of plots, each plot corresponding to one line in history. It is especially meant to be used for :obj:`woo.utils.makeVideo`.

	:param stride: only consider every stride-th line of history (default creates one frame per each line)
	:param title: Create title frame, where lines of title are separated with newlines (``\\n``) and optional subtitle is separated from title by double newline. 
	:param int titleFrames: Create this number of frames with title (by repeating its filename), determines how long the title will stand in the movie.
	:param int lastFrames: Repeat the last frame this number of times, so that the movie does not end abruptly.
	:return: List of filenames with consecutive frames.
	'''
	data,imgData,plots=P.data,P.imgData,P.plots
	fig=createPlots(P,noShow=True,replace=True,subPlots=True,scatterSize=60,wider=True)[0]
	sqrtFigs=math.sqrt(len(plots))
	fig.set_size_inches(8*sqrtFigs,5*sqrtFigs) # better readable
	fig.subplots_adjust(left=.05,right=.95,bottom=.05,top=.95) # make it more compact
	if len(plots)==1 and plots[plots.keys()[0]]==None: # only pure snapshot is there
		fig.set_size_inches(5,5)
		fig.subplots_adjust(left=0,right=1,bottom=0,top=1)
	#if not data.keys(): raise ValueError("plot.data is empty.")
	pltLen=max(len(data[data.keys()[0]]) if data else 0,len(imgData[imgData.keys()[0]]) if imgData else 0)
	if pltLen==0: raise ValueError("Both plot.data and plot.imgData are empty.")
	global current
	ret=[]
	print 'Saving %d plot frames, it can take a while...'%(pltLen)
	for i,n in enumerate(range(0,pltLen,stride)):
		current=n
		for l in P.currLineRefs: l.update()
		out=fileBase+'-%03d.png'%i
		fig.savefig(out)
		ret.append(out)
		sys.stderr.write('[%d]'%i)
	if len(ret)==0: raise RuntimeError("No images created?!")
	if title:
		import Image
		titleImgName=fileBase+'-title.png'
		createTitleFrame(titleImgName,Image.open(ret[-1]).size,title)
		ret=titleFrames*[titleImgName]+ret
	if lastFrames>1: ret+=(lastFrames-1)*[ret[-1]]
	return ret
Exemplo n.º 8
0
def user_color(original, new):
    '''allows user to change border colors from set options'''
    img = Image.open(original)  #opens original image
    #adds border to original
    color = raw_input('What color would you like the border to be?(Prompt \
will appear until all images are have a chosen color.): \n')
    time.sleep(1)
    thic = raw_input('What width would you like the border to be?(Prompt \
will appear until all images are have a chosen width.): \n')
    try:
        img_with_border = ImageOps.expand(img,
                                          border=int(thic),
                                          fill=str(color))
    except ValueError:
        img_with_border = ImageOps.expand(img, border=30, fill=str("blue"))
    try:
        img_with_border = ImageOps.expand(img,
                                          border=int(thic),
                                          fill=str(color))
    except TypeError:
        img_with_border = ImageOps.expand(img, border=30, fill=str("blue"))

    return img_with_border
Exemplo n.º 9
0
def rescale(data, width, height, force=True):
    """Rescale the given image, optionally cropping it to make sure the result image has the specified width and height."""

    max_width = width
    max_height = height

    input_file = StringIO(data)
    img = pil.open(input_file)
    if not force:
        img.thumbnail((max_width, max_height), pil.ANTIALIAS)
    else:
        src_width, src_height = img.size
        src_ratio = float(src_width) / float(src_height)
        dst_width, dst_height = max_width, max_height
        dst_ratio = float(dst_width) / float(dst_height)
        
        if dst_ratio < src_ratio:
            crop_height = src_height
            crop_width = crop_height * dst_ratio
            x_offset = float(src_width - crop_width) / 2
            y_offset = 0
        else:
            crop_width = src_width
            crop_height = crop_width / dst_ratio
            x_offset = 0
            y_offset = float(src_height - crop_height) / 3
        img = img.crop((x_offset, y_offset, x_offset+int(crop_width), y_offset+int(crop_height)))
        img = img.resize((dst_width, dst_height), pil.ANTIALIAS)
        
    tmp = StringIO()
    img.save(tmp, 'JPEG')
    tmp.seek(0)
    output_data = tmp.getvalue()
    input_file.close()
    tmp.close()
    
    return output_data
Exemplo n.º 10
0
def createPlots(P,subPlots=True,noShow=False,replace=True,scatterSize=60,wider=False):
	'''Create plots based on current data;

	:param subPlots: show all plots in one figure as subplots; otherwise, create multiple figures
	:param noShow: use headless backend for plots, and do not show plots on the screen
	:param replace: do not close existing figures, and do not update P.currLineRefs
	'''
	import logging
	data,imgData,plots,labels,xylabels,legendLoc,axesWd,annotateFmt=P.data,P.imgData,P.plots,P.labels,P.xylabels,P.legendLoc,P.axesWd,P.annotateFmt
	if replace:
		if P.currLineRefs:
			logging.info('Closing existing figures')
			ff=set([l.line.get_axes().get_figure() for l in P.currLineRefs]) # get all current figures
			for f in ff: pylab.close(f) # close those
		P.currLineRefs=[]
	figs=[]
	if len(plots)==0: return # nothing to plot
	if subPlots:
		# compute number of rows and colums for plots we have
		subCols=int(round(math.sqrt(len(plots)))); subRows=int(math.ceil(len(plots)*1./subCols))
		if wider: subRows,subCols=subCols,subRows
	# create a new figure; called once with subPlots, for each subplot without subPlots
	def _newFig():
		## pylab API		
		if not noShow: return pylab.figure() # this will go onto the screen; the pylab call sets up the windows as well
		else: # with noShow
			fig=matplotlib.figure.Figure()
			canvas=_HeadlessFigureCanvas(fig) # 
			return fig
	if subPlots: figs=[_newFig()]
	for nPlot,p in enumerate(plots.keys()):
		pStrip=p.strip().split('=',1)[0]
		if not subPlots:
			figs.append(_newFig())
			axes=figs[-1].add_subplot(1,1,1)
		else: axes=figs[-1].add_subplot(subRows,subCols,nPlot+1) # nPlot is 1-based in mpl, for matlab comatibility
		axes.grid(True)
		if plots[p]==None: # image plot
			if not pStrip in imgData.keys(): imgData[pStrip]=[]
			# fake (empty) image if no data yet
			import Image
			if len(imgData[pStrip])==0 or imgData[pStrip][-1]==None: img=Image.new('RGBA',(1,1),(0,0,0,0))
			else: img=Image.open(imgData[pStrip][-1])
			img=axes.imshow(img,origin='upper')
			if replace: P.currLineRefs.append(LineRef(line=img,scatter=None,annotation=None,line2=None,xdata=imgData[pStrip],ydata=None,imgData=imgData,dataName=pStrip))
			axes.set_axis_off()
			continue
		plots_p=[addPointTypeSpecifier(o) for o in tuplifyYAxis(plots[p])]
		plots_p_y1,plots_p_y2=[],[]; y1=True
		missing=set() # missing data columns
		if pStrip not in data.keys(): missing.add(pStrip.decode('utf-8','ignore'))
		for d in plots_p:
			if d[0]==None:
				y1=False; continue
			if not isinstance(d[0],(str,unicode)): raise ValueError('Plots specifiers must be strings (not %s)'%(type(d[0]).__name__))
			if y1: plots_p_y1.append(d)
			else: plots_p_y2.append(d)
			try:
				if (
					d[0] not in data.keys()
					# and not callable(d[0])
					and not (isinstance(d[0],(str,unicode)) and (d[0].startswith('**') or d[0].startswith('*'))) # hack for callable as strings
					# and not hasattr(d[0],'keys')
				):
					missing.add(d[0])
			except UnicodeEncodeError:
				import warnings
				warnings.error('UnicodeDecodeError when processing data set '+repr(d[0]))
		if missing:
			if len(data.keys())==0 or len(data[data.keys()[0]])==0: # no data at all yet, do not add garbage NaNs
				for m in missing: data[m]=[]
			else:
				addDataColumns(data,missing)
				try:
					print 'Missing columns in Scene.plot.data, added NaNs:',', '.join([m.encode('utf-8') for m in missing])
				except UnicodeDecodeError:
					warnings.warn('UnicodeDecodeError reporting missing data columns -- harmless, just wondering...')
		def createLines(pStrip,ySpecs,axes,isY1=True,y2Exists=False):
			'''Create data lines from specifications; this code is common for y1 and y2 axes;
			it handles y-data specified as callables/dicts passed as string (starting with '*'/'**'), which might create additional lines when updated with liveUpdate.
			'''
			# save the original specifications; they will be smuggled into the axes object
			# the live updated will run yNameFuncs to see if there are new lines to be added
			# and will add them if necessary
			yNameFuncs=set()
			yNames=set()
			ySpecs2=[]
			for ys in ySpecs:
				if not isinstance(ys[0],(str,unicode)): raise ValueError('Plot specifications must be strings (not a %s).'%type(ys[0]))
				if ys[0].startswith('**') or ys[0].startswith('*'):
					evEx=eval(ys[0][(2 if ys[0].startswith('**') else 1):],{'S':P.scene})
					yNameFuncs.add(evEx)  # add callable or dictionary
					# XXX: what is ys[1]? Previously, there was no line specifier there for dicts at least
					# print evEx,type(evEx), evEx.__iter__(),type(evEx.__iter__())
					ySpecs2+=[(ret,ys[1]) for ret in evEx] # traverse list or dict keys
				else: ySpecs2.append(ys)
			if len(ySpecs2)==0:
				print 'woo.plot: creating fake plot, since there are no y-data yet'
				line,=axes.plot([nan],[nan])
				line2,=axes.plot([nan],[nan])
				if replace: P.currLineRefs.append(LineRef(line=line,scatter=None,annotation=None,line2=line2,xdata=[nan],ydata=[nan]))
			# set different color series for y1 and y2 so that they are recognizable
			if matplotlib.rcParams.has_key('axes.color_cycle'): matplotlib.rcParams['axes.color_cycle']='b,g,r,c,m,y,k' if not isY1 else 'm,y,k,b,g,r,c'
			for d in ySpecs2:
				yNames.add(d)
				# should have been handled above already
				#if pStrip not in data:
				#	print 'Missing column %s in Scene.plot.data, added NaN.'%pString
				#	addDataColumns(data,[pStrip])
				if d[0] not in data:
					print 'Missing column %s in Scene.plot.data, added NaN.'%d[0]
					addDataColumns(data,[d[0]])
				line,=axes.plot(data[pStrip],data[d[0]],d[1],label=xlateLabel(d[0],P.labels),**lineKw)
				lineKwWithoutAlpha=dict([(k,v) for k,v in lineKw.items() if k!='alpha'])
				line2,=axes.plot([],[],d[1],color=line.get_color(),alpha=afterCurrentAlpha,**lineKwWithoutAlpha)
				# use (0,0) if there are no data yet
				scatterPt=[0,0] if len(data[pStrip])==0 else (data[pStrip][current],data[d[0]][current])
				scatterPtPos=[scatterPt[0] if not math.isnan(scatterPt[0]) else 0,scatterPt[1] if not math.isnan(scatterPt[1]) else 0]
				# if current value is NaN, use zero instead
				scatter=axes.scatter(scatterPtPos[0],scatterPtPos[1],s=scatterSize,color=line.get_color(),**scatterMarkerKw)
				if annotateFmt:
					if math.isnan(scatterPtPos[0]) or math.isnan(scatterPtPos[1]): text=''
					else: text=annotateFmt.format(xy=scatterPt)
					annotation=axes.annotate(text,xy=scatterPtPos,color=line.get_color(),**annotateKw)
					annotation.annotateFmt=annotateFmt
				else: annotation=None
				if replace: P.currLineRefs.append(LineRef(line=line,scatter=scatter,annotation=annotation,line2=line2,xdata=data[pStrip],ydata=data[d[0]]))
			axes=line.get_axes()
			labelLoc=(legendLoc[0 if isY1 else 1] if y2Exists>0 else 'best')
			l=axes.legend(loc=labelLoc)
			if l:
				l.get_frame().set_alpha(legendAlpha)
				if hasattr(l,'draggable'): l.draggable(True)
			if scientific:
				axes.ticklabel_format(style='sci',scilimits=(0,0),axis='both')
				# fixes scientific exponent placement for y2: https://sourceforge.net/mailarchive/forum.php?thread_name=20101223174750.GD28779%40ykcyc&forum_name=matplotlib-users
				if not isY1: axes.yaxis.set_offset_position('right')
			if isY1:
				axes.set_ylabel((', '.join([xlateLabel(_p[0],P.labels) for _p in ySpecs2])) if p not in xylabels or not xylabels[p][1] else xylabels[p][1])
				axes.set_xlabel(xlateLabel(pStrip,P.labels) if (p not in xylabels or not xylabels[p][0]) else xylabels[p][0])
			else:
				axes.set_ylabel((', '.join([xlateLabel(_p[0],P.labels) for _p in ySpecs2])) if (p not in xylabels or len(xylabels[p])<3 or not xylabels[p][2]) else xylabels[p][2])
			# if there are callable/dict ySpecs, save them inside the axes object, so that the live updater can use those
			if yNameFuncs:
				axes.wooYNames,axes.wooYFuncs,axes.wooXName,axes.wooLabelLoc=yNames,yNameFuncs,pStrip,labelLoc # prepend woo to avoid clashes
			if 0:
				# fix missing 'show' method; this has been fixed in matplotlib already, but we need to backport that
				# see https://github.com/matplotlib/matplotlib/commit/15fd0ae587a57cb1d7b69546eb359085315148c8
				# don't do that for headless backend, error there is fine
				fig=axes.get_figure()
				if not hasattr(fig,'show'):
					mgr=getattr(fig.canvas,'manager')
					if mgr: fig.show=lambda *args: mgr.window.show()
		createLines(pStrip,plots_p_y1,axes=axes,isY1=True,y2Exists=len(plots_p_y2)>0)
		if axesWd>0:
			axes.axhline(linewidth=axesWd,color='k')
			axes.axvline(linewidth=axesWd,color='k')
		# create y2 lines, if any
		if len(plots_p_y2)>0:
			axes=axes.twinx() # create the y2 axis
			createLines(pStrip,plots_p_y2,axes,isY1=False,y2Exists=True)
		### scene is not directly accessible from here, do it like this:
		S=woo.master.scene
		if S.plot==P:
			if 'title' in S.tags: axes.set_title(S.tags['title'])
	return figs
Exemplo n.º 11
0
#Play Video
import cv2
import mediapipe as mp
import streamlit as st
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
y=["image"]

temp_status = len(os.listdir("image"))

#img = None

uploaded_file = st.file_uploader("Alternatively upload a file")
if uploaded_file is not None:
    img = Image.open(uploaded_file)
    img.save("image/hands.jpg")
    st.success("Uploaded.")

# For static images:
hands = mp_hands.Hands(
    static_image_mode=True,
    max_num_hands=2,
    min_detection_confidence=0.5)

st.write("Raw image") 

try:
    st.image(img, use_column_width=True)
except NameError:
    pass
    def __init__(self, dataroot, train=True, augment=True):
        self.images = []
        self.bubbles = []
        self.labels = []

        dataset = []  # contains tuples of images and associated 360 bubbles
        if train:
            file = np.loadtxt(os.path.join(dataroot, "dataset_train.txt"),
                              dtype=str,
                              skiprows=3)
        else:
            file = np.loadtxt(os.path.join(dataroot, "dataset_test.txt"),
                              dtype=str,
                              skiprows=3)

        # load image pairs and create training/validation labels
        for pair in file:
            if 'right' in pair[1]:
                self.labels.extend([i for i in range(3, 6)])
            elif 'left' in pair[1]:
                self.labels.extend([i for i in range(0, 3)])

            dataset.extend([pair for i in range(3)])

        # calculate dataset length
        self.data_len = len(dataset)

        # transformations when loading images:
        PIL = transforms.ToPILImage()
        resize = transforms.Compose([transforms.Resize(300)])
        bub_size = transforms.Resize(500)
        if augment:
            self.image_trans = transforms.Compose([
                transforms.RandomCrop(224),
                transforms.ColorJitter(brightness=0.5,
                                       contrast=0.5,
                                       saturation=0.5,
                                       hue=0.05),
                transforms.ToTensor()
            ])
            self.bubble_trans = transforms.Compose([
                transforms.ColorJitter(brightness=0.5,
                                       contrast=0.5,
                                       saturation=0.5,
                                       hue=0.05),
                transforms.ToTensor()
            ])
        else:
            self.image_trans = transforms.Compose(
                [transforms.Resize(300),
                 transforms.ToTensor()])
            self.bubble_trans = transforms.Compose([transforms.ToTensor()])

        # load images and applying initial preprocessing
        for i, (bubble, image) in enumerate(dataset):
            image = io.imread(os.path.join(dataroot, image),
                              plugin='matplotlib')
            bubble = io.imread(os.path.join(dataroot, bubble),
                               plugin='matplotlib')
            label = torch.tensor(self.labels[i])

            # cropping parameters in height and width, this assumes images of shape (2100, 2800)
            params = [1300, 1300]

            # set left pixel of the image crop depending on label
            if label == 0 or label == 3:
                width = 0
            elif label == 1 or label == 4:
                width = int((image.shape[1] - params[1]) / 2)
            elif label == 2 or label == 5:
                width = int(image.shape[1] - (params[1] + 1))

            # set height of image crop
            height = int((image.shape[0] - params[0]) / 2)

            # pre-process image files
            image = PIL(image)
            image = TF.crop(image, height, width, params[0], params[1])
            image = resize(image)
            self.images.append(image)

            # preprocess bubble and add the array
            bubble = PIL(bubble)
            bubble = bub_size(bubble)
            self.bubbles.append(bubble)
Exemplo n.º 13
0
def get_rois(
    original_img: Image,
    PYR_SCALE: float,
    WIN_STEP: int,
    ROI_SIZE: tuple,
    visualize: int,
) -> Tuple[List[np.array], List[Tuple[float, float, float, float]]]:
    """
    Descrição
    ---------
    Itera sobre o metodo da piramide para gerar as regiões de interesse

    Entradas
    --------
    original_img (Image)
    Imagem de entrada

    PYR_SCALE (float)
    escala usada no metodo da piramide.
    WIN_STEP (int)
    passo entre cada window.

    ROI_SIZE (float)
    tamanho da janela.

    visualize (int)
    se > 0 entra em modo debug e visualiza cada janela na imagem original

    Saidas
    ------
    rois (tuple)
    regiões de interesse a serem classificadas

    locs (tuple)
    localização de cada roi na imagem original
    """
    # Inicializa a pirâmide da imagem
    pyramid = image_pyramid(original_img, scale=PYR_SCALE, min_size=ROI_SIZE)
    # Inicializa a lista de ROIs (Regiões de Interesse) geradas pela
    # pirâmide e pela sliding window, e a lista de coordenadas (x, y)
    # para guardar a posição de cada ROI na imagem original
    rois = []
    locs = []
    # Tempo inicial do pré-processamento da imagem original
    start = time.time()

    # Dimensões da imagem original
    (_, W) = original_img.shape[:2]

    # Aplica o algoritmo da pirâmide sobre a imagem
    for image in pyramid:
        # Obtém o fator de escala entre a imagem original e
        # a camada atual da pirâmide
        scale = W / float(image.shape[1])
        # Para cada camada da pirâmide, aplica o algoritmo de sliding window
        for (x, y, original_roi) in sliding_window(image, WIN_STEP, ROI_SIZE):
            # Reescala as coordenadas (x, y) da ROI com respeito às
            # dimensões da imagem original
            x = int(x * scale)
            y = int(y * scale)
            w = int(ROI_SIZE[0] * scale)
            h = int(ROI_SIZE[1] * scale)
            # Transforma a ROI (PIL Image) em array para a classificação
            # Atualiza a lista de ROIS e a lista de coordenadas
            rois.append(original_roi)
            locs.append((x, y, x + w, y + h))

            # Checa se as sliding windows devem ser mostradas
            if visualize > 0:
                # Clona a imagem original e então desenha as bounding boxes
                # representando a ROI atual
                clone = original_img.copy()
                cv2.rectangle(clone, (x, y), (x + w, y + h), (0, 255, 0), 2)
                # Mostra a ROI atual na tela
                cv2.imshow("Sliding Window", clone)
                cv2.imshow("ROI", original_roi)
                cv2.waitKey(0)

    # Fecha as sliding windows mostradas
    if visualize > 0:
        cv2.destroyAllWindows()

    # Tempo final do pré-processamento da imagem original
    end = time.time()
    # Duração do pré-processamento da imagem original
    print(
        f"[INFO] O pré-processamento da imagem durou {end - start:.5f} segundos"
    )

    return rois, locs
Exemplo n.º 14
0
    """
    ha, wa = imga.shape[:2]
    hb, wb = imgb.shape[:2]
    max_height = np.max([ha, hb])
    total_width = wa + wb
    new_img = np.zeros(shape=(max_height, total_width, 3))
    new_img[:ha, :wa] = imga
    new_img[:hb, wa:wa + wb] = imgb
    return new_img


def concat_n_images(image_path_list):
    """
    Combines N color images from a list of image paths.
    """
    output = None
    for i, img_path in enumerate(image_path_list):
        img = plt.imread(img_path)[:, :, :3]
        if i == 0:
            output = img
        else:
            output = concat_images(output, img)
    return output


for i in glob.glob('D:/GitHub/GitTesis/RAW DATA/*.img'):
    fopen = PIL.open(i)
    output = concat_n_images(fopen)

    plt.imshow(output)
    plt.show()
import PIL

img = PIL.Image("1217-Steve-Jobs-Quote-Think-Different.jpg")
win = PIL.ImageWin(img.getWidth(), img.getHeight())
img.draw(win)
img.setDelay(1, 15)  # setDelay(0) turns off animation

for row in range(img.getHeight()):
    for col in range(img.getWidth()):
        p = PIL.getPixel(col, row)

        newred = 255 - p.getRed()
        newgreen = 255 - p.getGreen()
        newblue = 255 - p.getBlue()

        newpixel = PIL.Pixel(newred, newgreen, newblue)

        img.setPixel(col, row, newpixel)

img.draw(win)
win.exitonclick()
Exemplo n.º 16
0
def border_one(original_image, percent_of_side=.3):
    """ Rounds the corner of a PIL.Image
        original_image must be a PIL.Image
        Returns a new PIL.Image with rounded corners, where
        0 < percent_of_side < 1 is the corner radius as a portion of the shorter 
        dimension of original_image
    """
    #set the radius of the rounded corners
    width, height = original_image.size

    ###
    #create a mask
    ###

    #start with transparent mask
    border_mask = PIL.Image.new('RGBA', (width, height), (127, 0, 127, 0))
    drawing_layer = PIL.ImageDraw.Draw(border_mask)

    # Overwrite the RGBA values with A=255.
    # The 127 for RGB values was used merely for visualizing the mask

    # Draw two rectangles to fill interior with opaqueness
    if width >= 300 or height >= 300:
        drawing_layer.polygon([(0, 0), (width, 0), (width, height),
                               (0, height)],
                              fill=(127, 0, 127, 170))

        drawing_layer.polygon([(0, 0), (30, 0), (30, height), (0, height)],
                              fill=(0, 0, 255, 0),
                              outline=(0, 255, 0, 1))
        drawing_layer.polygon([(width, 0), (width - 30, 0),
                               (width - 30, height), (width, height)],
                              fill=(0, 0, 255, 0),
                              outline=(0, 255, 0, 1))
        drawing_layer.polygon([(0, 0), (width, 0), (width, 30), (0, 30)],
                              fill=(0, 0, 255, 0),
                              outline=(0, 255, 0, 1))
        drawing_layer.polygon([(0, height), (width, height),
                               (width, height - 30), (0, height - 30)],
                              fill=(0, 0, 255, 0),
                              outline=(0, 255, 0, 1))

        # Make the new image, starting with all transparent
        result = PIL.Image.new('RGBA', original_image.size, (136, 39, 16, 255))
        result.paste(original_image, (0, 0), mask=border_mask)

        img = Image.open('ourlogo_test.png')
        logo_small = img.resize((85, 93))
        result.paste(logo_small, (width - 130, 40), mask=logo_small)

        return result

    elif width <= 300 and height <= 300:

        original_image.size = 500, 500

        drawing_layer.polygon([(0, 0), (width, 0), (width, height),
                               (0, height)],
                              fill=(127, 0, 127, 170))

        drawing_layer.polygon([(0, 0), (20, 0), (20, height), (0, height)],
                              fill=(0, 0, 255, 0),
                              outline=(0, 255, 0, 1))
        drawing_layer.polygon([(width, 0), (width - 20, 0),
                               (width - 20, height), (width, height)],
                              fill=(0, 0, 255, 0),
                              outline=(0, 255, 0, 1))
        drawing_layer.polygon([(0, 0), (width, 0), (width, 20), (0, 20)],
                              fill=(0, 0, 255, 0),
                              outline=(0, 255, 0, 1))
        drawing_layer.polygon([(0, height), (width, height),
                               (width, height - 30), (0, height - 30)],
                              fill=(0, 0, 255, 0),
                              outline=(0, 255, 0, 1))

        # Make the new image, starting with all transparent
        result = PIL.Image.new('RGBA', original_image.size, (136, 39, 16, 255))
        result.paste(original_image, (0, 0), mask=border_mask)

        img = Image.open('ourlogo_test.png')
        logo_small = img.resize((10, 23))
        result.paste(logo_small, (width - 130, 40), mask=logo_small)

        return result
Exemplo n.º 17
0
def createPlots(P,subPlots=True,noShow=False,replace=True,scatterSize=60,wider=False):
    '''Create plots based on current data;

    :param subPlots: show all plots in one figure as subplots; otherwise, create multiple figures
    :param noShow: use headless backend for plots, and do not show plots on the screen
    :param replace: do not close existing figures, and do not update P.currLineRefs
    '''
    import logging
    data,imgData,plots,labels,xylabels,legendLoc,axesWd,annotateFmt=P.data,P.imgData,P.plots,P.labels,P.xylabels,P.legendLoc,P.axesWd,P.annotateFmt
    if replace:
        if P.currLineRefs:
            logging.info('Closing existing figures')
            ff=set([_my_get_axes(l.line).get_figure() for l in P.currLineRefs]) # get all current figures
            for f in ff: pylab.close(f) # close those
        P.currLineRefs=[]
    figs=[]
    if len(plots)==0: return # nothing to plot
    if subPlots:
        # compute number of rows and colums for plots we have
        subCols=int(round(math.sqrt(len(plots)))); subRows=int(math.ceil(len(plots)*1./subCols))
        if wider: subRows,subCols=subCols,subRows
    # create a new figure; called once with subPlots, for each subplot without subPlots
    def _newFig():
        ## pylab API        
        if not noShow: return pylab.figure() # this will go onto the screen; the pylab call sets up the windows as well
        else: # with noShow
            fig=matplotlib.figure.Figure()
            canvas=_HeadlessFigureCanvas(fig) # 
            return fig
    if subPlots: figs=[_newFig()]
    for nPlot,p in enumerate(plots.keys()):
        pStrip=p.strip().split('=',1)[0]
        if not subPlots:
            figs.append(_newFig())
            axes=figs[-1].add_subplot(1,1,1)
        else: axes=figs[-1].add_subplot(subRows,subCols,nPlot+1) # nPlot is 1-based in mpl, for matlab comatibility
        axes.grid(True)
        if plots[p]==None: # image plot
            if not pStrip in list(imgData.keys()): imgData[pStrip]=[]
            # fake (empty) image if no data yet
            import Image
            if len(imgData[pStrip])==0 or imgData[pStrip][-1]==None: img=Image.new('RGBA',(1,1),(0,0,0,0))
            else: img=Image.open(imgData[pStrip][-1])
            img=axes.imshow(img,origin='upper')
            if replace: P.currLineRefs.append(LineRef(line=img,scatter=None,annotation=None,line2=None,xdata=imgData[pStrip],ydata=None,imgData=imgData,dataName=pStrip))
            axes.set_axis_off()
            continue
        plots_p=[addPointTypeSpecifier(o) for o in tuplifyYAxis(plots[p])]
        plots_p_y1,plots_p_y2=[],[]; y1=True
        missing=set() # missing data columns
        if pStrip not in list(data.keys()): missing.add(pStrip if isinstance(pStrip,str) else pStrip.decode('utf-8','ignore'))
        for d in plots_p:
            d0=d[0]
            if d0==None:
                y1=False; continue
            if not isinstance(d0,(past.builtins.str,str)): raise ValueError('Plots specifiers must be strings (not %s)'%(type(d[0]).__name__))
            if '=' in d0: d0=d0.split('=',1)[0]
            if y1: plots_p_y1.append((d0,d[1]))
            else: plots_p_y2.append((d0,d[1]))
            try:
                if (
                    d0 not in list(data.keys())
                    # and not callable(d[0])
                    and not (isinstance(d0,str) and (d0.startswith('**') or d0.startswith('*'))) # hack for callable as strings
                    # and not hasattr(d[0],'keys')
                ):
                    missing.add(d0)
            except UnicodeEncodeError:
                import warnings
                warnings.error('UnicodeDecodeError when processing data set '+repr(d[0]))
        if missing:
            if len(list(data.keys()))==0 or len(data[list(data.keys())[0]])==0: # no data at all yet, do not add garbage NaNs
                for m in missing: data[m]=[]
            else:
                addDataColumns(data,missing)
                try:
                    print('Missing columns in Scene.plot.data, added NaNs:',', '.join([m for m in missing]))
                except UnicodeDecodeError:
                    warnings.warn('UnicodeDecodeError reporting missing data columns -- harmless, just wondering...')
        def createLines(pStrip,ySpecs,axes,isY1=True,y2Exists=False):
            '''Create data lines from specifications; this code is common for y1 and y2 axes;
            it handles y-data specified as callables/dicts passed as string (starting with '*'/'**'), which might create additional lines when updated with liveUpdate.
            '''
            # save the original specifications; they will be smuggled into the axes object
            # the live updated will run yNameFuncs to see if there are new lines to be added
            # and will add them if necessary
            yNameFuncs=set()
            yNames=set()
            ySpecs2=[]
            for ys in ySpecs:
                if not isinstance(ys[0],(past.builtins.str,str)): raise ValueError('Plot specifications must be strings (not a %s).'%type(ys[0]))
                if ys[0].startswith('**') or ys[0].startswith('*'):
                    evEx=eval(ys[0][(2 if ys[0].startswith('**') else 1):],{'S':P.scene})
                    yNameFuncs.add(evEx)  # add callable or dictionary
                    # XXX: what is ys[1]? Previously, there was no line specifier there for dicts at least
                    # print evEx,type(evEx), evEx.__iter__(),type(evEx.__iter__())
                    ySpecs2+=[(ret,ys[1]) for ret in evEx] # traverse list or dict keys
                else: ySpecs2.append(ys)
            if len(ySpecs2)==0:
                print('woo.plot: creating fake plot, since there are no y-data yet')
                line,=axes.plot([nan],[nan])
                line2,=axes.plot([nan],[nan])
                if replace: P.currLineRefs.append(LineRef(line=line,scatter=None,annotation=None,line2=line2,xdata=[nan],ydata=[nan]))
            # set different color series for y1 and y2 so that they are recognizable
            if 'axes.color_cycle' in matplotlib.rcParams: matplotlib.rcParams['axes.color_cycle']='b,g,r,c,m,y,k' if not isY1 else 'm,y,k,b,g,r,c'
            for d in ySpecs2:
                yNames.add(d)
                # should have been handled above already
                #if pStrip not in data:
                #    print 'Missing column %s in Scene.plot.data, added NaN.'%pString
                #    addDataColumns(data,[pStrip])
                if d[0] not in data:
                    print('Missing column %s in Scene.plot.data, added NaN.'%d[0])
                    addDataColumns(data,[d[0]])
                line,=axes.plot(data[pStrip],data[d[0]],d[1],label=xlateLabel(d[0],P.labels),**lineKw)
                lineKwWithoutAlpha=dict([(k,v) for k,v in list(lineKw.items()) if k!='alpha'])
                line2,=axes.plot([],[],d[1],color=line.get_color(),alpha=afterCurrentAlpha,**lineKwWithoutAlpha)
                # use (0,0) if there are no data yet
                scatterPt=[0,0] if len(data[pStrip])==0 else (data[pStrip][current],data[d[0]][current])
                scatterPtPos=[scatterPt[0] if not math.isnan(scatterPt[0]) else 0,scatterPt[1] if not math.isnan(scatterPt[1]) else 0]
                # if current value is NaN, use zero instead
                scatter=axes.scatter(scatterPtPos[0],scatterPtPos[1],s=scatterSize,color=line.get_color(),**scatterMarkerKw)
                if annotateFmt:
                    if math.isnan(scatterPtPos[0]) or math.isnan(scatterPtPos[1]): text=''
                    else: text=annotateFmt.format(xy=scatterPt)
                    annotation=axes.annotate(text,xy=scatterPtPos,color=line.get_color(),**annotateKw)
                    annotation.annotateFmt=annotateFmt
                else: annotation=None
                if replace: P.currLineRefs.append(LineRef(line=line,scatter=scatter,annotation=annotation,line2=line2,xdata=data[pStrip],ydata=data[d[0]]))
            axes=_my_get_axes(line)
            labelLoc=(legendLoc[0 if isY1 else 1] if y2Exists>0 else 'best')
            l=axes.legend(loc=labelLoc)
            if l:
                l.get_frame().set_alpha(legendAlpha)
                if hasattr(l,'draggable'): l.draggable(True)
            if scientific:
                axes.ticklabel_format(style='sci',scilimits=(0,0),axis='both')
                # fixes scientific exponent placement for y2: https://sourceforge.net/mailarchive/forum.php?thread_name=20101223174750.GD28779%40ykcyc&forum_name=matplotlib-users
                if not isY1: axes.yaxis.set_offset_position('right')
            if isY1:
                axes.set_ylabel((', '.join([xlateLabel(_p[0],P.labels) for _p in ySpecs2])) if p not in xylabels or not xylabels[p][1] else xylabels[p][1])
                axes.set_xlabel(xlateLabel(pStrip,P.labels) if (p not in xylabels or not xylabels[p][0]) else xylabels[p][0])
            else:
                axes.set_ylabel((', '.join([xlateLabel(_p[0],P.labels) for _p in ySpecs2])) if (p not in xylabels or len(xylabels[p])<3 or not xylabels[p][2]) else xylabels[p][2])
            # if there are callable/dict ySpecs, save them inside the axes object, so that the live updater can use those
            if yNameFuncs:
                axes.wooYNames,axes.wooYFuncs,axes.wooXName,axes.wooLabelLoc=yNames,yNameFuncs,pStrip,labelLoc # prepend woo to avoid clashes
            if 0:
                # fix missing 'show' method; this has been fixed in matplotlib already, but we need to backport that
                # see https://github.com/matplotlib/matplotlib/commit/15fd0ae587a57cb1d7b69546eb359085315148c8
                # don't do that for headless backend, error there is fine
                fig=axes.get_figure()
                if not hasattr(fig,'show'):
                    mgr=getattr(fig.canvas,'manager')
                    if mgr: fig.show=lambda *args: mgr.window.show()
        createLines(pStrip,plots_p_y1,axes=axes,isY1=True,y2Exists=len(plots_p_y2)>0)
        if axesWd>0:
            axes.axhline(linewidth=axesWd,color='k')
            axes.axvline(linewidth=axesWd,color='k')
        # create y2 lines, if any
        if len(plots_p_y2)>0:
            axes=axes.twinx() # create the y2 axis
            createLines(pStrip,plots_p_y2,axes,isY1=False,y2Exists=True)
        ### scene is not directly accessible from here, do it like this:
        S=woo.master.scene
        if S.plot==P:
            if 'title' in S.tags: axes.set_title(S.tags['title'])
    return figs
Exemplo n.º 18
0
 def update(self):
     if isinstance(self.line,matplotlib.image.AxesImage):
         # image name
         try:
             if len(self.xdata)==0 and self.dataName: self.xdata=self.imgData[self.dataName]  # empty list reference an empty singleton, not the list we want; adjust here
             import Image
             if self.xdata[current]==None: img=Image.new('RGBA',(1,1),(0,0,0,0))
             else: img=Image.open(self.xdata[current])
             self.line.set_data(img)
         except IndexError: pass
     else:
         # regular data
         import numpy
         # current==-1 avoids copy slicing data in the else part
         if current==None or current==-1 or afterCurrentAlpha==1:
             self.line.set_xdata(self.xdata); self.line.set_ydata(self.ydata)
             self.line2.set_xdata([]); self.line2.set_ydata([])
         else:
             try: # try if we can extend the first part by one so that lines are connected
                 self.xdata[:current+1]; preCurrEnd=current+1
             except IndexError: preCurrEnd=current
             preCurrEnd=current+(1 if len(self.xdata)>current else 0)
             self.line.set_xdata(self.xdata[:preCurrEnd]); self.line.set_ydata(self.ydata[:preCurrEnd])
             self.line2.set_xdata(self.xdata[current:]); self.line2.set_ydata(self.ydata[current:])
         try:
             x,y=self.xdata[current],self.ydata[current]
         except IndexError: x,y=0,0
         # this could be written in a nicer way, very likely
         try:
             pt=numpy.ndarray((2,),buffer=numpy.array([float(x),float(y)]))
             if self.scatter:
                 self.scatter.set_offsets(pt)
                 # change rotation of the marker (possibly incorrect)
                 try:
                     dx,dy=self.xdata[current]-self.xdata[current-1],self.ydata[current]-self.ydata[current-1]
                     # smoothing from last n values, if possible
                     # FIXME: does not show arrow at all if less than window values
                     #try:
                     #    window=10
                     #    dx,dy=[numpy.average(numpy.diff(dta[current-window:current])) for dta in self.xdata,self.ydata]
                     #except IndexError: pass
                     # there must be an easier way to find on-screen derivative angle, ask on the matplotlib mailing list
                     # XXX: this can be siplified once we drop support for matplotlib < 1.5
                     # XXX: and is here to avoid MatplotlibDeprecationWarning for newer versions
                     axes=_my_get_axes(self.line)
                     p=axes.patch; xx,yy=p.get_verts()[:,0],p.get_verts()[:,1]; size=max(xx)-min(xx),max(yy)-min(yy)
                     aspect=(size[1]/size[0])*(1./axes.get_data_ratio())
                     if dx==0: angle=math.pi
                     angle=math.atan(aspect*dy/dx)
                     if dx<0: angle-=math.pi
                     self.scatter.set_transform(matplotlib.transforms.Affine2D().rotate(angle))
                 except IndexError: pass
             if self.annotation:
                 if math.isnan(x) or math.isnan(y):
                     if hasattr(self.annotation,'xyann'): self.annotation.xyann=(x,y)
                     else: self.annotation.xytext=(0,0)
                     self.annotation.set_text('') # make invisible, place anywhere
                 else:
                     #
                     if hasattr(self.annotation,'xyann'): self.annotation.xyann=(x,y) # newer MPL versions (>=1.4)
                     else: self.annotation.xyann=(x,y)
                     self.annotation.set_text(self.annotation.annotateFmt.format(xy=(float(x),float(y))))
         except TypeError: pass # this happens at i386 with empty data, saying TypeError: buffer is too small for requested array
Exemplo n.º 19
0
import PIL as IMAGE

file_src = 'img processing/imgs/fire/T0t0.jpg'  #需要压缩的图片地址
img_src = 'img processing/imgs/fire/yasuo.jpg'  #压缩后的图片地址
img = IMAGE.open(file_src)
w, h = img.size
w, h = round(w * 0.2), round(h * 0.2)  # 去掉浮点,防报错

img = img.resize((w, h), IMAGE.ANTIALIAS)
img.save(img_src, optimize=True, quality=85)  #	 质量为85效果最好
Exemplo n.º 20
0
    plt.figure()
    plt.scatter(lidarxx, lidaryy, color=colors, s=0.5)
    plt.scatter(edgexx, edgeyy, color='k', s=0.5)
    plt.xlim([0, w])
    plt.ylim([h, 0])

    edgeptsind = lidardepthmapIndrec[edges]
    edgepts3d = lidar_camcoord[edgeptsind, :]
    edgepts3dprojected = (intrinsic @ edgepts3d.T).T
    edgepts3dprojected[:,
                       0] = edgepts3dprojected[:, 0] / edgepts3dprojected[:, 2]
    edgepts3dprojected[:,
                       1] = edgepts3dprojected[:, 1] / edgepts3dprojected[:, 2]
    edgepts3dprojected = edgepts3dprojected[:, 0:3]
    plt.figure()
    plt.imshow(pil.fromarray(rgbarr))
    distances = edgepts3dprojected[:, 2]
    colors = cm.jet(1 / distances * 10)
    plt.gca().scatter(edgepts3dprojected[:, 0],
                      edgepts3dprojected[:, 1],
                      color=colors,
                      s=0.5)
    plt.xlim([0, w])
    plt.ylim([h, 0])

    from utils import *
    edges = (bsmvrec > 0) * (bsmvrec < 0.1)

    testsets = [[1610, 558, 1609, 554], [1610, 558, 1606, 558],
                [407, 618, 408, 613], [332, 587, 331, 583],
                [119, 743, 116, 734], [879, 651, 880, 645],
Exemplo n.º 21
0
        i += 1

    #saving model
    torch.save(cn.state_dict(), model_save_path)
    print('обучение закончено')

cn = ConvNet(num_classes)
cn.load_state_dict(torch.load(model_save_path))

batch = next(iter(signsValidationLoader))
predictions = cn(batch['image'])
y_test = batch['label']

#print(predictions, y_test)
_, predictions = torch.max(predictions, 1)
plt.imshow(PIL.ToPIL(batch['image'][0]))
print('Gound-true:', dataset.labels[batch['label'][0]])
print('Prediction:', dataset.labels[predictions[0]])

#####Кривые обучения


def smooth_curve(points, factor=0.9):
    smoothed_points = []
    for point in points:
        if smoothed_points:
            previous = smoothed_points[-1]
            smoothed_points.append(previous * factor + point * (1 - factor))
        else:
            smoothed_points.append(point)
    return smoothed_points
Exemplo n.º 22
0
            k[int(gt[i][1]), int(gt[i][0])] = 1

    # generate density map
    k = gaussian_filter_density(k)

    # File path to save density map
    file_path = img_path.replace('.jpg', '.h5').replace('images', 'ground')

    with h5py.File(file_path, 'w') as hf:
        hf['density'] = k

# In[10]:

file_path = img_paths[22].replace('.jpg', '.h5').replace('images', 'ground')
print(file_path)

# In[12]:

#Sample Ground Truth
gt_file = h5py.File(file_path, 'r')
groundtruth = np.asarray(gt_file['density'])
plt.imshow(groundtruth, cmap=CM.jet)
print("Sum = ", np.sum(groundtruth))

# In[13]:

# Image corresponding to the ground truth
img = Image.open(file_path.replace('.h5', '.jpg').replace('ground', 'images'))
plt.imshow(img)
print(file_path.replace('.h5', '.jpg').replace('ground', 'images'))
Exemplo n.º 23
0
import cyni
import numpy as np
import PIL as Image

cyni.initialize()
device = cyni.getAnyDevice()
device.open()
depthStream = device.createStream("depth", fps=30, width=640, height=480)
#colorStream = device.createStream("color", fps=30, width=1280, height=960)
#colorStream = device.createStream("color", fps=30, width=640, height=480)
#device.setImageRegistrationMode("depth_to_color")
device.setDepthColorSyncEnabled(on=True)
depthStream.start()
# colorStream.start()
# colorFrame = colorStream.readFrame()
# colorFrame = colorStream.readFrame()
# colorFrame = colorStream.readFrame()
# colorFrame = colorStream.readFrame()
depthFrame = depthStream.readFrame()
# registered = cyni.registerColorImage(depthFrame.data, colorFrame.data, depthStream, colorStream)
# Image.fromarray(colorFrame.data).save("color.png")
# Image.fromarray(registered).save("registered.png")
Image.fromarray(cyni.depthMapToImage(depthFrame.data)).save("depth.png")
Exemplo n.º 24
0
'''
Python script to crunch sprite assets in Unity by a constant value (for reducing filesize with big images
This automatically applies to .meta files, so nothing changes in game, and it works on Multiple sprite assets as well.
PIL (Python Image Library) needs to be installed to use this. 

@author: Brian Intile
'''

import os

from PIL import Image
import PIL


def get_immediate_subdirectories(directory):
    return [
        name for name in os.listdir(directory)
        if os.path.isdir(os.path.join(directory, name))
    ]


def browse_for_traits(directory, recurse, mult):
    for dir_0 in os.listdir(directory):
        if (os.path.isfile(directory + u'\\' + dir_0) &
            ((dir_0.split('.')[-1].lower() == u'png'.lower()) |
             (dir_0.split('.')[-1].lower() == u'jpg'.lower()))):
            change_value_names(directory + '\\' + dir_0, mult)
    if (recurse):
        directories = get_immediate_subdirectories(directory)
        for x in range(0, len(directories)):
Exemplo n.º 25
0
# -*- coding: utf-8 -*-
"""
Created on Tue Oct  3 17:13:57 2017

@author: cgowl_000
"""
import matplotlib as plt 
import numpy as np
import PIL  as im
r = [[1,0],[0,0]]
print(r)
g = [[0,1],[0,0]]
b = [[0,0],[1,0]]

stack = np.dstack((r,b,g))
im.Image(stack)
im.
Exemplo n.º 26
0
import PIL as Image

pil_im = Image.open('C:\\Users\\Arsalan\\Desktop\\myprogress.PNG')

print(pil_im)
Exemplo n.º 27
0
def fetch_card_img_s3(name, bucket=os.environ.get('POKEDEXR_S3')):
    s3 = boto3.resource('s3')
    x = s3.Object(bucket, f'card_images/original/{name}').get()
    src_img = PIL.imread(BytesIO(x['Body'].read()), 0)
    return src_img
Exemplo n.º 28
0
import PIL
import numpy as np

img_path = 'data/images_train/data/123033/0e27621e3f8b484c993619abd064948be27f60a2.jpg'
img = PIL.imread(img_path)
img_array = np.asarray(img)
print(img_array.shape)
Exemplo n.º 29
0
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 18 12:08:18 2014

@author: girish
"""

import PIL

from scipy.ndimage import filters
from PIL import Image

from pylab import *
import os

im = PIL.Image("banana.jpg")
plot(im)
Exemplo n.º 30
0
def filter_detections(
    original_img: Image,
    preds: List[tuple],
    locs: np.array,
    min_conf: float,
    visualize: int,
) -> Type[None]:
    """
    Definição
    ---------
    Mostra as predições

    Entradas
    --------
    original_img (Image)
    imagem original.

    preds (List[tuple])
    lista com as prediçoes (indice, classe, probabilidade).

    locs (np.array)
    localização das janelas classificadas

    min_conf (float)
    probabilidade minima para se considerar uma detecçao valida

    visualize (int)
    variavel de debub, se >0 permite visualizar passos intermediarios do processo
    """
    # Inicializa dicionário de predições separado por classes

    labels = {}

    # Itera sobre todas as predições de cada ROI
    for (i, p) in enumerate(preds):
        # Separa as informações da ROI atual
        (_, label, prob) = p
        # Filtra as detecções com baixa prioridade de acordo com min_conf
        if prob >= min_conf:
            # Seleciona a bounding box da predição
            box = locs[i]
            # Caso a chave label exista no dicionário labels, guarda seu valor em lab
            # Se não, guarda uma lista vazia em lab
            lab = labels.get(label, [])
            # Adiciona a bounding box e sua probabilidade no dicionário de labels
            lab.append((box, prob))
            labels[label] = lab

    # Itera sobre cada uma das classes detectadas
    # Clona a imagem original para desenhar as bounding boxes
    clone = original_img.copy()
    for label in labels:

        # Checa se as bounding boxes encontradas devem ser mostradas
        if visualize > 0:
            # Clona a imagem original para desenhar as bounding boxes
            clone = original_img.copy()
            # Itera sobre todas as bounding boxes da classe atual
            for (box, _) in labels[label]:
                # Desenha as bounding bouxes na imagem
                (startX, startY, endX, endY) = box
                cv2.rectangle(
                    clone, (startX, startY), (endX, endY), (0, 255, 0), 2
                )
            cv2.imshow("Debug", clone)

        # Aplica a técnica de non-maxima supression nas predições da classe atual
        boxes = np.array([p[0] for p in labels[label]])
        proba = np.array([p[1] for p in labels[label]])
        boxes = non_max_suppression(boxes, proba)
        # Itera sobre as bounding boxes remanescenes
        for (startX, startY, endX, endY) in boxes:
            # Desenha as bounding bouxes na imagem
            cv2.rectangle(
                clone, (startX, startY), (endX, endY), (0, 255, 0), 2
            )
            y = startY - 10 if startY - 10 > 10 else startY + 10
            cv2.putText(
                clone,
                label,
                (startX, y),
                cv2.FONT_HERSHEY_SIMPLEX,
                0.45,
                (0, 255, 0),
                2,
            )
    return clone
Exemplo n.º 31
0
    gt = mat["image_info"][0, 0][0, 0][0]

    # Read image
    img = plt.imread(img_path)

    # Create a zero matrix of image size
    k = np.zeros((img.shape[0], img.shape[1]))

    # Generate hot encoded matrix of sparse matrix
    for i in range(0, len(gt)):
        if int(gt[i][1]) < img.shape[0] and int(gt[i][0]) < img.shape[1]:
            k[int(gt[i][1]), int(gt[i][0])] = 1

    # generate density map
    k = gaussian_filter_density(k)
    image = Image.fromarray(255 * k)
    # image.show()

    # File path to save density map
    file_path = img_path.replace('.jpg', '.h5').replace('images', 'ground')

    with h5py.File(file_path, 'w') as hf:
        hf['density'] = k

# %%
path_test = "G:/pycharm/CSRnet-master/data/part_A_final/train_data/ground/IMG_107.h5"

# file_path = img_paths[-5].replace('.jpg','.h5').replace('images','ground')

file_path = path_test
print(file_path)
Exemplo n.º 32
0
def imageToNP(imageName):
    try:
        npa=pil.Image(imageName)
        return npa
    except(Exception):
        print("error")