提醒

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


我们在上一篇文章里介绍了如何制作运行在树莓派中的scratch3-adapter tensorflow插件。

tensorflow models的使用非常繁琐。本文介绍如何使用OpenCV的dnn模块调用TesorFlow训练的模型,实现物体检测。

配置要简单的多。

在树莓派中运行scratch3-adapter

这部分的操作与上篇文章相同.

安装依赖

1
2
3
4
5
6
7
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 install libatlas-base-dev
# sudo apt-get install qt4-dev-tools
# 如果失败 则:  sudo apt-get install qt4-dev-tools --fix-missing
pip3 install opencv-python imutils pyzmq --user

下载物体检测程序(包含模型)

1
2
cd ~/scratch3_adapter
git clone https://github.com/wwj718/ExploreOpencvDnn

构建scratch3-adapter插件

~/scratch3_adapter/extensions/构建extension_opencv.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
36
37
38
39
40
41
42
43
44
45
46
47
import time
import zmq
from zmq import Context
import subprocess
import pathlib
import platform

from scratch3_adapter.core_extension import Extension
from scratch3_adapter import settings


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

    def run(self):
        # REP
        port = 38780
        context = Context.instance()
        socket = context.socket(zmq.REP)
        socket.bind("tcp://*:%s" % port)
        # Object_detection_for_adapter.py 依赖于shell中的变量,所以需要在命令行里启动
        # ~/scratch3_adapter目录
        scratch3_adapter_dir = pathlib.Path.home() / "scratch3_adapter"
        script =  "{}/ExploreOpencvDnn/main_for_adapter.py".format(scratch3_adapter_dir)
        if (platform.system() == "Darwin"):
            # which python3
            python = "/usr/local/bin/python3"
        if platform.system() == "Windows":
            python = "python"
        if platform.system() == "Linux":
            python = "/usr/bin/python3"
        cmd = [python, script]
        tf = subprocess.Popen(cmd)
        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 = OpencvExtension

搞定.

双击运行scratch3-adapter. 之后勾选extension_opencv插件就行。

提醒

scratch3-adapter opencv 插件目前支持树莓派、macOS、Windows.

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

参考