0

运行在树莓派中的 scratch3-adapter opencv 插件

 2 years ago
source link: http://wwj718.github.io/post/%E5%B0%91%E5%84%BF%E7%BC%96%E7%A8%8B/adapter-opencv/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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


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

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

配置要简单的多。

在树莓派中运行scratch3-adapter

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

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

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

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

构建scratch3-adapter插件

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

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.

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK