Ejemplo n.º 1
0
def prod_apply(username, password, channel):
    modelMeta = ws.modelMeta(model)
    print(modelMeta)
    with tf.Session() as sess:
        saver = tf.train.import_meta_graph(modelMeta, clear_devices=True)
        saver.restore(sess, modelStore)
        x = tf.get_collection('x')[0]
        y_conv = tf.get_collection('y_conv')[0]
        keep_prob = tf.get_collection('keep_prob')[0]
        pred = tf.argmax(y_conv, 1)

        applyStore = TrainStore(ws, 'apply').getRootPath()
        urlSnapshot = UrlSnapshot(applyStore, username, password, channel)
        while (True):
            jpgFile = urlSnapshot.pull()
            if jpgFile is not None:
                # print detail
                imgArray = jb.imgResize2Array(jpgFile, imgWidth)
                print(y_conv.eval(feed_dict={x: [imgArray], keep_prob: 1.0}))
                label, moveTo = classify(pred, x, keep_prob, jpgFile)
                if sendmail:
                    conf['email']['body'] = label
                    jbMail = JbMail(conf)
                    jbMail.attachImages([moveTo])
                    print('sending email ...')
                    # jbMail.send()
                    jbMail.send_ssl()
            sleep(15)
            break
Ejemplo n.º 2
0
def prod_apply():
    modelMeta = ws.modelMeta(model)
    print(modelMeta)
    with tf.Session() as sess:
        saver = tf.train.import_meta_graph(modelMeta, clear_devices=True)
        saver.restore(sess, modelStore)
        x = tf.get_collection('x')[0]
        y_conv = tf.get_collection('y_conv')[0]
        keep_prob = tf.get_collection('keep_prob')[0]
        pred = tf.argmax(y_conv, 1)

        while (True):
            jpgFile = getSnapshot()
            if jpgFile is not None:
                imgArray = jb.imgResize2Array(jpgFile, imgWidth)
                print(y_conv.eval(feed_dict={x: [imgArray], keep_prob: 1.0}))
                label, moveTo = classify(pred, x, keep_prob, jpgFile)
                if sendmail and label == 'visitor':
                    conf['email']['body'] = label
                    jbMail = JbMail(conf)
                    jbMail.attachImages([moveTo])
                    print('sending email ...')
                    # jbMail.send()
                    jbMail.send_ssl()
            sleep(30)
Ejemplo n.º 3
0
def learn():
    x = tf.placeholder(tf.float32, [None, imgWidth * imgHeight])
    # Define loss and optimizer
    y_ = tf.placeholder(tf.float32, [None, labelSize])
    y_conv, keep_prob, train_step, accuracy = mnstModel(
        x, y_, imgWidth, imgHeight, labelSize)

    # for save model
    saver = tf.train.Saver()
    tf.add_to_collection('x', x)
    tf.add_to_collection('keep_prob', keep_prob)
    tf.add_to_collection('y_conv', y_conv)

    # Import data
    learnStore = TrainStore(ws, 'learn')
    trainData = jb.ImgLabelData(learnStore)

    applyStore = TrainStore(ws, 'apply')
    testData = jb.ImgLabelData(applyStore)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for i in range(trainIteration):
            batch = trainData.nextBatch(batchSize, imgWidth)
            if i % 50 == 0:
                train_accuracy = accuracy.eval(feed_dict={
                    x: batch[0],
                    y_: batch[1],
                    keep_prob: 1.0
                })
                print('step %d, training accuracy %g' % (i, train_accuracy))
            train_step.run(feed_dict={
                x: batch[0],
                y_: batch[1],
                keep_prob: 0.5
            })

        saver.save(sess, modelStore)
        print("model saved in", modelStore)

        testBatch = testData.nextBatch(2)
        print('test accuracy %g' % accuracy.eval(feed_dict={
            x: testBatch[0],
            y_: testBatch[1],
            keep_prob: 1.0
        }))
Ejemplo n.º 4
0
def classify(pred, x, keep_prob, jpg):
    applyStore = TrainStore(ws, 'apply')
    labels = applyStore.getLabels()
    imgArray = jb.imgResize2Array(jpg, imgWidth)
    classification = pred.eval(feed_dict={x: [imgArray], keep_prob: 1.0})
    label = labels[classification[0]]
    mvTo = jpg.replace('/apply/', '/apply/' + label + '/')
    print(mvTo, label)
    os.rename(jpg, mvTo)
    return label, mvTo
Ejemplo n.º 5
0
def daily_apply():
    modelMeta = ws.modelMeta(model)
    print(modelMeta)
    with tf.Session() as sess:
        saver = tf.train.import_meta_graph(modelMeta, clear_devices=True)
        saver.restore(sess, modelStore)
        x = tf.get_collection('x')[0]
        y_conv = tf.get_collection('y_conv')[0]
        keep_prob = tf.get_collection('keep_prob')[0]
        pred = tf.argmax(y_conv, 1)

        applyStore = TrainStore(ws, 'apply')
        labels = applyStore.getLabels()
        count = {}
        for label in labels:
            count[label] = 0
        archives = getDailyList()
        print(archives)
        for archive in archives:
            if ".tar.gz" not in archive:
                continue
            file = getDailyArchive(archive)
            dailyApplyFolder = extractArchive(file)
            for label in labels:
                wd = dailyApplyFolder + '/' + label
                if not os.path.exists(wd):
                    os.makedirs(wd)
            print(dailyApplyFolder)
            jpgs = glob.glob(dailyApplyFolder + '/*.jpg')
            for jpgFile in jpgs:
                imgArray = jb.imgResize2Array(jpgFile, imgWidth)
                print(y_conv.eval(feed_dict={x: [imgArray], keep_prob: 1.0}))
                label, moveTo = dailyClassify(pred, x, keep_prob, jpgFile,
                                              dailyApplyFolder)
                count[label] = count[label] + 1
                # if sendmail and label == 'visitor':
                #     conf['email']['body'] = label
                #     jbMail = JbMail(conf)
                #     jbMail.attachImages([moveTo])
                #     print('sending email ...')
                #     # jbMail.send()
                #     jbMail.send_ssl()
            delDailyArchive(archive)
            print(count)
            break