def main(): import sys try: fn = sys.argv[1] except: fn = 'fruits.jpg' img = cv.imread(cv.samples.findFile(fn)) if img is None: print('Failed to load image file:', fn) sys.exit(1) img_mark = img.copy() mark = np.zeros(img.shape[:2], np.uint8) sketch = Sketcher('img', [img_mark, mark], lambda : ((255, 255, 255), 255)) while True: ch = cv.waitKey() if ch == 27: break if ch == ord(' '): res = cv.inpaint(img_mark, mark, 3, cv.INPAINT_TELEA) cv.imshow('inpaint', res) if ch == ord('r'): img_mark[:] = img mark[:] = 0 sketch.show() print('Done')
def main(): import sys try: fn = sys.argv[1] except: fn = 'example_wood.jpg' img = cv.imread(cv.samples.findFile(fn)) if img is None: print('Failed to load image file:', fn) sys.exit(1) img_original = img.copy() cv.imshow('original', img_original) img_mark = img.copy() mark = np.zeros(img.shape[:2], np.uint8) sketch = Sketcher('img', [img_mark, mark], lambda: ((255, 255, 255), 255)) while True: ch = cv.waitKey() if ch == 27: break if ch == ord(' '): res = cv.inpaint(img_mark, mark, 3, cv.INPAINT_TELEA) cv.imshow('inpaint', res) if ch == ord('r'): img_mark[:] = img mark[:] = 0 sketch.show() print('Done')
class App: def __init__(self, fn): self.img = cv.imread(fn) if self.img is None: raise Exception('Failed to load image file: %s' % fn) h, w = self.img.shape[:2] self.markers = np.zeros((h, w), np.int32) self.markers_vis = self.img.copy() self.cur_marker = 1 self.colors = np.int32(list(np.ndindex(2, 2, 2))) * 255 self.auto_update = True self.sketch = Sketcher('img', [self.markers_vis, self.markers], self.get_colors) def get_colors(self): return list(map(int, self.colors[self.cur_marker])), self.cur_marker def watershed(self): m = self.markers.copy() cv.watershed(self.img, m) cv.imshow('watershed1', self.img) overlay = self.colors[np.maximum(m, 0)] vis = cv.addWeighted(self.img, 0.5, overlay, 0.5, 0.0, dtype=cv.CV_8UC3) cv.imshow('watershed2', vis) def run(self): while cv.getWindowProperty('img', 0) != -1 or cv.getWindowProperty( 'watershed', 0) != -1: ch = cv.waitKey(50) if ch == 27: break if ch >= ord('1') and ch <= ord('7'): self.cur_marker = ch - ord('0') print('marker: ', self.cur_marker) if ch == ord(' ') or (self.sketch.dirty and self.auto_update): self.watershed() self.sketch.dirty = False if ch in [ord('a'), ord('A')]: self.auto_update = not self.auto_update print('auto_update if', ['off', 'on'][self.auto_update]) if ch in [ord('r'), ord('R')]: self.markers[:] = 0 self.markers_vis[:] = self.img self.sketch.show() cv.destroyAllWindows()
class DatasetCreator: ESC = 27 DEL = 127 RET = 13 KEY_UP = 0 KEY_DOWN = 1 EXT = ".bmp" FINAL_SIZE = 50 def __init__(self, prefix, dryRun): h, w = 300, 300 img = np.zeros((h, w, 3), np.uint8) img[:,:] = (255, 255, 255) self.brush = Brush("Brush") self.sketcher = Sketcher("Dataset Creator", img, self.brush) self.__folders = OCR.generateFolderList(OCR.DIGITS | OCR.LETTERS | OCR.SYMBOLS) self.__prefix = prefix self.__lastFile = None self.__dryRun = dryRun def run(self): while True: k = cv2.waitKey(0) & 0xFF if k == self.ESC: break elif k == self.RET: self.sketcher.reset() self.sketcher.show() elif k == self.KEY_UP: self.brush.brushSize += 1 elif k == self.KEY_DOWN: self.brush.brushSize -= 1 elif k == self.DEL and not self.__dryRun: if self.__lastFile: os.remove(self.__lastFile) self.__lastFile = None print path + " Removed" elif chr(k) in self.__folders: folder = self.__folders[chr(k)] path = common.generateFilename(folder, self.__prefix, self.EXT) item = self.sketcher.sketch.copy() item = cv2.resize(item, (self.FINAL_SIZE, self.FINAL_SIZE)) cv2.imshow("Generated Dataset item", item) self.sketcher.reset() self.__lastFile = path if not self.__dryRun: cv2.imwrite(path, item) print path + " Written" else: print path + " Preview"
class App: def __init__(self, fn): self.img = cv2.imread(fn) h, w = self.img.shape[:2] self.markers = np.zeros((h, w), np.int32) self.markers_vis = self.img.copy() self.cur_marker = 1 self.colors = np.int32(list(np.ndindex(2, 2, 2))) * 255 self.auto_update = True self.sketch = Sketcher('img', [self.markers_vis, self.markers], self.get_colors) def get_colors(self): return map(int, self.colors[self.cur_marker]), self.cur_marker def watershed(self): m = self.markers.copy() cv2.watershed(self.img, m) overlay = self.colors[np.maximum(m, 0)] vis = cv2.addWeighted(self.img, 0.5, overlay, 0.5, 0.0, dtype=cv2.CV_8UC3) cv2.imshow('watershed', vis) def run(self): while True: ch = 0xFF & cv2.waitKey(50) if ch == 27: break if ch >= ord('1') and ch <= ord('7'): self.cur_marker = ch - ord('0') print 'marker: ', self.cur_marker if ch == ord(' ') or (self.sketch.dirty and self.auto_update): self.watershed() self.sketch.dirty = False if ch in [ord('a'), ord('A')]: self.auto_update = not self.auto_update print 'auto_update if', ['off', 'on'][self.auto_update] if ch in [ord('r'), ord('R')]: self.markers[:] = 0 self.markers_vis[:] = self.img self.sketch.show() cv2.destroyAllWindows()
class App: def __init__(self, fn): self.img = cv2.imread(fn) if self.img is None: raise Exception('Failed to load image file: %s' % fn) h, w = self.img.shape[:2] self.markers = np.zeros((h, w), np.int32) self.markers_vis = self.img.copy() self.cur_marker = 1 self.colors = np.int32( list(np.ndindex(2, 2, 2)) ) * 255 self.auto_update = True self.sketch = Sketcher('img', [self.markers_vis, self.markers], self.get_colors) def get_colors(self): return list(map(int, self.colors[self.cur_marker])), self.cur_marker def watershed(self): m = self.markers.copy() cv2.watershed(self.img, m) overlay = self.colors[np.maximum(m, 0)] vis = cv2.addWeighted(self.img, 0.5, overlay, 0.5, 0.0, dtype=cv2.CV_8UC3) cv2.imshow('watershed', vis) def run(self): while True: ch = 0xFF & cv2.waitKey(50) if ch == 27: break if ch >= ord('1') and ch <= ord('7'): self.cur_marker = ch - ord('0') print('marker: ', self.cur_marker) if ch == ord(' ') or (self.sketch.dirty and self.auto_update): self.watershed() self.sketch.dirty = False if ch in [ord('a'), ord('A')]: self.auto_update = not self.auto_update print('auto_update if', ['off', 'on'][self.auto_update]) if ch in [ord('r'), ord('R')]: self.markers[:] = 0 self.markers_vis[:] = self.img self.sketch.show() cv2.destroyAllWindows()
class App: def __init__(self, fn): self.img = cv2.imread(fn) h, w = self.img.shape[:2] self.markers = np.zeros((h, w), np.int32) self.markers_vis = self.img.copy() self.cur_marker = 1 self.colors = np.int32(list(np.ndindex(2, 2, 2))) * 255 self.auto_update = True self.sketch = Sketcher("img", [self.markers_vis, self.markers], self.get_colors) def get_colors(self): return map(int, self.colors[self.cur_marker]), self.cur_marker def watershed(self): m = self.markers.copy() cv2.watershed(self.img, m) overlay = self.colors[np.maximum(m, 0)] vis = cv2.addWeighted(self.img, 0.5, overlay, 0.5, 0.0, dtype=cv2.CV_8UC3) cv2.imshow("watershed", vis) def run(self): while True: ch = 0xFF & cv2.waitKey(50) if ch == 27: break if ch >= ord("1") and ch <= ord("7"): self.cur_marker = ch - ord("0") print "marker: ", self.cur_marker if ch == ord(" ") or (self.sketch.dirty and self.auto_update): self.watershed() self.sketch.dirty = False if ch in [ord("a"), ord("A")]: self.auto_update = not self.auto_update print "auto_update if", ["off", "on"][self.auto_update] if ch in [ord("r"), ord("R")]: self.markers[:] = 0 self.markers_vis[:] = self.img self.sketch.show() cv2.destroyAllWindows()
def main(): canvas = numpy.ones((h + tah, w)) cv2.line(canvas, (0, tah - 1), (w, tah - 1), (0, 0, 0), 1) cv2.line(canvas, (145, 0), (145, tah - 1), (.5, 255, 255), 1) font = cv2.FONT_HERSHEY_PLAIN font2 = cv2.FONT_HERSHEY_TRIPLEX text1 = "Press 'C' to clear" cv2.putText(canvas, text1, (5, 10), font, 0.9, (0.5, 255, 0), 1) text2 = "Press 'T' to test" cv2.putText(canvas, text2, (5, 22), font, 0.9, (0.5, 255, 0), 1) img = canvas.copy() sketch = Sketcher('Draw', [img], lambda: ((0, 255, 0), 255)) while (True): if (cv2.waitKey() == ord('c')): print("Clear") img[:] = canvas sketch.show() if (cv2.waitKey() == ord('t')): process(img) output = str(random.randint(0, 10)) cv2.putText(img, output, (162, 22), font2, 1, (0, 255, 0), 1) sketch.show() print("Test") if (cv2.getWindowProperty('Draw', 0) == -1) or (cv2.waitKey() == 27): break cv2.destroyAllWindows()
class App: def __init__(self, fn): #self.img = cv.imread(fn) self.img = fn if self.img is None: raise Exception('Failed to load image file: %s' % fn) h, w = self.img.shape[:2] self.markers = np.zeros((h, w), np.int32) self.markers_vis = self.img.copy() self.cur_marker = 1 self.colors = np.int32(list(np.ndindex(2, 2, 2))) * 255 self.thresh_value = 50 self.thresh_max = None self.auto_update = True self.preprocess(0) self.sketch = Sketcher('img', [self.markers_vis, self.markers], self.get_colors) self.boundary_points = [] def change_thresh(self): self.thresh_value += 10 if self.thresh_value > self.thresh_max: self.thresh_value = 10 # 经过一系列图像处理得到一些前景和背景信息 def preprocess(self, flag=0): gray = cv.cvtColor(self.img, cv.COLOR_BGR2GRAY) # cv.imshow("gray", gray) # ret:返回的阈值, thresh:返回的二值化图像 if flag == 0: ret, thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU) self.thresh_max = ret else: ret, thresh = cv.threshold(gray, self.thresh_value, 255, cv.THRESH_BINARY) # print("ret:", ret) # cv.imshow("thresh", thresh) # noise removal kernel = np.ones((3, 3), np.uint8) opening = cv.morphologyEx(thresh, cv.MORPH_OPEN, kernel, iterations=2) # cv.imshow("opening", opening) # sure background area sure_bg = cv.dilate(opening, kernel, iterations=30) # cv.imshow("sure_bg", sure_bg) # Finding sure foreground area # distanceTransform用于计算每个非零像素距离与其最近的零点像素之间的距离, # 输出的是保存每一个非零点与最近零点的距离信息 dist_transform = cv.distanceTransform(opening, cv.DIST_L2, 5) normalized_img = np.zeros(dist_transform.shape) normalized_img = cv.normalize(dist_transform, normalized_img, 0, 255, cv.NORM_MINMAX) normalized_img = np.uint8(normalized_img) # cv.imshow("normalize_img", normalized_img) # self.write_matrix_to_file(normalized_img) ret, sure_fg = cv.threshold(dist_transform, 0.7 * dist_transform.max(), 255, 0) sure_fg = np.uint8(sure_fg) # cv.imshow("sure_fg", sure_fg) # Finding unknown region # unknown = cv.subtract(sure_bg, sure_fg) # Marker labelling # ret:一共有多少个区域,前景数+1(背景) # markers:为区域编号的矩阵,背景为0,前景依次为1,2,3 ret, markers = cv.connectedComponents(sure_fg) # 前景标记为1,背景标记为2 self.markers[markers[:, :] == 1] = 1 self.markers[sure_bg[:, :] == 0] = 2 # self.write_matrix_to_file(self.markers) overlay = self.colors[np.maximum(self.markers, 0)] alpha = np.zeros(overlay.shape) for a in range(3): # 这里im2[:,:,a]!=0生成一个bool型矩阵掩码 alpha[overlay[:, :, a] != 0] = 0.5 # self.markers_vis = cv.addWeighted(self.markers_vis, 0.5, overlay, 0.5, 0.0, dtype=cv.CV_8UC3) self.markers_vis[:] = overlay * alpha + self.markers_vis * (1 - alpha) self.markers_vis[:] = np.uint8(self.markers_vis) def reset(self): self.markers[:] = 0 self.markers_vis[:] = self.img def get_colors(self): return list(map(int, self.colors[self.cur_marker])), self.cur_marker def watershed(self): m = self.markers.copy() # m.shape = (h, w) self.write_matrix_to_file(m) cv.watershed(self.img, m) overlay = self.colors[np.maximum(m, 0)] self.vis = cv.addWeighted(self.img, 0.5, overlay, 0.5, 0.0, dtype=cv.CV_8UC3) # cv.imshow('watershed', self.vis) ol = overlay.astype(np.uint8) mask_bool = ol[:, :, 2] == 255 nonb = mask_bool.nonzero() all_points_y = nonb[0].tolist() all_points_x = nonb[1].tolist() self.boundary_points.clear() self.boundary_points.append(all_points_x) self.boundary_points.append(all_points_y) def start(self): self.watershed() return self.vis, self.boundary_points def run(self): self.watershed() while cv.getWindowProperty('img', 0) != -1 or cv.getWindowProperty( 'watershed', 0) != -1: ch = cv.waitKey(50) if ch == 27: break if ch >= ord('1') and ch <= ord('7'): self.cur_marker = ch - ord('0') print('marker: ', self.cur_marker) if self.sketch.dirty and self.auto_update: self.watershed() self.sketch.dirty = False if ch in [ord('a'), ord('A')]: self.auto_update = not self.auto_update print('auto_update if', ['off', 'on'][self.auto_update]) if ch in [ord('r'), ord('R')]: self.markers[:] = 0 self.markers_vis[:] = self.img self.sketch.show() if ch in [ord('q'), ord('Q'), ord(' ')]: # 清空标记和掩码 self.reset() self.change_thresh() self.preprocess(1) self.watershed() # 显示带有先验的图片 self.sketch.show() cv.destroyAllWindows() return self.vis def write_matrix_to_file(self, matrix): h, w = matrix.shape name = "haha.txt" f = open(name, "w") for i in range(h): for j in range(w): f.write(str(int(matrix[i][j]))) f.write("\n") f.close()
import numpy as np import cv2 from common import Sketcher if __name__ == '__main__': import sys try: fn = sys.argv[1] except: fn = '../cpp/fruits.jpg' print __doc__ img = cv2.imread(fn) img_mark = img.copy() mark = np.zeros(img.shape[:2], np.uint8) sketch = Sketcher('img', [img_mark, mark], lambda: ((255, 255, 255), 255)) while True: ch = 0xFF & cv2.waitKey() if ch == 27: break if ch == ord(' '): res = cv2.inpaint(img_mark, mark, 3, cv2.INPAINT_TELEA) cv2.imshow('inpaint', res) if ch == ord('r'): img_mark[:] = img mark[:] = 0 sketch.show() cv2.destroyAllWindows()
if __name__ == '__main__': import sys try: fn = sys.argv[1] except: fn = '../data/fruits.jpg' print(__doc__) img = cv2.imread(fn) if img is None: print('Failed to load image file:', fn) sys.exit(1) img_mark = img.copy() mark = np.zeros(img.shape[:2], np.uint8) sketch = Sketcher('img', [img_mark, mark], lambda : ((255, 255, 255), 255)) while True: ch = 0xFF & cv2.waitKey() if ch == 27: break if ch == ord(' '): res = cv2.inpaint(img_mark, mark, 3, cv2.INPAINT_TELEA) cv2.imshow('inpaint', res) if ch == ord('r'): img_mark[:] = img mark[:] = 0 sketch.show() cv2.destroyAllWindows()
def main(): """ Main method of the program. """ # Pull system arguments try: fn = sys.argv[1] except: fn = INPUT_IMAGE # Load the image and store into a variable #image = cv2.imread(cv2.samples.findFile(fn)) image = cv2.imread('mango_img_752.png') if image is None: print('Failed to load image file:', fn) sys.exit(1) # Create an image for sketching the mask image_mark = image.copy() sketch = Sketcher('Image', [image_mark], lambda: ((255, 255, 255), 255)) # Sketch a mask while True: ch = cv2.waitKey() if ch == 27: # ESC - exit break if ch == ord('r'): # r - mask the image break if ch == ord(' '): # SPACE - reset the inpainting mask image_mark[:] = image sketch.show() # define range of white color in HSV lower_white = np.array([0, 0, 255]) upper_white = np.array([255, 255, 255]) # Create the mask mask = cv2.inRange(image_mark, lower_white, upper_white) # Create the inverted mask mask_inv = cv2.bitwise_not(mask) # Convert to grayscale image gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Extract the dimensions of the original image rows, cols, channels = image.shape #print(image.shape) image = image[0:rows, 0:cols] # Bitwise-OR mask and original image colored_portion = cv2.bitwise_or(image, image, mask=mask) colored_portion = colored_portion[0:rows, 0:cols] cv2.imshow('Mask', colored_portion) # Bitwise-OR inverse mask and grayscale image gray_portion = cv2.bitwise_or(gray, gray, mask=mask_inv) gray_portion = np.stack((gray_portion, ) * 3, axis=-1) # Combine the two images output = colored_portion + gray_portion # Save the image #cv2.imwrite(OUTPUT_IMAGE, output) # Create a table showing input image, mask, and output mask = np.stack((mask, ) * 3, axis=-1) table_of_images = np.concatenate((image, mask, output), axis=1) #cv2.imwrite(TABLE_IMAGE, table_of_images) # Display images, used for debugging '''cv2.imshow('Original Image', image) cv2.imshow('Sketched Mask', image_mark) cv2.imshow('Mask', mask) cv2.imshow('Output Image', output)''' #cv2.imshow('Table of Images', table_of_images) cv2.waitKey(0) # Wait for a keyboard event
def main(): try: fn = sys.argv[1] except: fn = INPUT_IMAGE image = cv2.imread(cv2.samples.findFile(fn)) # Resim bulunamazsa program bozulmasın if image is None: print('Failed to load image file:', fn) sys.exit(1) # Common dosyamızdan örnek oluşturalım image_mark = image.copy() sketch = Sketcher('Image', [image_mark], lambda: ((255, 255, 255), 255)) # Kullanıcı Komut alanı while True: ch = cv2.waitKey() if ch == 27: # ESC - exit break if ch == ord('r'): # r - mask the image break if ch == ord(' '): # SPACE - reset the inpainting mask image_mark[:] = image sketch.show() # Ten rengine göre uygun renk aralığı ataması lower_white = np.array([50, 50, 50]) upper_white = np.array([255, 255, 255]) mask = cv2.inRange(image_mark, lower_white, upper_white) # Maskeleme işlemi mask_inv = cv2.bitwise_not(mask) # Renk dönüşüm alanı gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) rows, cols, channels = image.shape image = image[0:rows, 0:cols] colored_portion = cv2.bitwise_or(image, image, mask=mask) colored_portion = colored_portion[0:rows, 0:cols] gray_portion = cv2.bitwise_or(gray, gray, mask=mask_inv) gray_portion = np.stack((gray_portion, ) * 3, axis=-1) # Çıktı oluşturulan alan output = colored_portion + gray_portion # Çıktıyı kayıt edelim cv2.imwrite(OUTPUT_IMAGE, output) # Fotoğrafın ilk ve son halini tabloya dönüştürelim mask = np.stack((mask, ) * 3, axis=-1) table_of_images = np.concatenate((image, mask), axis=1) cv2.imwrite(TABLE_IMAGE, table_of_images) # Kullanıcıya gösterelim #cv2.imshow('Original Image', image) #cv2.imshow('Output Image', output) cv2.imshow('Table of Images', table_of_images) cv2.waitKey(0)
class TestOCR: ESC = 27 KEY_UP = 0 KEY_DOWN = 1 def __init__(self, model, filenames, flags, pattern): self.ocr = OCR() self.ocr.loadModel(model, OCR.MODEL_ANN, flags) if filenames: counter = {} for filename in filenames: ch = self.ocr.charFromFile(filename) if pattern: label = self.__exec(pattern, filename) if label in counter: predicted, total = counter[label] counter[label] = (predicted + int(label == ch), total + 1) else: counter[label] = (int(label == ch), 1) print "%s in %s" % (ch, filename) if pattern: self.__analyze(counter) else: h, w = 200, 200 img = np.zeros((h, w, 3), np.uint8) img[:,:] = (255, 255, 255) self.brush = Brush("Brush") self.sketcher = Sketcher('Test OCR', img, self.brush) def __analyze(self, counter): print "" totalPercent = [] for label, (predicted, total) in sorted(counter.iteritems()): percent = int(float(predicted) / total * 100) totalPercent.append(percent) print "{0}\t{1} / {2} - {3} %".format(label, predicted, total, percent) print "\nTotal recognized : {0:.1f} %\n".format(np.mean(totalPercent)) def __exec(self, file, arg): out = subprocess.Popen([file, arg], stdout=subprocess.PIPE).communicate()[0] return out def __displayResponse(self, ch): response = np.zeros((75, 75, 3), np.uint8) response[:,:] = (255, 255, 255) cv2.putText(response, ch, (15, 35), cv2.FONT_HERSHEY_PLAIN, 3.0, (0, 0, 0), 1) cv2.imshow("Response", response) def run(self): while True: k = cv2.waitKey(0) & 0xFF if k == self.ESC: break elif k == ord('r'): self.sketcher.reset() self.sketcher.show() elif k == ord(' '): ch = self.ocr.charFromImage(self.sketcher.sketch) self.sketcher.show() self.sketcher.reset() self.__displayResponse(ch) print "OCR : %s" % ch elif k == self.KEY_UP: self.brush.brushSize += 1 elif k == self.KEY_DOWN: self.brush.brushSize -= 1
def main(): """ Main method of the program. """ imgList = [] for img in all_files: imgList.append(img) print(len(imgList)) INPUT_IMAGE = imgList[14] IMAGE_NAME = INPUT_IMAGE[:INPUT_IMAGE.index(".")] OUTPUT_IMAGE = IMAGE_NAME + "_output.png" try: fn = sys.argv[1] except: fn = INPUT_IMAGE image = cv2.imread(INPUT_IMAGE) #cv2.imshow('Mask', image) #print(img) if image is None: print('Failed to load image file:', fn) sys.exit(1) # Create an image for sketching the mask image_mark = image.copy() sketch = Sketcher('Image', [image_mark], lambda: ((255, 255, 255), 255)) while True: ch = cv2.waitKey() if ch == 27: # ESC - exit cv2.destroyAllWindows() break if ch == ord('r'): # r - mask the image break if ch == ord(' '): # SPACE - reset the inpainting mask image_mark[:] = image sketch.show() # define range of white color in HSV lower_white = np.array([0, 0, 255]) upper_white = np.array([255, 255, 255]) # Create the mask mask = cv2.inRange(image_mark, lower_white, upper_white) # Create the inverted mask mask_inv = cv2.bitwise_not(mask) # Convert to grayscale image gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Extract the dimensions of the original image rows, cols, channels = image.shape #print(image.shape) image = image[0:rows, 0:cols] # Bitwise-OR mask and original image colored_portion = cv2.bitwise_or(image, image, mask=mask) colored_portion = colored_portion[0:rows, 0:cols] os.chdir( "/Users/pavankumar/Documents/Processed Datasets/scripts/msk_unripen/") #out = os.path.join(os.getcwd() , '/MaskedImages/').join(OUTPUT_IMAGE) print(OUTPUT_IMAGE) cv2.imwrite(OUTPUT_IMAGE, colored_portion) cv2.waitKey(0) # Wait for a keyboard event
def draw(h, w, tah): canvas = np.ones((h + tah, w), dtype=np.float32) cv2.line(canvas, (0, tah - 1), (w, tah - 1), (0, 0, 0), 1) cv2.line(canvas, (201, 0), (201, h + tah), (.5, 255, 255), 1) cv2.line(canvas, (402, 0), (402, h + tah), (.5, 255, 255), 1) font = cv2.FONT_HERSHEY_PLAIN font2 = cv2.FONT_HERSHEY_TRIPLEX font3 = cv2.FONT_HERSHEY_SIMPLEX font4 = cv2.FONT_HERSHEY_DUPLEX #left box text1 = "Press 'C' to clear" cv2.putText(canvas, text1, (20, 10), font, 0.9, (0.5, 255, 0), 1) text2 = "Press 'E' to evaluate" cv2.putText(canvas, text2, (20, 22), font, 0.9, (0.5, 255, 0), 1) #middle box text3 = "Prediction" cv2.putText(canvas, text3, (240, 22), font3, .7, (0, 255, 0), 1) #right box text4 = "Confidence" cv2.putText(canvas, text4, (440, 22), font3, .7, (0, 255, 0), 1) img = canvas.copy() sketch = Sketcher(name, [img], lambda: ((0, 255, 0), 255)) while (True): if cv2.waitKey(0) == ord('c'): # clear the screen print("Clear") img[:] = canvas sketch.show() if cv2.waitKey(0) == ord('e'): # evaluate drawing # remove previous results from screen cv2.rectangle(img, (202, tah), (400, h + tah), (255, 255, 255), -1) cv2.rectangle(img, (404, tah), (600, h + tah), (255, 255, 255), -1) img2 = process( img) #call process img to apply filtering and edge detection #convert image into tensor input to_eval = tf.estimator.inputs.numpy_input_fn(x={"x": img2}, y=None, shuffle=False) # evaluate input image, Returns np.array output1 = classifier.predict(input_fn=to_eval) output2 = classifier.predict(input_fn=to_eval) #extract prediction class and probabilities from classfier output_classes = [c['classes'] for c in output1] output_prob = [p['probabilities'] for p in output2] #sort probabilities probs = output_prob[0] vals = [] for i in range(0, len(probs)): vals.append((i, probs[i])) sorted_probs = sorted(vals, key=lambda x: x[1]) #determine confidency level if sorted_probs[-1][1] >= 0.9: conf = 'Very Confident' elif sorted_probs[-1][1] < 0.9 and sorted_probs[-1][1] >= 0.8: conf = 'Confident' elif sorted_probs[-1][1] < 0.8 and sorted_probs[-1][1] >= 0.65: conf = 'Somewhat Confident' elif sorted_probs[-1][1] < 0.65: conf = 'Not Confident' # print evaluation confidence prob2 = str(sorted_probs[-2][0]) + ' = ' + str(sorted_probs[-2][1]) cv2.putText(img, conf, (430, 115), font3, .6, (0, 255, 0), 1) # print prediction output = str(output_classes) cv2.putText(img, str(output[1]), (250, 150), font4, 4, (0, 255, 0), 3) sketch.show() print("Eval") if (cv2.getWindowProperty(name, 0) == -1) or (cv2.waitKey() == 27): break cv2.destroyAllWindows()