def main(): words = load_words() sess = tf.InteractiveSession() path = PATH.DATASET_DIR+'test7x1x2.record' #iterator = read_record(path, epochs=10, batch_size=1024) iterator = read_from_npy(batch_size=1024) x, y = iterator.get_next() y = tf.cast(y, tf.int64) x = tf.reshape(x, (-1, 48,48,1)) y_in = tf.one_hot(y, 3557, dtype=tf.int64) y_out = built_net(x) train_op, loss, acc = optimizer(y_in, y_out) tf.global_variables_initializer().run() t0 = time.time() for epoch in range(1, 100): for i in range(235): sess.run(train_op) if i%10 == 0: x_,y_ = sess.run([y,y_out]) print('in: %s\nout: %s' %(x_[1:10], np.argmax(y_, 1)[1:10])) losses, accurary = sess.run([loss, acc]) print('[==>] Epoch: %d \tStep: %d \tLoss: %s \tAcc: %s \tTime: %ss' %(epoch, i, losses, accurary, round(time.time()-t0, 2))) t0 = time.time()
def predict(imgs, model, xy, save=False, cls=None): img01 = imgs / 255 #print(imgs) if cls == None: cls = load_words() st = time.time() predicts = model.predict(img01) result = '' new_xy = [] for i, probabilities in enumerate(predicts): index_cls = np.argmax(probabilities) if index_cls == 3557: print(index_cls) if index_cls == 3557 or probabilities[index_cls] < 0.8: continue new_xy.append(xy[i]) result += cls[index_cls] if save: nrootdir = ("./cut_image/") if not os.path.isdir(nrootdir): os.makedirs(nrootdir) cv2.imwrite(nrootdir + cls[index_cls] + ".jpg", imgs[i]) #print('Result: %s' % (result)) et = time.time() #print('花费时间: %s' % (et - st)) return result, new_xy
def get_frame_from_camera(): print('Get serial...') p = serial.tools.list_ports.comports()[0] serialName = p[0] serialFd = serial.Serial(serialName, 9600, timeout=60) print('Loading classes...') cls = load_words() print('Loading model...') #model_path = PATH.MODEL_DIR + 'model28x3_1.h5' model_path = PATH.MODEL_DIR + 'model_keras/model.h5' model = keras.models.load_model(model_path) camera = cv2.VideoCapture(0) camera.set(3, 640) camera.set(4, 480) serialFd.write(b'@PlayFlashText#099$') state = RUN while True: _, frame = camera.read() w, h, c = frame.shape s = int(part_num / 2) w, h = int(w / part_num), int(h / part_num) focus_frame = frame[w * s:w * (s + 1), h * s:h * (s + 1), :] processed_frame, regions, xy = findContour(focus_frame) regions = np.array(regions) show_frame = draw_frame(serialFd, processed_frame, regions, xy, model, cls) state = controler(state, serialFd) show_frame = action(state, show_frame) #cv2.rectangle(show_frame,(w*s, h*s),(w*(s+1), h*(s+1)),(255, 0, 0)) cv2.imshow('processed frame', show_frame) key = cv2.waitKey(1) if key == ord('q'): break camera.release() cv2.destroyAllWindows()
def predict(data, model, save=True, cls=None): im = np.round(data / 255.) if cls == None: cls = load_words() st = time.time() predicts = model.predict(im) result = '' for i, probabilities in enumerate(predicts): index = np.argmax(probabilities) if probabilities[index] < 0.01: continue result += cls[index] if save: nrootdir = ("./pic/") if not os.path.isdir(nrootdir): os.makedirs(nrootdir) cv2.imwrite(nrootdir + cls[index] + ".jpg", data[i]) print('Result: %s' % (result)) et = time.time() print('花费时间: %s' % (et - st)) return result
def main(): cls = load_words() model_path = PATH.MODEL_DIR + 'model28x3_1.h5' print('Loading model...') model = K.models.load_model(model_path) train_font_dir = PATH.TRAIN_FONT_DIR fonts = read_fontnames(train_font_dir) while True: words = input('请输入要生成的图片文字\n') data = gene_data(words, [fonts[3]], train_font_dir) images = [] for image in np.array(data): kernel = np.ones((1, 1), np.uint8) erosion = cv2.erode(image, kernel) kernel = np.ones((2, 2), np.uint8) dilation = cv2.dilate(erosion, kernel) dilation = np.round(dilation, 0) cv2.imshow('hh', dilation) cv2.waitKey(0) images.append(dilation) images = np.array(images).reshape(-1, 48, 48, 1) predict(images, model, cls=cls)
import keras from configuration.config import PATH model_name = 'model1/model2.h5' model = keras.models.load_model(PATH.MODEL_DIR + model_name) import cv2 import numpy as np import utils.utils as tool im = cv2.imread('test_pic/13.jpg') im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) im = im.reshape([1, 48, 48, 1]) r = model.predict(im) r = np.argmax(r) cls = tool.load_words() print(cls[r])