我目前正在从事图像处理项目。识别出三种类型的对象,标记为1,2和3,由's_class'表示。检测到的输出对象/ objetcs由s_class给出,它是一个numpy.ndarray。当我打印它时,它给出了类似的输出
print(s_class)
>>[2.] #second object is detected
[3. 2. 1.] #all three objects 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,我需要打印“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与上述条件进行比较?
解决办法:可以比较使用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








暂无数据