numpy.ndarray值无法与if语句进行比较

问题描述 投票:0回答:1

我,我目前正致力于张量流图像处理项目。识别出三种类型的对象,其标记为1(汽车),2(总线)和3(货车)由's_class'表示。检测到的输出对象/ objetcs由s_class给出,它是一个numpy.ndarray。当我打印检测到的对象编号时,

(boxes, scores, classes, num) = sess.run(
    [detection_boxes, detection_scores, detection_classes, num_detections],
    feed_dict={image_tensor: frame_expanded})
s_class = classes[scores > 0.6]
print(s_class)

它给出了像,

>>[2.]    #bus is detected
  [3. 2. 1.]    #all three objects(van,bus,car) are detected
  [1. 2. 3.]    
  [1. 2. 3.]
  [1. 2. 3.]
  [2. 1. 3.]
  [2. 1. 3.]
  [2. 1. 3.]
  [2. 1. 3.]
  [2. 3. 1.]
  [2. 3. 1.]
  []              #nothing is detected
  [2.]            #only second object is detected
  []
  [1.]            #only first object is detected

并继续......对象没有按照有组织的顺序在数组中表示(例如: - [1. 2. 3.]或[2. 1. 3.] ......等等 - 它改变了位置)我需要如果检测到对象1则打印“ok”,并在检测到对象2和/或3时打印“notok”。对象1和/或3也检测到对象1应该打印“notok”。我试过了,

if (s_class==1):
    print("ok")
elif (s_class==2 or s_class==3):
    print("notok")
elif (s_class==1 and (s_class==2 or s_class==3)):
    print("notok")
elif (s_class==1 and s_class==2 and s_class==3 ):
    print("notok")

这不起作用。我怎样才能将这个numpy.ndarray与上述条件进行比较?

python numpy tensorflow numpy-ndarray object-detection-api
1个回答
0
投票

你可以用in比较。

x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)
x0 = x[0] #array([1, 2, 3])

#True
1 in x0 and 2 in x0

#True
1 in x0 or 2 in x0

#False
1 in x0 and 4 in x0
© www.soinside.com 2019 - 2024. All rights reserved.