惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
F
Fortinet All Blogs
A
About on SuperTechFans
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
P
Proofpoint News Feed
D
Docker
Jina AI
Jina AI
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
I
InfoQ
Recorded Future
Recorded Future
爱范儿
爱范儿
Last Week in AI
Last Week in AI
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
L
LINUX DO - 热门话题
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
Lohrmann on Cybersecurity
The Last Watchdog
The Last Watchdog
V
Visual Studio Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Privacy International News Feed
博客园 - 三生石上(FineUI控件)
Schneier on Security
Schneier on Security
Simon Willison's Weblog
Simon Willison's Weblog
Martin Fowler
Martin Fowler
雷峰网
雷峰网
Latest news
Latest news
Scott Helme
Scott Helme
T
Tenable Blog
Vercel News
Vercel News
宝玉的分享
宝玉的分享
PCI Perspectives
PCI Perspectives
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
Attack and Defense Labs
Attack and Defense Labs
Spread Privacy
Spread Privacy
量子位
H
Heimdal Security Blog

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟 从Python转向Pharo https://blog.just4fun.site/post/iot/iot-open-source-projects/ Python异步编程笔记 https://blog.just4fun.site/post/iot/iot-open-source-hardware-community/ 万物积木化开发者社区 CodeLab ❤️ Blender Scratch3技术分析之云变量 API(第7篇) [译]对管道(Pipes)的偏爱 [译]提出正确的问题比得到正确答案更重要 蓝牙设备与Scratch3.0 创建你的第一个Scratch3.0 Extension Scratch3技术分析之项目内部数据(第6篇) Scratch3技术分析之社区 API(第5篇) Scratch3技术分析之User API(第4篇) Scratch3技术分析之项目主页API(第3篇) Scratch3技术分析之静态资源API(第2篇) Scratch3.0、micro:bit与Windows7 https://blog.just4fun.site/post/iot/zerynth-vs-micropython/ 核聚变、方所与半宅空间 可视化编程为何是个糟糕的主意 codelab.club周末聚会 关于codelab.club '下一件大事'是一个房间 Hungry Robot - Eat everything 编程作为一种思考方式 今日简史 史蒂夫·乔布斯传 罗素自选文集 https://blog.just4fun.site/post/edx/tianjin-scratch-ai/ https://blog.just4fun.site/post/edx/richie-cms-openedx/ 徒步武功山 WebUSB与micro:bit 积木化编程与3D场景 夜宿武功山顶 scratch3-adapter接入优必选Alpha系列机器人 https://blog.just4fun.site/post/edx/video-migration-note/ scratch3-adapter重构笔记 https://blog.just4fun.site/post/edx/edx-community-members/ 两种硬件编程风格的比较 使用micro:bit自制PPT翻页笔 柏拉图对话集 scratch3.0 + micro:bit 七月电影放映计划 非营利组织的管理 Screenly--用树莓派让任何屏幕变为可编程的数字标牌 以最佳实践开始你的Django项目 micro:bit与事件驱动 为Scratch3.0设计的插件系统(上篇) OCR应用一例 近两年读过的一些好书 blockly开发之使用python驱动浏览器中的turtle(2) 牛顿新传 文学理论入门 逻辑的引擎 人生的意义 blockly开发之生成并运行js代码(1) blockly开发之hello world(0) micro:bit使用笔记 神器之Termux https://blog.just4fun.site/post/iot/micropython-notes/ Cozmo what is this Scratch的前世今生 下段旅程 我行在远方 爆裂 途中杂记 https://blog.just4fun.site/post/edx/open-edx-startup/ cozmo系列之入门 - 有性格且可编程的机器人 PaperWeekly开发笔记 创业二三事
运行在树莓派中的scratch3-adapter tensorflow插件
种瓜 · 2018-12-27 · via 夜行人

文章目录

提醒

暂未升级到 3.0 , 正在进行中…


树莓派目前的浏览器不支持webGL,但scratch3.0需要webGL支持。这篇文章准备让scratch3-adapter运行在树莓派中,而scratch3.0运行在电脑上的chrome浏览器里。

scratch team正在和树莓派社区合作,推出scratch3.0的树莓派版本。所以在未来,你也可以使用树莓派中使用scratch3.0。

当我们允许scratch3.0和scratch3-adapter运行在不同机器上时,我们得到了一些有趣的副产品,诸如支持移动端(ipad/mobile)、分布式运行、远程教育等等。

在树莓派中运行scratch3-adapter

下载scratch3-adapter。建议你重新下载插件,我刚才有更新,修复了一个字节码相关的bug。

下载好之后,给予它运行权限: chmod +x xxx

之后双击运行即可.

建立连接

首先访问: https://raspberrypi.local:12358/

点击高级 > 继续前往raspberrypi.local(不安全)

当你看到hello world就ok了。

打开scratch3v3.codelab.club, 指定adapter_host参数: https://scratch3v3.codelab.club/?adapter_host=raspberrypi.local

你将看到,本地的网页已经与树莓派上的scratch3-adapter建立了连接. 之后你就可以在树莓派中构建scratch3-adapter的插件,同时在PC上的scratch3.0中操控它.和本地插件没有区别。

如果你在windows下打开scratch3.0,将raspberrypi.local替代为你的树莓派的IP。

对于mac/linux用户,如果你没有修改树莓的hostname,可以直接使用raspberrypi.local,作为adapter_host.

如果windows用户也不想每次去找树莓派的IP,需要安装Bonjour Print Services (Windows),之后raspberrypi.local就会直接指向树莓派。这部分涉及到mDNS

在树莓派安装依赖(tensorflow及其他)

scratch3-adatper默认启动为图形界面模式,所以你需要在图形界面中操作。我们之后会推出cli版本。

如果你希望一切都在浏览器中操作,可以使用noVNC,在浏览器中操作树莓派的桌面。

安装 tensorflow

tensorflow只是一个普通的Python库,scratch3-adatper允许使用Python构建插件,来拓展scrtach3.0的能力。所以这部分没有任何特殊性。构建AI插件和构建普通插件没有区别。你可以参考目前已有的插件例子:scratch3_adapter_extensions

在树莓派中安装tensorflow已经变得非常简单,tensorflow 1.9之后官方正式支持树莓派, 安装只需运行两行命令:

1
2
sudo apt install libatlas-base-dev
pip3 install tensorflow pyzmq --user

使用python3运行以下代码,看是否安装成功:

1
2
3
4
5
# python3
import tensorflow as tf
tf.enable_eager_execution()
hello = tf.constant('Hello, TensorFlow!')
print(hello) # tf.Tensor(b'Hello, TensorFlow!', shape=(), dtype=string)

安装opencv

1
2
3
4
5
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
# sudo apt-get install qt4-dev-tools
pip3 install opencv-python --user

安装 Protobuf

如果你想使用tensorflow/models,你就需要安装protobuf-compiler。

sudo apt-get install protobuf-compiler

其他

1
pip3 install matplotlib pyzmq --user

运行物体识别程序

Set up TensorFlow Directory Structure and PYTHONPATH Variable往下做相应配置。

配置好之后你应该就能在树莓派上运行Object_detection_usb_cam.py 而不报错。

程序目前使用usb摄像头,如果你想使用树莓派官方的摄像头,需要做些调整。

以上这些步骤与scratch3-adapter无关,是在树莓派中用tensorflow运行物体识别程序需要的步骤,挺繁琐的。

ps: 我们之后写一篇文章讲解在opencv中跑神经网络,而不用tensorflow,将省去很多繁琐步骤。

完成之后,我们就可以开始在scratch3-adapter中写插件了。

构建scratch3-adapter插件

首先把tensorflow物体识别程序放到用户目录下:

1
2
cd ~
wget https://raw.githubusercontent.com/wwj718/TensorFlow-Object-Detection-on-the-Raspberry-Pi/master/Object_detection_for_adapter.py

~/scratch3_adapter/extensions/构建extension_tensorflow.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import time
import zmq
from zmq import Context
import subprocess

from scratch3_adapter.core_extension import Extension
from scratch3_adapter import settings


class TensorflowExtension(Extension):
    def __init__(self):
        name = type(self).__name__ # class name
        super().__init__(name)

    def run(self):
        # REP
        port = 38779
        context = Context.instance()
        socket = context.socket(zmq.REP)
        socket.bind("tcp://*:%s" % port)
        # Object_detection_for_adapter.py 依赖于shell中的变量,所以需要在命令行里启动
        cmd = "/usr/bin/python3 /home/pi/Object_detection_for_adapter.py"
        tf = subprocess.Popen(cmd , shell = True)
        while self._running:
            tf_class = socket.recv_json().get("class")
            socket.send_json({"status":"200"})
            # 发往scratch3.0中的eim积木
            self.publish({"topic": "eim", "message": tf_class})
        # release socket
        tf.terminate()
        tf.wait()
        socket.close()
        context.term()

export = TensorflowExtension

搞定。

之后双击启动extension_tensorflow插件就行。

注意: tensorflow的model在在树莓派中加载很慢,所以从运行插件,到你看到摄像头被激活,可能有几秒钟时间。

tips

每次退出scratch3-adapter,记得取消勾选extension_tensorflow,不关闭这个进程,树莓派的很多计算资源仍然会被它占用。

如果你不慎直接退出了scratch3-adapter,可以通过ps aux |grep adapter查杀Object_detection_for_adapter.py

展示

提醒

[scratch3-adapter tensorflow插件](http(s://blog.just4fun.site/adapter-tensorflow.html)除了用于树莓派中,也完全可用在电脑上。需要对资源(model)路径做些调整(使用绝对路径)。

运行在笔记本上,处理速度要快很多。

todo

在未来,我们会直接发布内置scratch3-adapter的树莓派镜像,支持tensorflow/opencv/dlib/gpio。开箱即用,只需要将树莓派接入局域网(将内置wifi-connect来简化wifi连接),你就可以立刻用scratch3.0来为它编程。

参考