def show_line_field(im, p1, p2): distance_from_line_field(im, *(p1 + p2) ) fig, ax = plt.subplots(2) ax[0].imshow(im, cm.rainbow_r, interpolation = 'nearest') ax[0].plot( *zip(p1, p2), color='w' ) ax[0].set_xlim( 0, xres-1) ax[0].set_ylim( 0, yres-1) pixels = [] def putpixel_collect(im, x1, y1, value): putpixel (im, x1, y1, value) pixels.append ( (x1, y1) ) im2 = np.copy(im) line_bresenham_int(im2, *(p1 + p2 + ( 128, putpixel_collect)) ) ax[1].imshow(im2, cm.rainbow_r, interpolation = 'nearest') ax[1].plot( *zip(p1, p2), color='b' ) ax[1].plot( *zip(*pixels), color='b', marker='o', markersize=2, linestyle='None' ) ax[1].set_xlim( 0, xres-1) ax[1].set_ylim( 0, yres-1) plt_show()
def show_line_vs_pixel_grid(xres, yres): p1 = ( xres // 4, yres // 4) p2 = ( 3 * xres // 4 , 3 * yres // 4 ) fix, axis = plt.subplots (1) axis.set_xlim( 0, xres-1) axis.set_ylim( 0, yres-1) axis.set_title('Line: (%s, %s) - (%s, %s)' % (p1 + p2) ) start, end = axis.get_xlim() for x in np.arange(start + 0.5, end + 0.5, 1): # draw lines at half-points manually axis.axvline (x, color='lightgray') axis.xaxis.set_ticks(xrange(int(start), int(end+1))) start, end = axis.get_ylim() for y in np.arange(start + 0.5, end + 0.5, 1): axis.axhline (y, color='lightgray') axis.yaxis.set_ticks(xrange(int(start), int(end+1))) axis.plot( *zip(p1, p2), color='g' ) axis.plot( *zip(p1, p2), color='b', marker='o', markersize=3, linestyle='None' ) plt_show()
def on_click(event): """Plot click event handler - zoom into set""" global box print event.inaxes box_width = (box[1].real - box[0].real) box_height = (box[1].imag - box[0].imag) center = complex(box[0].real + (float(event.xdata)/get_image_size(img)[0])*box_width, box[0].imag + (1.0-(float(event.ydata)/get_image_size(img)[1]))*box_height) new_box_size = complex(box_width/1.5, box_height/1.5) box[0] = center - new_box_size/2 box[1] = center + new_box_size/2 render_mandel_iterative(img, box[0], box[1], max_iter) ax.imshow(img, cm.spectral, interpolation='nearest') plt_show()
def show_line_dda_with_rounding_error(xres, yres): im = get_grayscale_image(xres, yres) p1 = ( xres // 5, yres // 3 ) p2 = ( 4 * xres // 5, 2 * yres // 3) dx = abs(p1[0]-p2[0]) dy = abs(p1[1]-p2[1]) args = {} if dx > dy: args['sharex'] = True else: args['sharey'] = True error = line_dda(im, *(p1 + p2 + ( 255, )) ) #fig, axis = plt.subplots( 2, **args ) grid = gridspec.GridSpec (2, 1, height_ratios = [2, 1]) axis = [plt.subplot(grid[i]) for i in [0, 1]] axis[0].imshow(im, cm.gray, interpolation = 'nearest') axis[0].plot( *zip(p1, p2), color='g' ) axis[0].set_xlim( 0, xres-1) axis[0].set_ylim( 0, yres-1) axis[0].xaxis.set_ticks( xrange(0, xres, 8) ) axis[0].grid(True, color = 'white', which = 'major') axis[1].plot(*zip(*error)) axis[1].set_xlim( 0, xres-1) #axis[1].xaxis.set_ticks(0, xres-1 ) axis[1].xaxis.set_ticks( xrange(0, xres, 8) ) axis[1].grid(True, which = 'major') #axis[1].set_aspect(1) #axis[1].set_autoscaley_on(False) #axis[1].set_ylim( -0.6, 0.6 ) plt.tight_layout() plt_show()
def show_image(path): # edges = engine.edges img, faces = engine.read_image(path) _, landmarks = faces[0] # def get_color(idx1, idx2): # f = (engine.get_landmark_id(idx1) == engine.LANDMARK_LEFT_EYE) \ # + (engine.get_landmark_id(idx2) == engine.LANDMARK_LEFT_EYE) # if f == 0: # f = (engine.get_landmark_id(idx1) == engine.LANDMARK_RIGHT_EYE) \ # + (engine.get_landmark_id(idx2) == engine.LANDMARK_RIGHT_EYE) # if f == 0: # return 0, 0, 255 # if f == 1: # return 255, 0, 0 # if f == 2: # return 0, 255, 0 # return 255, 255, 255 # # for i, j in edges: # cv2.line(img, landmarks[i], landmarks[j], get_color(i, j), 1, cv2.LINE_AA) for a, b, c in engine.triangles: cv2.line(img, landmarks[a], landmarks[b], (0, 0, 255), 1, cv2.LINE_AA) cv2.line(img, landmarks[a], landmarks[c], (0, 0, 255), 1, cv2.LINE_AA) cv2.line(img, landmarks[c], landmarks[b], (0, 0, 255), 1, cv2.LINE_AA) for triangle in engine.bound_triangles: ps = list( map( lambda x: landmarks[x] if x >= 0 else engine.get_bound_point(img, x), triangle)) # print(ps[0], ps[1], ps[2]) cv2.line(img, ps[0], ps[1], (0, 255, 0), 1, cv2.LINE_AA) cv2.line(img, ps[0], ps[2], (0, 255, 0), 1, cv2.LINE_AA) cv2.line(img, ps[2], ps[1], (0, 255, 0), 1, cv2.LINE_AA) utils.plt_show(img)
def main(): # engine.save_face_models(FACE_MODEL_FILE, 'Data/img/005.jpg') engine.load_face_models(FACE_MODEL_FILE) # img_filename = 'Data/img/005.jpg' img_filename = 'Test/0007_01.jpg' # show_image(img_filename) # init_svm(train=True, test=True) # init_svm(train=False, test=False) # img, img_morph = svm_beautify(img_filename) init_knn(train=False) img, img_morph = knn_beautify(img_filename, 'male') # img, img_morph = make_bigger_eyes(img_filename, 0.05) # img, img_morph = make_thinner_outline(img_filename, 0.1) utils.plt_show(img) utils.plt_show(img_morph)
def process_batch(batch, show=False): output = np.clip(utils.tensor_to_img(batch)[0], 0., 1.) plt.imsave(args['output_path'], output) if show: utils.plt_show(output) return output
import matplotlib.pyplot as plt import matplotlib.cm as cm import numpy as np fig, ax = plt.subplots(1) xres, yres = 100, 80 img = get_grayscale_image(xres, yres) ticks = np.linspace(0, min(xres, yres)-1, endpoint=True, num=9).astype(np.int32) x, y = np.meshgrid(ticks, ticks) pts = np.dstack((y, x)) c = 0 for i in xrange(pts.shape[0]-1): for j in xrange(pts.shape[1]-1): draw_polygon(img, [pts[j][i], pts[j+1][i], pts[j+1][i+1], pts[j][i+1]], (((i+j) % 2) + 1) * 100) c += 1 ax.imshow(img, cm.gray, interpolation='nearest') plt_show()
face_detector = mtcnn.MTCNN() face_encoder = load_model(encoder_model) img = cv2.imread(test_img_path) # plt_show(img) img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) results = face_detector.detect_faces(img_rgb) for res in results: face, pt_1, pt_2 = get_face(img_rgb, res['box']) encode = get_encode(face_encoder, face, required_size) encode = l2_normalizer.transform(np.expand_dims(encode, axis=0))[0] name = 'unknown' distance = float("inf") for db_name, db_encode in encoding_dict.items(): dist = cosine(db_encode, encode) if dist < recognition_t and dist < distance: name = db_name distance = dist if name == 'unknown': cv2.rectangle(img, pt_1, pt_2, (0, 0, 255), 2) cv2.putText(img, name, pt_1, cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2) else: cv2.rectangle(img, pt_1, pt_2, (0, 255, 0), 2) cv2.putText(img, name + f'__{distance:.2f}', pt_1, cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2) cv2.imwrite(test_res_path, img) plt_show(img)
import matplotlib.pyplot as plt import cv2 import numpy as np from utils import process_config,frame_factory,frame_diff,thresh_otsu,concatenate,resize_and_gray,show_img,open_op,PIL_filter,\ plt_show,shape_filter from ipywidgets import interact, FloatSlider params = process_config('..\\config.cfg') frames = frame_factory(params) fgbg = cv2.createBackgroundSubtractorMOG2() for i in range(200): img, gray = resize_and_gray(frames[i], True) #gray=filter_img.filter(gray) fgmask = fgbg.apply(gray) #absdiff=cv2.absdiff(gray,fgbg.getBackgroundImage()) #cv2.imshow('frame',concatenate(img,fgmask)) #cv2.imshow('bs',np.concatenate([gray,absdiff],axis=1)) #k = cv2.waitKey(100) #if k == 27: # break #else: # continue #cv2.destroyAllWindows() img, gray = resize_and_gray(frames[200], True) fgmask = fgbg.apply(gray) plt_show(fgmask) plt_show(cv2.absdiff(gray, fgbg.getBackgroundImage())) absdiff = cv2.absdiff(gray, fgbg.getBackgroundImage()) ret, bg = cv2.threshold(fgmask, 126, 255, cv2.THRESH_BINARY) ret, fg = cv2.threshold(fgmask, 128, 255, cv2.THRESH_BINARY) plt_show(bg) plt_show(fg)