【问题标题】:Tensorflow Object detection API : Turn a GPIO pin high when a specific class is detectedTensorflow 对象检测 API:检测到特定类时将 GPIO 引脚设为高电平
【发布时间】:2019-02-28 20:57:55
【问题描述】:

我正在使用 Tensorflow 对象检测 API 来检测 respberry pi 上的对象,它是实时对象检测,并且我可以正常工作。它可以绘制带有标签的边界框和它检测到的类的会议分数。所以这是我的问题:

当检测到特定类时,如何将 GPIO 引脚设为高电平,假设特定类是“人”,并且我希望引脚 11 为高电平,我该怎么做?

这是我认为相关的代码:

# Perform the actual detection by running the model with the image as input
        (boxes, scores, classes, num) = sess.run(
            [detection_boxes, detection_scores, detection_classes, num_detections],
            feed_dict={image_tensor: frame_expanded})

        # Draw the results of the detection (aka 'visulaize the results')
        vis_util.visualize_boxes_and_labels_on_image_array(
            frame,
            np.squeeze(boxes),
            np.squeeze(classes).astype(np.int32),
            np.squeeze(scores),
            category_index,
            use_normalized_coordinates=True,
            line_thickness=3,
            min_score_thresh=0.40)

        cv2.putText(frame,"FPS: {0:.2f}".format(frame_rate_calc),(30,50),font,1,(255,255,0),2,cv2.LINE_AA)

        # All the results have been drawn on the frame, so it's time to display it.
        cv2.imshow('Object detector', frame)

np.squeeze(classes).astype(np.int32) 可以成为获取检测到的类的一种方式吗?

【问题讨论】:

    标签: python tensorflow raspberry-pi


    【解决方案1】:

    您可以通过 /sys/class/gpio 接口控制 GPIO。就我而言,我使用另一个嵌入式系统。但它应该或多或少相同。我正在使用 bash 命令。但是您可以通过 Python 文件操作轻松替换这些。

    ubuntu@localhost:/sys/class/gpio$ ls
    export  gpiochip1008  gpiochip1016  gpiochip890  unexport
    

    gpio 接口可以有多个。使用接口:

    启用前两位

    echo 1008 > export
    echo 1009 > export
    

    这将为新的 IO 文件创建一个编号:

    root@localhost:/sys/class/gpio# ls -al
    total 0
    drwxr-xr-x  2 root root    0 Feb 11 17:05 .
    drwxr-xr-x 45 root root    0 Feb 11 16:45 ..
    --w-------  1 root root 4096 Feb 11 17:08 export
    lrwxrwxrwx  1 root root    0 Feb 11 16:53 gpio1008 -> ../../devices/soc0/amba_pl/41200000.gpio/gpiochip1/gpio/gpio1008
    lrwxrwxrwx  1 root root    0 Feb 11 17:02 gpio1009 -> ../../devices/soc0/amba_pl/41200000.gpio/gpiochip1/gpio/gpio1009
    --w-------  1 root root 4096 Feb 11 17:05 unexport
    

    将输出的方向设置为“out”(默认为“in”)

    echo out > gpio1008/direction
    echo out > gpio1009/direction
    

    使引脚变高:

    echo 1 > gpio1016/value
    echo 1 > gpio1017/value
    

    【讨论】:

      【解决方案2】:

      代码很简单:

      import RPi.GPIO as GPIO
      
      GPIO.setwarnings(False)
      GPIO.setmode(GPIO.BOARD)
      GPIO.setup(11, GPIO.OUT, initial=GPIO.LOW)
      GPIO.setup(13, GPIO.OUT, initial=GPIO.LOW)
      
      if object_detected_class==1:
          GPIO.output(11, GPIO.HIGH)
      
      else: #other class
          GPIO.output(13, GPIO.HIGH)
      

      【讨论】:

        【解决方案3】:

        您可以使用 0.3 或其他东西过滤检测分数,而不是如果检测类在过滤索引中包含该类,您可以发送高到 pin

        from gpiozero import LED
        
        led = LED(11) #pin you plug led
        filter=0.3 #filter of scores if you decrease it program finds more item
        selected_class=4 #you want to find class
        isledhigh=False
        ...
            makeledhigh=False
            (boxes, scores, classes, num) = sess.run(
                    [detection_boxes, detection_scores, detection_classes, num_detections],
                    feed_dict={image_tensor: frame_expanded})
            for i in range(int(num[0])):
                if classes[i]==selected_class and scores[i]>=filter:
                    makeledhigh=True
            if makeledhigh and !isledhigh:
                led.on()
                isledhigh=True
            if isledhigh and !makeledhigh:
                led.off()
                isledhigh=False
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-01-17
          • 1970-01-01
          • 2017-12-02
          • 1970-01-01
          • 1970-01-01
          • 2020-11-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多