Tensorflow对象检测API:检测到特定类别时将GPIO引脚拉高

时间:2019-02-28 11:31:23

标签: python tensorflow raspberry-pi

我正在使用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)是获取检测到的类的一种方法吗?

3 个答案:

答案 0 :(得分:0)

您可以通过/ 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

答案 1 :(得分: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

答案 2 :(得分:0)

代码非常简单:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: 2019-03-01T07:08:29Z
  generateName: vm-wojtek-7674c7b54d-
  labels:
    app: vm-wojtek
    pod-template-hash: "3230736108"
  name: vm-wojtek-7674c7b54d-js6d6
  namespace: default
  ownerReferences:
  - apiVersion: extensions/v1beta1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: vm-wojtek-7674c7b54d
    uid: d12ee2d5-3bf0-11e9-a82b-aaf390a8b6f0
  resourceVersion: "60801"
  selfLink: /api/v1/namespaces/default/pods/vm-wojtek-7674c7b54d-js6d6
  uid: d1329b70-3bf0-11e9-a82b-aaf390a8b6f0
spec:
  containers:
  - env:
    - name: KUBERNETES_PORT_443_TCP_ADDR
      value: vm-tf-agent-b84257d1.hcp.westeurope.azmk8s.io
    - name: KUBERNETES_PORT
      value: tcp://vm-tf-agent-b84257d1.hcp.westeurope.azmk8s.io:443
    - name: KUBERNETES_PORT_443_TCP
      value: tcp://vm-tf-agent-b84257d1.hcp.westeurope.azmk8s.io:443
    - name: KUBERNETES_SERVICE_HOST
      value: vm-tf-agent-b84257d1.hcp.westeurope.azmk8s.io
    image: ...
    imagePullPolicy: Always
    name: vm-wojtek
    ports:
    - containerPort: 8080
      protocol: TCP
    - containerPort: 8443
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /mnt/azure
      name: volume
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-n2vjm
      readOnly: true
  dnsPolicy: ClusterFirst
  nodeName: aks-default-36880111-0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - azureFile:
      secretName: azure-secret
      shareName: vm-share
    name: volume
  - name: default-token-n2vjm
    secret:
      defaultMode: 420
      secretName: default-token-n2vjm
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: 2019-03-01T07:08:29Z
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: 2019-03-01T07:10:31Z
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: 2019-03-01T07:08:29Z
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://17781db099f449df1b56e53b358021c06f7e140f9610d9d09bb4cf366956c687
    image: vmrdacr.azurecr.io/vm-2.0.4:v11
    imageID: ...
    lastState: {}
    name: vm-wojtek
    ready: true
    restartCount: 0
    state:
      running:
        startedAt: 2019-03-01T07:10:31Z
  hostIP: 10.240.0.4
  phase: Running
  podIP: 10.244.0.9
  qosClass: BestEffort
  startTime: 2019-03-01T07:08:29Z