Esempio n. 1
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
Esempio n. 2
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
Esempio n. 3
0
def robot(username, password):
    learnStore = TrainStore(ws, 'learn').getRootPath()
    urlSnapshot = UrlSnapshot(learnStore, username, password, channel=2)
    for i in range(360):
        jpgFilename = urlSnapshot.pull()
        print(jpgFilename)
        sleep(10)
Esempio n. 4
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
        }))
Esempio 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
Esempio n. 6
0
def take_snapshot(username, password):
    url = 'http://114.35.223.91/cgi-bin/net_jpeg.cgi?ch=2'  # + ch  # + '&1514199511126'
    request = Request(url)
    credentials = ('%s:%s' % (username, password))
    encoded_credentials = base64.b64encode(credentials.encode('ascii'))
    request.add_header('Authorization',
                       'Basic %s' % encoded_credentials.decode("ascii"))

    learnStore = TrainStore(ws, 'learn').getRootPath()
    for i in range(72):
        response = urlopen(request)
        data = response.read()
        timestamp = datetime.datetime.now()
        jpgFilename = learnStore + '/at_' + timestamp.strftime(
            "%Y%m%d_%H%M%S_%f") + '.jpg'
        jpgFile = open(jpgFilename, "wb")
        jpgFile.write(data)
        jpgFile.close()
        print(jpgFilename)
        sleep(3600)