我爱编程

Ubuntu+AndriodPad 配置FindYourCang

2018-05-08  本文已影响0人  JomarWu

步骤


安装系统

  1. 下载ubuntu-16.04.4-server-amd64.iso

  2. 根据教程制作安装U盘,选择Ubuntu Sever Installer

    image.png
  3. 进入BIOS,配置UEFI Boot mode的引导为U盘优先

  4. 插入安装U盘,重启电脑,根据教程安装Ubuntu Server

    • 选择安装盘的时候区分清楚U盘和主机硬盘
    • 选择软件包的时候,勾选standard system utilitiesOpenSSH server
  5. 拔掉U盘,重启电脑进入系统,如果显示"/dev/sda2: clean, 55880048/77066240 files, 22630945 blocks",参考教程

基本环境配置


  1. 安装系统包
sudo apt-get update && sudo apt-get upgrade -y && sudo reboot
sudo apt-get install ubuntu-desktop
sudo apt-get install -y vim git build-essential
sudo apt-get install python-dev
sudo apt-get install nginx
sudo apt-get install curl
  1. 确认Python版本是2.7
$ python -V
Python 2.7.12
  1. 下载配置GOOGLE SDK
  2. 配置Google密钥
    1. 登录Google Cloud Platform
    2. 创建一个Project
    3. 开放用到的API
      • Google Cloud Storage and Google Cloud Storage JSON API
      • Vision API
      • Speech API
      • Natural Language API
      • Cloud ML API
    4. 创建一个服务帐号密钥,选择json格式,并下载到/home/dobot/FindYourCandy
    5. 将密钥文件的名称改为credential.json
    6. 在文件~/.bashrc最后一行加入:```
      export GOOGLE_APPLICATION_CREDENTIALS="path_to_your_own_credential_file"
    7. `source ~/.bashrc`
    
  3. 安装Python包
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
sudo pip install numpy==1.12.0
  1. 安装OpenCV3.2
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libgtk2.0-dev
mkdir ~/opencv_from_git
cd ~/opencv_from_git
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
git clone https://github.com/opencv/opencv_extra.git
cd ~/opencv_from_git/opencv/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ -D BUILD_DOCS=ON -D WITH_TBB=ON ..
make -j7
sudo make install
  1. 设置设备权限
$ sudo adduser dobot dialout
$ sudo adduser dobot video
$ ls -l /dev/ttyUSB*
crwxrwxrwx 1 root dialout 188, 0 May  6 17:06 /dev/ttyUSB0
$ sudo chmod 777 /dev/ttyUSB0
$  ls -l /dev/video*
crwxrwxrwx+ 1 root video 81, 0 May  6 17:06 /dev/video0
$ sudo chmod 777 /dev/video0

下载FindYourCandy项目并校准

  1. 下载FindYourCandy项目
cd ~
git clone https://github.com/BrainPad/FindYourCandy.git
  1. 为配合GoogleML的默认配置,项目改为使用Tensorflow 1.0.1。修改~/FindYourCandy/webapp/requirements/base.txt文件内容为:

Flask==0.12
uWSGI==2.0.14

pytz==2016.10
google-cloud==0.23.0

numpy==1.12.0
scipy==0.18.1
gensim==0.13.4.1
scikit-learn==0.18.1

h5py==2.6.0
protobuf==3.2.0
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.1-cp27-none-linux_x86_64.whl

  1. 安装项目依赖
cd ~/FindYourCandy
sudo pip install -r robot-arm/requirements.txt
sudo pip install -r webapp/requirements.txt
  1. 配置环境变量
export FLASK_ENV='prd'   
export GOOGLE_APPLICATION_CREDENTIALS="path_to_your_own_credential_file"
export PYTHONPATH=/usr/local/lib/python2.7/dist-packages
  1. 下载英文词向量日语词向量到~/FindYourCandy/webapp/candysorter/resources/models
  2. 下载inception-V3模型
cd ~/FindYourCandy/webapp/candysorter/resources/models
wget http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz
tar xvzf inception-2015-12-05.tgz
  1. 修改~/FindYourCandy/webapp/candysorter/config.py

# replace "YOUR-OWN-BUCKET-NAME" to your own bucket name
CLOUD_ML_BUCKET = 'gs://{YOUR-OWN-BUCKET-NAME}'
CLOUD_ML_PACKAGE_URIS = ['gs://{YOUR-OWN-BUCKET-NAME}/package/trainer-0.0.0.tar.gz']
CLOUD_ML_PYTHON_MODULE = 'trainer.train'
CLOUD_ML_TRAIN_DIR = 'gs://{YOUR-OWN-BUCKET->NAME}/{job_id}/checkpoints'
CLOUD_ML_LOG_DIR = 'gs://{YOUR-OWN-BUCKET-NAME}/logs/{job_id}'
CLOUD_ML_DATA_DIR = 'gs://{YOUR-OWN-BUCKET-NAME}/{job_id}/features'

  1. 将代码中TF0.12.1的接口改为TF1.0.1
    #/Users/wubinbin/Developer/FindYourCandy
#FindYourCandy/train/trainer/model.py
        if not for_predict:
            # add loss operation if initializing for training
            one_hot = tf.one_hot(self.label_ids, num_classes, name='target')
            self.loss_op = tf.reduce_mean(
                # JoMar Modify
                # tf.nn.softmax_cross_entropy_with_logits(logits, one_hot)
                tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=one_hot)
                # tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=one_hot)
            )

        self.softmax_op = tf.nn.softmax(logits)
        self.saver = tf.train.Saver()

        if not for_predict:
            # add train operation and summary operation if initializing for training
            # Optimizer
            with tf.variable_scope('optimizer'):
                self.global_step = tf.Variable(0, name='global_step', trainable=False)
            # Summaries
            with tf.variable_scope('summaries'):
                # JoMar Modify
                # tf.scalar_summary('in sample loss', self.loss_op)
                # self.summary_op = tf.merge_all_summaries
                tf.summary.scalar('in sample loss', self.loss_op)
                self.summary_op = tf.summary.merge_all()
#FindYourCandy/train/trainer/train.py
        loss_log = []
        with tf.Session() as sess:
            # JoMar Modify
            # summary_writer = tf.train.SummaryWriter(self.log_dir, graph=sess.graph)
            summary_writer = tf.summary.FileWriter(self.log_dir, graph=sess.graph)
            sess.run(tf.initialize_all_variables())
  1. 代码优化
#FindYourCandy/webapp/candysorter/views/api.py
#防止没有糖果的时候出现崩溃    # Find nearest candy
    logger.info('Finding nearest candy.')
    # JoMar Modify
    listTemp = [speech_sim.dot(s) for s in candy_sims]
    if len(listTemp) < 1:
        logger.info('Can not found candy.')
    else:
        nearest_idx = np.argmax(listTemp)
        logger.info('  Nearest candy: idx=%d, url=%s', nearest_idx, candy_urls[nearest_idx])

        # Save pickup point
        logger.info('Saving pickup point.')
        nearest_centroid = candies[nearest_idx].box_centroid
        pickup_point = image_calibrator.get_coordinate(nearest_centroid[0], nearest_centroid[1])
        cache.set('pickup_point', pickup_point)
#FindYourCandy/webapp/candysorter/ext/google/cloud/ml/_http.py
#Google视觉API更新了接口
class Connection(_http.JSONConnection):
    API_BASE_URL = 'https://ml.googleapis.com'
    # JoMar Modify
    # API_VERSION = 'v1beta1'
    API_VERSION = 'v1'
    API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}'


配置安卓设备

  1. 使用安卓设备
  2. 使用ChromeV59版本,并在系统中添加到权限白名单。Tips最新版本不能开麦克风权限
    3.Tips 注意服务界面网页会连接Google Cloud

问题

升级pip后出现ImportError: cannot import name main


上一篇下一篇

猜你喜欢

热点阅读