def test_with_pro_2(rawImg,pro): ''' Use this fuc when ### above are first implement''' ref = rawImg.copy() img = auto_resized(rawImg,conf['train_resolution']) img_gray = cv2.cvtColor( img , cv2.COLOR_BGR2GRAY) roi = img_gray (boxes, probs) = od.detect(roi, winStep=conf["step_size"], winDim=conf["sliding_size"], pyramidScale=1.1, minProb=pro) # since for size effect with the camera, pyramidScale = 1.001, mnust>1, # if positive size would change, we have to use 1.5 or 2 ...etc pick = non_max_suppression(np.array(boxes), probs, conf["overlap_thresh"]) orig = img.copy() # Resize Back, I am GOD !!!!! y_sizeFactor = ref.shape[0]/float(img.shape[0]) x_sizeFactor = ref.shape[1]/float(img.shape[1]) # loop over the allowed bounding boxes and draw them for (startX, startY, endX, endY) in pick: #cv2.rectangle(orig, (startX, startY), (endX, endY), (0, 255, 0), 2) startX = int(startX* x_sizeFactor) endX = int(endX * x_sizeFactor) startY = int(startY* y_sizeFactor) endY = int(endY * y_sizeFactor) cv2.rectangle(ref, (startX, startY), (endX, endY), (0, 255, 0), 2) cv2.putText(ref, "Hodling SkrewDriver", (startX, startY), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 4) #print (startX, startY), (endX, endY) show(ref) return ref, pick
def test_with_hc_222(frame_id,pro): ''' Use this fuc when ### above are first implement''' img = vid.get_data(frame_id) img = auto_resized(img,conf['train_resolution']) # ROI roi roi = img[20:180,35:345] # use check_hsv img_gray = check_hsv(roi) (boxes, probs) = od.detect(img_gray, winStep=conf["step_size"], winDim=conf["sliding_size"], pyramidScale=1.1, minProb=pro) # since for size effect with the camera, pyramidScale = 1.001, mnust>1, # if positive size would change, we have to use 1.5 or 2 ...etc pick = non_max_suppression(np.array(boxes), probs, conf["overlap_thresh"]) orig = img.copy() # loop over the allowed bounding boxes and draw them for (startX, startY, endX, endY) in pick: if startY < 50 and startX < 50: pass elif abs(startX - endX)>80: # Size effect pass elif (check_hsv2(img)[startY+20:endY+20,startX+35:endX+35]).sum()<255*300: # White region = 255 print (startX, startY, endX, endY) print '[*] White Value Index must > 20' print (check_hsv2(img)[startY+20:endY+20,startX+35:endX+35]).sum() print '[*] End' else: cv2.rectangle(orig, (startX+35, startY+20), (endX+35, endY+20), (0, 255, 0), 2) show(orig) return img_gray, orig
def test_with_hc_v2(frame_id,pro,falsePositive=False,falseNegative=False): ''' Use this fuc when ### above are first implement''' img = vid.get_data(frame_id) img = auto_resized(img,conf['train_resolution']) img_gray = cv2.cvtColor( img , cv2.COLOR_BGR2GRAY) roi = img_gray[20:180,35:345] (boxes, probs) = od.detect(roi, winStep=conf["step_size"], winDim=conf["sliding_size"], pyramidScale=1.1, minProb=pro) # since for size effect with the camera, pyramidScale = 1.001, mnust>1, # if positive size would change, we have to use 1.5 or 2 ...etc pick = non_max_suppression(np.array(boxes), probs, conf["overlap_thresh"]) orig = img.copy() # loop over the allowed bounding boxes and draw them for (startX, startY, endX, endY) in pick: if startY < 50 and startX < 50: pass else : cv2.rectangle(orig, (startX+35, startY+20), (endX+35, endY+20), (0, 255, 0), 2) print (startX+35, startY+20), (endX+35, endY+20) ### ## ### if falsePositive == True: hard_neg_extrect(conf,) show(orig) return img_gray, orig
def recog_show(self, img): hand_5 = cv2.CascadeClassifier(self.xmlPath) ref = img.copy() hand5 = hand_5.detectMultiScale( ref.copy()[50:550,50:800], scaleFactor=1.2, minNeighbors=15, #35 ==> 1 #flags = cv2.cv.CV_HAAR_SCALE_IMAGE ) hand = [] hand_roi=[] for (x,y,w,h) in hand5: # Position Aware + Size Aware if (x < 160 and y < 160) or y<160 or h<90: pass else: # draw rectangle at the specific location cv2.rectangle(ref,(x+50,y+50),(x+50+w,y+50+h),(255,0,0),2) cv2.putText(ref, "Hand", (x+50,y+50), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 0, 0), 4) hand.append([(x+50+x+50+w)/2,(y+50+h+y+50)/2]) #show(ref) print ((x+50,y+50),(x+50+w,y+50+h)) hand_roi.append([x+50,y+50,x+50+w,y+50+h]) if len(hand_roi)>1: result_box = max(hand_roi) elif len(hand_roi)==1: result_box = hand_roi.pop() else: result_box = 0 show(ref)
def test_with_pro_depth_size(rawImg,pro): ''' Use this fuc when ### above are first implement''' #img = vid.get_data(frame_id) roi = auto_resized(rawImg,conf['train_resolution']) (boxes, probs) = od.detect(roi, winStep=conf["step_size"], winDim=conf["sliding_size"], pyramidScale=1.1, minProb=pro) # since for size effect with the camera, pyramidScale = 1.001, mnust>1, # if positive size would change, we have to use 1.5 or 2 ...etc pick = non_max_suppression(np.array(boxes), probs, conf["overlap_thresh"]) orig = roi.copy() # loop over the allowed bounding boxes and draw them hand_roi=[] for (startX, startY, endX, endY) in pick: if endX-startX>30: pass else: cv2.rectangle(orig, (startX, startY), (endX, endY), (0, 255, 0), 2) print (startX, startY), (endX, endY) hand_roi.append([startX, startY, endX, endY]) show(orig) ############### if len(hand_roi)>1: result_box = max(hand_roi) elif len(hand_roi)==1: result_box = hand_roi.pop() else: result_box = 0 ##### return orig, result_box
def color_slicing(): # 彩色分层 image=io.imread('trafficlight.png') segred=image.copy() seggreen=image.copy() segyellow=image.copy() maskred=(image[:,:,0]>100) & (image[:,:,1]<50 ) & (image[:,:,2]<50) maskgreen=(image[:,:,0]<100) & (image[:,:,1]>100 ) & (image[:,:,2]<100) maskyellow=(image[:,:,0]>100) & (image[:,:,1]>100 ) & (image[:,:,2]<70) segred[:,:,0]*=maskred segred[:,:,1]*=maskred segred[:,:,2]*=maskred io.imshow(segred) io.imsave('lightred.png',segred) io.show() seggreen[:,:,0]*=maskgreen seggreen[:,:,1]*=maskgreen seggreen[:,:,2]*=maskgreen io.imshow(seggreen) io.imsave('lightgreen.png',seggreen) io.show() segyellow[:,:,0]*=maskyellow segyellow[:,:,1]*=maskyellow segyellow[:,:,2]*=maskyellow io.imshow(segyellow) io.imsave('lightyellow.png',segyellow)
def test_with_pro_depth_iter(rawImg,pro): ''' Use this fuc when ### above are first implement''' #img = vid.get_data(frame_id) roi = auto_resized(rawImg,conf['train_resolution']) def memo(proThresh): (boxes, probs) = od.detect(roi, winStep=conf["step_size"], winDim=conf["sliding_size"], pyramidScale=1.1, minProb=proThresh) # since for size effect with the camera, pyramidScale = 1.001, mnust>1, # if positive size would change, we have to use 1.5 or 2 ...etc pick = non_max_suppression(np.array(boxes), probs, conf["overlap_thresh"]) if pick>1: proThresh+=0.01 memo(proThresh) elif pick ==0: proThresh-=0.01 memo(proThresh) else: return pick pick = memo(0.5) orig = img.copy() # loop over the allowed bounding boxes and draw them for (startX, startY, endX, endY) in pick: cv2.rectangle(orig, (startX+35, startY+20), (endX+35, endY+20), (0, 255, 0), 2) print (startX+35, startY+20), (endX+35, endY+20) show(orig) return roi, orig
def align_img(image_name, loss_fn=sum_of_squared_diff, max_disp = 15, big=False, name=None, hist_eq = False): b,g,r = get_bgr(image_name) if hist_eq == True: b, g, r = exposure.equalize_hist(b), exposure.equalize_hist(g), exposure.equalize_hist(r) print("Aligning green and blue: ") ag = align(g, b, loss_fn=loss_fn, max_disp = max_disp, big=big, name=name) print("Aligning blue and red: ") ar = align(r, b, loss_fn=loss_fn, max_disp = max_disp, big=big, name=name) # create a color image im_out = np.dstack([ar, ag, b]) plt.show() # save the image iname = image_name.split('.') iname[-1] = 'jpg' image_name = '.'.join(iname) fname = 'out_' + image_name skio.imsave(fname, im_out) # display the image skio.imshow(im_out) plt.show() skio.show()
def main(): args = vars(parser.parse_args()) filename = os.path.join(os.getcwd(), args["image"][0]) image = skimage.img_as_uint(color.rgb2gray(io.imread(filename))) subsample = 1 if (not args["subsample"] == 1): subsample = args["subsample"][0] image = transform.downscale_local_mean(image, (subsample, subsample)) image = transform.pyramid_expand(image, subsample, 0, 0) image = exposure.rescale_intensity(image, out_range=(0,args["depth"][0])) if (args["visualize"]): io.imshow(image) io.show() source = generate_face(image, subsample, args["depth"][0], FLICKER_SPEED) if source: with open(args["output"][0], 'w') as file_: file_.write(source) else: print "Attempted to generate source code, failed."
def main(): from skimage import data, io, filters testfolder='/Users/davidgreenfield/Downloads/pics_boots/' testimage='B00A0GVP8A.jpg' image = io.imread(testfolder+testimage,flatten=True) # or any NumPy array! edges = filters.sobel(image) io.imshow(edges) io.show()
def _detect_spots_hough_circle(image, radius): edges = canny(image) imshow(edges) show() hough_radii = np.arange(radius/2, radius*2, 10) hough_circles = hough_circle(edges, hough_radii) print(hough_circles)
def main(sc, outputDir, outputIndices, filename, onThreshold, speciesToBin, skipTime, interactive): global globalSpeciesToBin, globalSkipTime, globalOnThreshold # Broadcast the global variables. globalSpeciesToBin=sc.broadcast(speciesToBin) globalSkipTime=sc.broadcast(skipTime) globalOnThreshold = sc.broadcast(onThreshold) # Load the records from the sfile. allRecords = sc.newAPIHadoopFile(filename, "robertslab.hadoop.io.SFileInputFormat", "robertslab.hadoop.io.SFileHeader", "robertslab.hadoop.io.SFileRecord", keyConverter="robertslab.spark.sfile.SFileHeaderToPythonConverter", valueConverter="robertslab.spark.sfile.SFileRecordToPythonConverter") # Bin the species counts records and sum across all of the bins. # results = allRecords.filter(filterSpeciesCounts).map(binSpeciesCounts).values().reduce(addBins) results = allRecords.filter(filterSpeciesCounts).map(storeEvent).values().reduce(addBins) results[0].sort() # print("results = " + str(results)) #.reduceByKey(addLatticeBins).values().collect() # Save the on events in a file in pickle format. pickle.dump(results, open("analysis/pdf.dat/onEvent_gradient_sp2.p", "wb")) # Save the pdfs into a .mat file in the output directory named according to the output indices. # pdfs=np.zeros((len(speciesToBin),),dtype=object) # for i in range(0,len(speciesToBin)): # minCount=results[i][0] # maxCount=results[i][1] # bins=results[i][2] # pdf = np.zeros((len(bins),2),dtype=float) # pdf[:,0]=np.arange(minCount,maxCount+1).astype(float) # pdf[:,1]=bins.astype(float)/float(sum(bins)) # pdfs[i] = pdf # print("Binned species %d: %d data points from %d to %d into %s"%(speciesToBin[i],sum(bins),minCount,maxCount,outputDir)) # cellio.cellsave(outputDir,pdfs,outputIndices); # cellio.cellsave(outputDir,results,outputIndices); # If interactive, show the pdf. if interactive: for repidx in range(len(results[0])): replicate = results[0][repidx][0] times=[] event=[] for val in results[0][repidx][1:]: times.append(val[0]) event.append(val[1]) print("times = " + str(times)) print("event = " + str(event)) plt.figure() plt.subplot(1,1,1) plt.plot(times,event) plt.axis([0, times[-1], -1, 2]) plt.xlabel('time') plt.ylabel('event') plt.title('replicate = %s'%(replicate)) io.show() else: print "Warning: cannot plot PDF with only a single bin."
def color_transformation(): # 彩色变换 image=data.coffee() brighter=np.uint8(image*0.5+255*0.5) darker=np.uint8(image*0.5) io.imshow(brighter) io.show() io.imshow(darker) io.show()
def main(): os.chdir("daneA/set0") for file in glob.glob("*.png"): img = normalize_one_picture(file) io.imshow(img) print(file) io.show() return 0
def main(argv): filename = argv[1] img = io.imread(filename, as_grey=True) lpyra = tuple(transform.pyramid_laplacian(img)) l = lpyra[0] l = exposure.equalize_hist(l) y, x = np.indices((l.shape[0],l.shape[1])) vect = np.array(zip(y.reshape(y.size),x.reshape(x.size),l.reshape(l.size))) io.imshow(l) io.show()
def test1(): data, key = load_h5(ROOT + os.sep + 'train320.h5') X = data['X'] img = np.squeeze(X[700]) score = data['y'][700] img = np.swapaxes(img, 0, 2) img = np.swapaxes(img, 0, 1) skio.imshow(img) print score skio.show()
def main(): import skimage.io as io import sys if len(sys.argv) != 2: print "Usage: skivi <image-file>" sys.exit(-1) io.use_plugin('qt') io.imshow(io.imread(sys.argv[1]), fancy=True) io.show()
def template_match_v2(refImg, newImg, thresHold): res = cv2.matchTemplate(newImg, refImg, cv2.TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) print min_val, max_val, min_loc, max_loc top_left = max_loc h , w, _ = refImg.shape bottom_right = (top_left[0]+w,top_left[1]+h) print (top_left, bottom_right) cv2.rectangle(newImg, top_left, bottom_right, 255, 2) show(newImg) return newImg[top_left[1]:top_left[1]+h, top_left[0]:top_left[0]+w], (top_left, bottom_right)
def ApplyThresholdToImageRegion(image2, Tb, Bb, Lb, Rb,shouldThresholdImage): image = rgb2gray(image2) global foregroundPixelValue global backgroundPixelValue thresholdValue = threshold_otsu(image) NumberOfRows = image.shape[0] NumberOfColumns = image.shape[1] numberOfBlackPixels = 0 numberOfWhitePixels = 0 selem = disk(3) # simpe thresholding for y in range(NumberOfRows): for x in range(NumberOfColumns): isWithinBoundary = IsWithinBoundary(y,x,image2, Tb, Bb, Lb, Rb,shouldThresholdImage) if (isWithinBoundary): if image[y,x] > thresholdValue: #black image[y,x] = 0 numberOfBlackPixels += 1 else: #white image[y,x] = 1 numberOfWhitePixels += 1 # assume foreground has more pixels in face region if (numberOfWhitePixels > numberOfBlackPixels): foregroundPixelValue = 1 backgroundPixelValue = 0 #print("foreground color is white") else: foregroundPixelValue = 0 backgroundPixelValue = 1 #print("foreground color is black") image = opening(image,selem) if (foregroundPixelValue == 0): image = opening(image,selem) else: image = closing(image,selem) if drawFaceImages: io.imshow(image) io.show() return image
def show(self,circle=False,data=None): """ Show the data in a debugging window. """ if data is None: data = self.data if circle: io.imshow(self.data_with_circle()) else: io.imshow(data) io.show()
def debug(): marked = np.zeros(image.shape, dtype=np.uint8) for rectangle in rectangles: rr, cc = rectangle.pixels(marked.shape) randcolor = randint(0, 255), randint(0, 255), randint(0, 255) marked[rr, cc] = randcolor print(image.shape, segments.shape, marked.shape) io.imshow_collection([image, segments, marked]) io.show()
def predict(image_path): net.blobs['data'].reshape(1, 3, SZ, SZ) img = skio.imread(image_path) net.blobs['data'].data[...] = transformer.preprocess('data', img/255.0) out = net.forward() print np.squeeze(out.values()) img = img_mani.filter_img(img) skio.imshow(img) skio.show() net.blobs['data'].data[...] = transformer.preprocess('data', img) out = net.forward() print np.squeeze(out.values()) return np.squeeze(out.values())
def assign_label(img, segnum): global labelMap del labelMap labelMap = {} for seg in xrange(0, segnum): imgcopy = img.copy() for pixnum in xrange(0, len(pixelMap[seg])): i, j = pixelMap[seg][pixnum] imgcopy[i, j, :] = [0, 0, 0] io.imshow(imgcopy) io.show() lab = int(raw_input()) labelMap[s] = lab del imgcopy
def get_path_info_preview(self, path): path_lower = path.lower() #name name = os.path.basename(os.path.normpath(path)) name_final = ("name: " + name) path_sl = path + "/" if ".jpg" in path_lower: preview = ("Used path leads to current image.") img = io.imread(path) io.imshow(img) io.show() elif ".png" in path_lower: preview = ("Used path leads to current image.") img = io.imread(path) io.imshow(img) io.show() elif ".dcm" in path_lower: preview = ("Used path leads to current image.") ds = pdicom.dcmread(path) plt.imshow(ds.pixel_array, cmap=plt.cm.bone) else: preview = ("Preview of files in dir: " + name) only_files = [f for f in listdir(path) if isfile(join(path, f))] for x in only_files: if (".dcm" or ".Dcm" or ".DCM") in x: ending = os.path.basename(os.path.normpath(path_sl + x)) preview_path = path_sl + ending ds = pdicom.dcmread(preview_path) plt.imshow(ds.pixel_array, cmap=plt.cm.bone) break elif (".jpg" or ".Jpg" or ".JPG") in x: ending = os.path.basename(os.path.normpath(path_sl + x)) preview_path = path_sl + ending img = io.imread(preview_path) io.imshow(img) io.show() break elif (".png" or ".Png" or ".PNG") in x: ending = os.path.basename(os.path.normpath(path_sl + x)) preview_path = path_sl + ending img = io.imread(preview_path) io.imshow(img) io.show() break else: None break
def show(image, bw): # apply threshold # thresh = threshold_otsu(image) # remove artifacts connected to image border cleared = bw.copy() clear_border(cleared) # label image regions label_image = label(cleared) borders = np.logical_xor(bw, cleared) label_image[borders] = -1 image_label_overlay = label2rgb(label_image, image=image) #fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6)) io.imshow(image_label_overlay) io.show() return image_label_overlay
def main(): parser = argparse.ArgumentParser(description='Image compression with SVD') parser.add_argument('-c', dest='compress', nargs='?', help='compress image using SVD') parser.add_argument('-k', dest='k', nargs='*', default=['5', '5', '5'], help='compression factor k (default 5)') parser.add_argument('-s', dest='ssim', nargs=2, help='calculate ssim between 2 images') parser.add_argument('-r', dest='size', type=int, default=100, help='image resize percentage (default 100)') parser.add_argument('-f', dest='fname', nargs='?', help='saved compressed image to file') args = parser.parse_args() args.k = [int(x) for x in args.k] if args.ssim: img1 = img_as_float(io.imread(args.ssim[0])) img2 = img_as_float(io.imread(args.ssim[1])) ss = ssim(img1, img2) print "Strucural similarity: %r" % ss elif args.compress: img = io.imread(args.compress) print "Original image dimensions {0}".format(img.shape) if args.size < 100: img = misc.imresize(img, args.size) img = img_as_float(img) # check if this is an RGB or grayscale image if len(img.shape) > 2: if len(args.k) != img.shape[2]: print "Provide correct number of k values (%r)" % img.shape[2] return compressed = svd_compress_rgb(img, args.k[0], args.k[1], args.k[2]) else: compressed = svd_compress_gs(img, args.k[0]) print "Compression ratio: %r" % compress_ratio(img, args.k) if args.fname: io.imsave(args.fname, compressed) io.imshow(compressed) io.show() elif args.size < 100: print "Resizing image to {0}%".format(args.r) img = misc.imresize(img, args.size) plt.figure(figsize=(10, 3.6)) plt.imshow(img) plt.show() else: parser.print_help()
def skrepresent(path): """Show a square image that represents the file specified starting from an np.zeros array""" data = None with open(path, 'rb') as infile: data = bytearray(mmap.mmap(infile.fileno(), 0, access=mmap.ACCESS_READ)) side = math.ceil(math.sqrt(len(data))) #img = Image.new('RGB', (side, side), "black") pixels = np.zeros(shape=(side, side, 3), dtype=np.uint8) for i in range(side): for j in range(side): index = i*side+j if index < len(data): pixels[i, j, 0] = data[index] pixels[i, j, 1] = data[index] pixels[i, j, 2] = 0 io.imshow(pixels) io.show()
def read_img(img_path, target_shape =(224,224,3)): mean=( 103.939,116.779,123.68) img = io.imread(img_path) # we only deal with color image for now assert img.shape[2]==3 # resize so that the shorter side=256 w, h, _ = img.shape resize_ratio = 256./min(w, h) if w<h: h = int(h * resize_ratio) resized_shape = (256, h) else: w = int(w * resize_ratio) resized_shape = (w, 256) # resize accroding to shorter side img = resize(img, resized_shape) img = img*255 # crop the center 224*224 part img = img[16:240,16:240,:] io.imshow(img.astype('uint8')) io.show() #decenterize img[:,:,0] -= mean[0] img[:,:,1] -= mean[1] img[:,:,2] -= mean[2] # 'RGB'->'BGR' img = img[:,:,::-1] #'tf'->'th' img = np.transpose(img,(2,0,1)) # expand dim for test img = np.expand_dims(img,axis=0) return img
def main(): img = misc.imread("wheat.png") # labels1 = segmentation.slic(img, compactness=100, n_segments=9) labels1 = segmentation.slic(img, compactness=50, n_segments=4) out1 = color.label2rgb(labels1, img, kind='overlay') print(labels1.shape) g = graph.rag_mean_color(img, labels1) labels2 = graph.cut_threshold(labels1, g, 29) out2 = color.label2rgb(labels2, img, kind='overlay') # get roi # logicalIndex = (labels2 != 1) # gray = rgb2gray(img); # gray[logicalIndex] = 0; plt.figure() io.imshow(out1) plt.figure() io.imshow(out2) io.show()
def main(sc, outputDir, outputIndices, filename, speciesToBin, skipTime, sparse, interactive): global globalSpeciesToBin, globalSkipTime, globalSparse # Broadcast the global variables. globalSpeciesToBin=sc.broadcast(speciesToBin) globalSkipTime=sc.broadcast(skipTime) globalSparse=sc.broadcast(sparse) # Load the records from the sfile. allRecords = sc.newAPIHadoopFile(filename, "robertslab.hadoop.io.SFileInputFormat", "robertslab.hadoop.io.SFileHeader", "robertslab.hadoop.io.SFileRecord", keyConverter="robertslab.spark.sfile.SFileHeaderToPythonConverter", valueConverter="robertslab.spark.sfile.SFileRecordToPythonConverter") # Bin the species counts records and sum across all of the bins. results = allRecords.filter(filterLatticeTimeSeries).map(binLatticeOccupancy).reduceByKey(addLatticeBins).values().collect() totalTimePoints=results[0][0] bins=results[0][1] bins[:,:,:,:,0] += totalTimePoints print "Recovered bins from %d total time points"%(totalTimePoints) print bins.shape # Save the pdfs into a .mat file in the output directory named according to the output indices. pdfs=np.zeros((len(speciesToBin),),dtype=object) for i in range(0,len(speciesToBin)): pdf=bins[i,:,:,:,:].astype(float)/float(np.sum(bins[i,0,0,0,:])) pdfs[i] = pdf cellio.cellsave(outputDir,pdfs,outputIndices); print("Binned species data into %s"%(outputDir)) # If interactive, show the pdf. if interactive: subvolumeCounts=bins.sum(axis=1).sum(axis=1).sum(axis=1) for i in range(0,len(speciesToBin)): print "Subvolume distribution for species %d"%(speciesToBin[i]) plt.figure() plt.subplot(1,1,1) plt.bar(np.arange(0,subvolumeCounts.shape[1]),np.log10(subvolumeCounts[i,:])) io.show()
# -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. """ from skimage import io, util import numpy as np import matplotlib.pyplot as plt ichan = io.imread('pic.png') ichan_grey = io.imread( 'pic.png', as_grey=True) #copy and rename #True is 0~1 read as grey ichan_grey = util.img_as_ubyte( ichan_grey) #set original png to grey #convert from 0_1 to 0_255 io.imshow(ichan) plt.figure() io.imshow(ichan_grey) io.show() #show with cmd print(ichan.shape)
def show(self): io.imshow(self.img) io.show()
def showIM(): imgs = read_video('C:\\Users\\zhang\\Pictures\\test\\1.avi') for im in imgs: skio.imshow(im) skio.show()
def viewImage(im): imshow(im) show()
def vap_impop_up(): """ show image to """ io.show()
process = False # read in the image def read_image(imname, flag=False): im = skio.imread(imname, flag) return im z = np.array([np.array([1, -1])]) print(z.ndim) camera = read_image("sample_data/cameraman.png", True) print(camera.ndim) skio.imshow(camera) skio.show() camera = read_image("sample_data/cameraman.png", True) # Convolve with dx [1, -1] def convolve_dx(im): dx = np.array([np.array([1, -1])]) convolved_x = scipy.signal.convolve2d(im, dx, mode='same') return convolved_x convolved_x = convolve_dx(camera) skio.imshow(convolved_x)
def build_pca(data: tuple) -> tuple: x_train, x_test, y_train, y_test, k, names = data x_train = img_as_float(x_train).astype(np.float32) x_test = img_as_float(x_test).astype(np.float32) t_1 = 0 print("Normalizing the data", end="") t_0 = perf_counter() # Normalize all images to reduce the error due to lighting conditions S = norm_img(x_train) t_1 += print_time(t_0) if VERBOSE: m = mean_img(S) # Mean image # Reshape flattened image with its dimension and show img = m.reshape(-1, FACE_DIM[0]) io.imshow(img) # Show mean image io.show() A = S.T # [N^2 x M] print("Calculating covariance", end="") t_0 = perf_counter() # Covariance matrix C=AA', L=A'A # C = np.dot(A, A.T) # [N^2 x N^2] L = np.dot(A.T, A) # [M x M] L /= A.shape[1] # divide by M t_1 += print_time(t_0) print("Finding eigenvalues and eigenvectors", end="") t_0 = perf_counter() # vv are the eigenvector for L; dd are the eigenvalue for both L and C dd, vv = np.linalg.eig(L) # [M], [M x M] t_1 += print_time(t_0) for ev in vv: np.testing.assert_array_almost_equal(1.0, np.linalg.norm(ev)) if VERBOSE: with plt.style.context('seaborn-whitegrid'): x_range = range(1, len(dd) + 1) tot = sum(dd) dd_desc = sorted(dd, reverse=True) var_exp = [100 * i / tot for i in dd_desc] cum_var_exp = np.cumsum(var_exp) plt.figure(figsize=(9, 6)) plt.bar(x_range, var_exp, alpha=0.5, align='center', label='individual explained variance') plt.step(x_range, cum_var_exp, where='mid', label='cumulative explained variance') plt.ylabel('Explained variance ratio') plt.xlabel('Principal components') plt.ylim(0, 100) plt.xlim(0, k * 1.5) plt.legend(loc='best') plt.tight_layout() plt.show() print("Selecting principal components", end="") t_0 = perf_counter() # Sort the eigen values in descending order and then sorted the eigen vectors by the same index idx = (-dd).argsort()[:k] # [k] # d = dd[idx] # [k] v = vv[:, idx] # [M x k] v = norm_eig(v) # Normalization of eigenvectors # Eigenvectors of C matrix u = np.dot(A, v).astype(np.float32) # [N^2 x k] u = norm_eig(u) # Normalization of eigenvectors # Find the weight of each face in the training set. omega = np.dot(u.T, A).astype(np.float32) # [k x M] t_1 += print_time(t_0) y_pred = [] print("Predicting people's names on the test set", end="") t_0 = perf_counter() for i in range(len(x_test)): test_i = np.array(x_test[i, :]).reshape(1, -1) idx = pred_pca(test_i, u, omega) prediction = y_train[idx] y_pred.append(prediction) y_pred = np.asarray(y_pred, dtype=np.float32) t = perf_counter() - t_0 print(f"\t-> took {1000 * t / len(y_pred):.4f}ms per sample on average") t_1 += t print(f"\n\t=> PCA was completed in {t_1:.3f}s\n") success_percent(y_pred, y_test, "PCA") return y_train, u, omega, names
def show(): #replaces cv2.waitKey() return io.show()
def show_img_skimage(image): io.imshow(image) io.show()
def KMeans(image, lab, clasters, kef1=0.5, kef2=0.1, metric=1): NormalMin1 = np.min(image[:, :, 1]) NormalMin2 = np.min(image[:, :, 2]) NormalMax1 = np.max(image[:, :, 1]) NormalMax2 = np.max(image[:, :, 2]) tempClaster = np.copy(clasters) * 0 sizeByX, sizeByY, temp = image.shape D1 = np.zeros((len(tempClaster))) D1 = D1.astype('float64') Error = np.array([0, 1]) NumClasters = np.zeros((len(tempClaster))) while (Error[0] != Error[1]): for i in range(0, sizeByX): for j in range(0, sizeByY): buffNormal1 = (image[i][j][1] - NormalMin1) / (NormalMax1 - NormalMin1) buffNormal2 = (image[i][j][2] - NormalMin2) / (NormalMax2 - NormalMin2) distanceAfin = np.array([]) for iterat in range(0, len(clasters)): if (metric == 1): buff = math.sqrt( pow(kef1 * (buffNormal1 - clasters[iterat][0]), 2) + pow(kef1 * (buffNormal2 - clasters[iterat][1]), 2) + pow(kef2 * ((i / sizeByX) - clasters[iterat][2]), 2) + pow(kef2 * ((j / sizeByY) - clasters[iterat][3]), 2)) elif (metric == 2): buff = max( abs(kef1 * (buffNormal1 - clasters[iterat][0])), abs(kef1 * (buffNormal2 - clasters[iterat][1])), abs(kef2 * ((i / sizeByX) - clasters[iterat][2])), abs(kef2 * ((j / sizeByY) - clasters[iterat][3]))) distanceAfin = np.append(distanceAfin, [buff]) for iterat in range(0, len(clasters)): if (distanceAfin[iterat] == np.min(distanceAfin)): tempClaster[iterat][0] += buffNormal1 tempClaster[iterat][1] += buffNormal2 tempClaster[iterat][2] += (i / sizeByX) tempClaster[iterat][3] += (j / sizeByY) NumClasters[iterat] += 1 tempD1 = pow(distanceAfin[iterat], 2) D1[iterat] = D1[iterat] + tempD1 lab2[i][j][0] = 10 * iterat lab2[i][j][1] = 10 * iterat lab2[i][j][2] = 10 * iterat for iterat in range(0, len(clasters)): if NumClasters[iterat] == 0: NumClasters[iterat] = 1 clasters[iterat][0] = tempClaster[iterat][0] / NumClasters[iterat] clasters[iterat][1] = tempClaster[iterat][1] / NumClasters[iterat] clasters[iterat][2] = tempClaster[iterat][2] / NumClasters[iterat] clasters[iterat][3] = tempClaster[iterat][3] / NumClasters[iterat] tempClaster = tempClaster * 0 rgb = color.lab2rgb(lab2) io.imshow(rgb) io.show() Error[0] = Error[1] Error[1] = math.sqrt(np.sum(D1)) # print(Error) NumClasters = NumClasters * 0 D1 = D1 * 0 return clasters
def show_img(ori_img): io.imshow(ori_img[150], cmap='gray') io.show()
def show_img_single(data): io.imshow(data[:, :], cmap='gray') io.show()
def show_img(data): for i in range(data.shape[0]): io.imshow(data[i, :, :], cmap='gray') io.show()
def bitmap2material(img, expand_nmap=False, verbose=False, isPlotting=False): if verbose: print('Start processing...', flush=True) if verbose: print('Making seamless texture...', flush=True) img = filters.make_seamless(img) gray_img = color.rgb2gray(img) if verbose: print('...done', flush=True) if(isPlotting): io.imshow(img) io.show() if verbose: print('Computing normal map...', flush=True) n_map = filters.sobel_rgb_decoded(gray_img) if verbose: print('...done', flush=True) if(isPlotting): io.imshow(n_map) io.show() if expand_nmap: # works rather slowly, so just optional if verbose: print('Applying expansion filter to normal map...', flush=True) n_map = filters.norm_expansion(n_map, verbose=True) if verbose: print('...done', flush=True) if(isPlotting): io.imshow(n_map) io.show() if verbose: print('Computing bump map...', flush=True) bump_map = filters.bump_from_normal(n_map, initial_value=gray_img, verbose=False)[0] if verbose: print('...done', flush=True) if(isPlotting): io.imshow(bump_map) io.show() if verbose: print('Making Ambient Occlusion map...', flush=True) ao_map = filters.ambient_occlusion(bump_map, n_map, verbose=True) if verbose: print('...done', flush=True) if(isPlotting): io.imshow(ao_map, cmap='gray') io.show() if verbose: print('Making specular map...', flush=True) specular_map = filters.make_specular_map(gray_img) if verbose: print('...done', flush=True) if(isPlotting): io.imshow(specular_map, cmap='gray') io.show() if verbose: print('...ready!', flush=True) return (img, n_map, bump_map, ao_map, specular_map)
def performDetect(imagePath="data/dog.jpg", thresh= 0.1, configPath = "./darknet/yolov3-va_ultra.cfg", weightPath = "./darknet/yolov3-va_ultra.weights", metaPath= "./darknet/data/football.data", showImage= True, makeImageOnly = False, initOnly= False): """ Convenience function to handle the detection and returns of objects. Displaying bounding boxes requires libraries scikit-image and numpy Parameters ---------------- imagePath: str Path to the image to evaluate. Raises ValueError if not found thresh: float (default= 0.25) The detection threshold configPath: str Path to the configuration file. Raises ValueError if not found weightPath: str Path to the weights file. Raises ValueError if not found metaPath: str Path to the data file. Raises ValueError if not found showImage: bool (default= True) Compute (and show) bounding boxes. Changes return. makeImageOnly: bool (default= False) If showImage is True, this won't actually *show* the image, but will create the array and return it. initOnly: bool (default= False) Only initialize globals. Don't actually run a prediction. Returns ---------------------- When showImage is False, list of tuples like ('obj_label', confidence, (bounding_box_x_px, bounding_box_y_px, bounding_box_width_px, bounding_box_height_px)) The X and Y coordinates are from the center of the bounding box. Subtract half the width or height to get the lower corner. Otherwise, a dict with { "detections": as above "image": a numpy array representing an image, compatible with scikit-image "caption": an image caption } """ # Import the global variables. This lets us instance Darknet once, then just call performDetect() again without instancing again global metaMain, netMain, altNames #pylint: disable=W0603 assert 0 < thresh < 1, "Threshold should be a float between zero and one (non-inclusive)" if not os.path.exists(configPath): raise ValueError("Invalid config path `"+os.path.abspath(configPath)+"`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `"+os.path.abspath(weightPath)+"`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `"+os.path.abspath(metaPath)+"`") if netMain is None: netMain = load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = load_meta(metaPath.encode("ascii")) if altNames is None: # In Python 3, the metafile default access craps out on Windows (but not Linux) # Read the names file and create a list to feed to detect try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass if initOnly: print("Initialized detector") return None if not os.path.exists(imagePath): raise ValueError("Invalid image path `"+os.path.abspath(imagePath)+"`") # Do the detection #detections = detect(netMain, metaMain, imagePath, thresh) # if is used cv2.imread(image) detections = detect(netMain, metaMain, imagePath.encode("ascii"), thresh) if showImage: try: from skimage import io, draw import numpy as np image = io.imread(imagePath) print("*** "+str(len(detections))+" Results, color coded by confidence ***") imcaption = [] for detection in detections: label = detection[0] confidence = detection[1] pstring = label+": "+str(np.rint(100 * confidence))+"%" imcaption.append(pstring) print(pstring) bounds = detection[2] shape = image.shape # x = shape[1] # xExtent = int(x * bounds[2] / 100) # y = shape[0] # yExtent = int(y * bounds[3] / 100) yExtent = int(bounds[3]) xEntent = int(bounds[2]) # Coordinates are around the center xCoord = int(bounds[0] - bounds[2]/2) yCoord = int(bounds[1] - bounds[3]/2) boundingBox = [ [xCoord, yCoord], [xCoord, yCoord + yExtent], [xCoord + xEntent, yCoord + yExtent], [xCoord + xEntent, yCoord] ] # Wiggle it around to make a 3px border rr, cc = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr2, cc2 = draw.polygon_perimeter([x[1] + 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr3, cc3 = draw.polygon_perimeter([x[1] - 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr4, cc4 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] + 1 for x in boundingBox], shape= shape) rr5, cc5 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] - 1 for x in boundingBox], shape= shape) boxColor = (int(255 * (1 - (confidence ** 2))), int(255 * (confidence ** 2)), 0) draw.set_color(image, (rr, cc), boxColor, alpha= 0.8) draw.set_color(image, (rr2, cc2), boxColor, alpha= 0.8) draw.set_color(image, (rr3, cc3), boxColor, alpha= 0.8) draw.set_color(image, (rr4, cc4), boxColor, alpha= 0.8) draw.set_color(image, (rr5, cc5), boxColor, alpha= 0.8) if not makeImageOnly: io.imshow(image) io.show() detections = { "detections": detections, "image": image, "caption": "\n<br/>".join(imcaption) } except Exception as e: print("Unable to show image: "+str(e)) return detections
def show_imgs_skimage(imgs): io.imshow_collection(imgs) io.show()
from skimage.future import graph from skimage import data, segmentation, color, filters, io gimg = color.rgb2gray(img) labels = segmentation.slic(img, compactness=30, n_segments=400) edges = filters.sobel(gimg) edges_rgb = color.gray2rgb(edges) g = graph.rag_boundary(labels, edges) lc = graph.show_rag(labels, g, edges_rgb, img_cmap=None, edge_cmap='viridis', edge_width=1.2) plt.colorbar(lc, fraction=0.03) io.show() # Sobel + Watershed ############################################################ from skimage.filters import sobel from skimage.measure import label from skimage.segmentation import slic, join_segmentations from skimage.morphology import watershed from skimage.color import label2rgb from skimage import data coins = rgb2gray(img) # Make segmentation using edge-detection and watershed. edges = sobel(coins)
def detect(self, imagePath="data/dog.jpg", thresh= 0.25, showImage= True, makeImageOnly = False): if not os.path.exists(imagePath): raise ValueError("Invalid image path `"+os.path.abspath(imagePath)+"`") assert 0 < thresh < 1, "Threshold should be a float between zero and one (non-inclusive)" im = load_image(imagePath.encode("ascii"), 0, 0) detections = self._detect_image(im, thresh, .5, .45) free_image(im) if showImage: try: from skimage import io, draw import numpy as np image = io.imread(imagePath) print("*** "+str(len(detections))+" Results, color coded by confidence ***") imcaption = [] for detection in detections: label = detection[0] confidence = detection[1] #pstring = label+": "+str(np.rint(100 * confidence))+"%" #imcaption.append(pstring) #print(pstring) bounds = detection[2] shape = image.shape # x = shape[1] # xExtent = int(x * bounds[2] / 100) # y = shape[0] # yExtent = int(y * bounds[3] / 100) yExtent = int(bounds[3]) xEntent = int(bounds[2]) # Coordinates are around the center xCoord = int(bounds[0] - bounds[2]/2) yCoord = int(bounds[1] - bounds[3]/2) boundingBox = [ [xCoord, yCoord], [xCoord, yCoord + yExtent], [xCoord + xEntent, yCoord + yExtent], [xCoord + xEntent, yCoord] ] # Wiggle it around to make a 3px border rr, cc = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr2, cc2 = draw.polygon_perimeter([x[1] + 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr3, cc3 = draw.polygon_perimeter([x[1] - 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape) rr4, cc4 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] + 1 for x in boundingBox], shape= shape) rr5, cc5 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] - 1 for x in boundingBox], shape= shape) boxColor = (int(255 * (1 - (confidence ** 2))), int(255 * (confidence ** 2)), 0) draw.set_color(image, (rr, cc), boxColor, alpha= 0.8) draw.set_color(image, (rr2, cc2), boxColor, alpha= 0.8) draw.set_color(image, (rr3, cc3), boxColor, alpha= 0.8) draw.set_color(image, (rr4, cc4), boxColor, alpha= 0.8) draw.set_color(image, (rr5, cc5), boxColor, alpha= 0.8) if not makeImageOnly: io.imshow(image) io.show() detections = { "detections": detections, "image": image, "caption": "\n<br/>".join(imcaption) } except Exception as e: print("Unable to show image: "+str(e)) return detections
def rotate_and_show(im, deg): rotated = scipy.ndimage.interpolation.rotate(im, deg) skio.imshow(rotated) skio.show() return rotated
def show(self): io.imshow(self.data) io.show()
def get_landmarks(self, input_image, all_faces=False): if isinstance(input_image, str): try: image = io.imread(input_image) except IOError: print("error opening file :: ", input_image) return None else: image = input_image detected_faces = self.detect_faces(image) if len(detected_faces) > 0: landmarks = [] for i, d in enumerate(detected_faces): if i > 1 and not all_faces: break if self.enable_cuda or self.use_cnn_face_detector: d = d.rect cv2.rectangle(input_image, (d.left(), d.top()), (d.right(), d.bottom()), (0, 255, 0), 3) #cv2.imshow('name',input_image) #cv2.waitKey(0) print("rect", d.left(), d.top(), d.right(), d.bottom()) # input('.....') center = torch.FloatTensor([ d.right() - (d.right() - d.left()) / 2.0, d.bottom() - (d.bottom() - d.top()) / 2.0 ]) print("center before", center) center[1] = center[1] - (d.bottom() - d.top()) * 0.12 scale = (d.right() - d.left() + d.bottom() - d.top()) / 195.0 inp = crop(image, center, scale) io.imshow(inp) io.show() inp = torch.from_numpy( inp.transpose( # (2, 0, 1))).float().unsqueeze_(0) (2, 0, 1))).float().div(255.0).unsqueeze_(0) if self.enable_cuda: inp = inp.cuda() # Experimental print("input size", inp.size(), type(inp)) out = self.face_alignemnt_net(inp)[-1].data.cpu() print("output size", out.size()) if self.flip_input: out += flip(self.face_alignemnt_net( Variable(flip(inp), volatile=True))[-1].data.cpu(), is_label=True) pts, pts_img = get_preds_fromhm(out, center, scale) print("pts_img before view ops", pts_img.size()) pts, pts_img = pts.view(68, 2) * 4, pts_img.view(68, 2) print("pts_img after view ops", pts_img.size()) if self.landmarks_type == LandmarksType._3D: heatmaps = np.zeros((68, 256, 256)) for i in range(68): if pts[i, 0] > 0: heatmaps[i] = draw_gaussian(heatmaps[i], pts[i], 2) heatmaps = torch.from_numpy(heatmaps).view( 1, 68, 256, 256).float() if self.enable_cuda: heatmaps = heatmaps.cuda() depth_pred = self.depth_prediciton_net( Variable(torch.cat((inp, heatmaps), 1), volatile=True)).data.cpu().view(68, 1) pts_img = torch.cat((pts_img, depth_pred * (1.0 / (256.0 / (200.0 * scale)))), 1) print("pts_img after DEPTH ops", pts_img.size()) landmarks.append(pts_img.numpy()) else: print("Warning: No faces were detected.") return None return landmarks
def show_img(im): io.imshow(im) io.show()
def show_image(image): # print('show_image') io.imshow(image) io.show()
_raspistill_yuv(shutter_speed=shutter_speed, light_color=light_color, resolution=resolution) file = open(_capture_tmp, 'rb') img = Image.frombytes('RGB', resolution, file.read()) img = np.array(img) return img def capture(shutter_speed=100000, light_color='blue', resolution=_resolution): _raspistill(shutter_speed=shutter_speed, light_color=light_color, resolution=resolution) img = imread(_capture_tmp) return img # run as standalone script if __name__ == '__main__': print('testing ' + __file__) print('capturing...') image = capture(shutter_speed=100000) print('display image...') imshow(image) show()
def show(self): """Display the image.""" io.imshow(self._rescale(self.array)) io.show()
#path = '../data/test_sobel.jpg' try: img = io.imread(path) except FileNotFoundError: print('File don\'t exist!') print('Start processing...', flush=True) img = img_as_float(img) print('Making seamless texture...', flush=True) img = make_seamless(img, verbose=True) io.imsave('out/seamless.png', img) gray_img = color.rgb2gray(img) print('...done', flush=True) io.imshow(img) and io.show() print('Computing normal map...', flush=True) n_map = sobel_rgb_decoded(gray_img) print('...done', flush=True) io.imshow(n_map) and io.show() #''' # works rather slowly, so just optional print('Applying expansion filter to normal map...', flush=True) n_map = norm_expansion(n_map, verbose=True) print('...done', flush=True) io.imshow(n_map) and io.show() #''' io.imsave('out/n_map.png', n_map)
def main(): # Initialize generator and discriminator models generator = Generator() discriminator = Discriminator() # Ensure the checkpoint directory exists sys.enforce_dir(ARGS.checkpoint_dir) # Set up tf checkpoint manager checkpoint = tf.train.Checkpoint( generator=generator, discriminator=discriminator) manager = tf.train.CheckpointManager( checkpoint, ARGS.checkpoint_dir, max_to_keep=3) if ARGS.command != 'train' or ARGS.restore_checkpoint: # Restores the latest checkpoint using from the manager checkpoint.restore(manager.latest_checkpoint).expect_partial() # save_model_weights(generator, discriminator) # generator.load_weights("generator.h5") # discriminator.load_weights("discriminator.h5") print("Restored checkpoint") try: with tf.device("/device:" + ARGS.device): if ARGS.command == "train": # train here! dataset = Datasets( ARGS.untouched_dir, ARGS.edited_dir, "train", ARGS.editor) train(dataset, manager, generator, discriminator) if ARGS.command == 'test': # test here! dataset = Datasets( ARGS.untouched_dir, ARGS.edited_dir, "test", ARGS.editor) test(dataset, generator) if ARGS.command == 'evaluate': # Ensure the output directory exists sys.enforce_dir(ARGS.output_dir) img_name = ARGS.image_path.split("/")[-1].split(".")[0] # evaluate here! img = io.imread(ARGS.image_path) # if img.shape[-1] == 4: # rgba_img = Image.fromarray(img) # img = np.array(rgba_img.convert('RGB')) edited_img = sys.edit_original(img, generator) io.imshow(edited_img) io.show() io.imsave(ARGS.output_dir + "/" + img_name + "-edited.png", edited_img) pass if ARGS.command == 'performance': # get performance metrics here! dataset = Datasets( ARGS.untouched_dir, ARGS.edited_dir, "test", ARGS.editor) performance(dataset, generator) except RuntimeError as e: # something went wrong should not get here print(e)
def train(): letters = ['a', 'd', 'm', 'n', 'o', 'p', 'q', 'r', 'u', 'w'] Features = [] sum = 0 sdevs = [] means = [] YPred = [] for i in range(0,len(letters)): imgloc = "training/" + letters[i] + ".bmp" img = io.imread(imgloc) #img = medfilt(img) #print img.shape io.imshow(img) plt.title('Original Image') io.show() hist = exposure.histogram(img) plt.bar(hist[1], hist[0]) plt.title('Histogram') plt.show() th = 210 img_binary = (img < th).astype(np.double) if (i == 4): img_binary = ndimage.morphology.binary_dilation(img_binary) img_binary = medfilt(img_binary) io.imshow(img_binary) plt.title('Binary Image') io.show() #print(type(img_binary)) img_label = label(img_binary, background=0) io.imshow(img_label) plt.title('Labeled Image') io.show() #print np.amax(img_label) regions = regionprops(img_label) io.imshow(img_binary) ax = plt.gca() plt.title('Bounding boxes') for props in regions: minr, minc, maxr, maxc = props.bbox #print str(maxr - minr) + "," + str(maxc - minc) if (maxr - minr) < 8 or (maxc - minc) < 8 or (maxr - minr + maxc - minc) < 25 or (maxr - minr + maxc - minc) > 150: continue sum += 1 ax.add_patch(Rectangle((minc, minr), maxc - minc, maxr - minr, fill=False, edgecolor='red', linewidth=1)) roi = img_binary[minr:maxr, minc:maxc] m = moments(roi) cr = m[0, 1] / m[0, 0] cc = m[1, 0] / m[0, 0] mu = moments_central(roi, cr, cc) nu = moments_normalized(mu) hu = moments_hu(nu) Features.append(hu) YTrue.append(letters[i]) #print(sum) io.show() #print Features[i] #print "*****" sdev = np.std(Features[i]) mean = np.mean(Features[i]) sdevs.append(sdev) means.append(mean) D = cdist(Features, Features) io.imshow(D) plt.title('Distance Matrix') io.show() #print(len(D)) #print(D) D_index = np.argsort(D, axis=1) #print(D_index) for i in range(0, len(Features)): YPred.append(YTrue[D_index[i][1]]) confM = confusion_matrix(YTrue, YPred) io.imshow(confM) plt.title('Confusion Matrix') io.show() #print(type(Features)) sdev = np.std(sdevs) mean = np.mean(means) #print sdev #print mean #print(Features) #print (sum / 10) for i in range(0, len(letters)): Features[i] -= mean Features[i] /= sdev #print(len(Features)) #print(YTrue) #print(YPred) return Features
def plot_seam(img, seam): for i in range(0, len(seam)): img[i][seam[i]] = 1 io.imshow(img) io.show()
def performDetect(imagePath='/home/kishore/masked.jpg', thresh=0.25, configPath="./v3/mask.cfg", weightPath="v3/mask_18000.weights", metaPath="./v3/mask.data", showImage=False, makeImageOnly=True, initOnly=False): global metaMain, netMain, altNames #pylint: disable=W0603 assert 0 < thresh < 1, "Threshold should be a float between zero and one (non-inclusive)" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass if initOnly: print("Initialized detector") return None if not os.path.exists(imagePath): raise ValueError("Invalid image path `" + os.path.abspath(imagePath) + "`") detections = detect(netMain, metaMain, imagePath.encode("ascii"), thresh) if args.debug: print('*-*' * 20) print(detections) print('*-*' * 20) try: from skimage import io, draw import numpy as np image = io.imread(imagePath) detections = { "detections": detections, "image": image, "caption": "\n<br/>".join('mask') } print( "*** " + str(len(detections)) + " Results, color coded by confidence ***") if args.debug else None imcaption = [] for detection in detections: label = detection[0] confidence = detection[1] pstring = label + ": " + str(np.rint(100 * confidence)) + "%" imcaption.append(pstring) print(pstring) bounds = detection[2] shape = image.shape yExtent = int(bounds[3]) xEntent = int(bounds[2]) xCoord = int(bounds[0] - bounds[2] / 2) yCoord = int(bounds[1] - bounds[3] / 2) boundingBox = [[xCoord, yCoord], [xCoord, yCoord + yExtent], [xCoord + xEntent, yCoord + yExtent], [xCoord + xEntent, yCoord]] # Wiggle it around to make a 3px border if showImage: rr, cc = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] for x in boundingBox], shape=shape) rr2, cc2 = draw.polygon_perimeter([x[1] + 1 for x in boundingBox], [x[0] for x in boundingBox], shape=shape) rr3, cc3 = draw.polygon_perimeter([x[1] - 1 for x in boundingBox], [x[0] for x in boundingBox], shape=shape) rr4, cc4 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] + 1 for x in boundingBox], shape=shape) rr5, cc5 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] - 1 for x in boundingBox], shape=shape) boxColor = (int(255 * (1 - (confidence**2))), int(255 * (confidence**2)), 0) draw.set_color(image, (rr, cc), boxColor, alpha=0.8) draw.set_color(image, (rr2, cc2), boxColor, alpha=0.8) draw.set_color(image, (rr3, cc3), boxColor, alpha=0.8) draw.set_color(image, (rr4, cc4), boxColor, alpha=0.8) draw.set_color(image, (rr5, cc5), boxColor, alpha=0.8) if makeImageOnly: io.imshow(image) io.show() except Exception as e: print("Unable to show image: " + str(e)) return detections