【问题标题】:Can't assign set to variable python无法将设置分配给变量python
【发布时间】:2015-07-30 16:34:04
【问题描述】:

嗨,我想知道这是否可能,因为我试过了,但我的变量总是空的。在我的项目中,我通过树莓派上的 PiCamera 跟踪静态对象和激光指示器,并将它们的轮廓的质心分别计算为 (smallx,smally)(small2x,small2y)

我使用它们坐标之间的差异来查看指针是否应该向上、向下、向左或向右移动,以便与第一个静态对象相遇。之后,它会选择 1 到 4 之间的方向移动,因为我的方向控件在 x-y 轴上并不完美并且是倾斜的。

我留下了从这里查找的控件和轮廓,并缩短了我的总代码,这样你就不会遇到一大堆垃圾来整理。

编辑:根据我的理解,我不认为我可以在不发布几百行代码和我的小设备的情况下提供可运行的东西,但我会把它归结为相关的代码的确切部分。运行 Python 2.7.3,使用 opencv2.4.10

代码:

#import libraries like picamera and opencv
#set empty variables like:
up = down = left = right = set()
smallx = smally = small2x = small2y = 0
#etc etc

with picamera.PiCamera() as camera:
    with picamera.array.PiRGBArray(camera) as rawCapture:

    #Calibrate my controls with the camera. updates the up, down, left, and right sets.


with picamera.PiCamera() as camera:
    with picamera.array.PiRGBArray(camera) as rawCapture:

    # Take pictures, threshold them, find contours, append their arrays to list

    if len(cnts)>0:     #If any objects were identified
            contm = sorted(smalList, key=lambda tup: tup[1])
            smallest = cnts[smalList[0][0]]           #**Take smallest object(my static object)**

            smallM = cv2.moments(smallest)                      
            smallx = int(smallM['m10']/smallM['m00'])  #**Calculate xcoord**   
            smally = int(smallM['m01']/smallM['m00'])  #**Calculate ycoord**

            cv2.line(frame, (smallx,smally), (smallx,smally), 1, 8,0) #Draws centroid

            # print(len(cnts))

            if len(cnts)==2:        #If only 2 objects were identified
                smallester = cnts[smalList[1][0]]      #** Take pointer object **
                small2 = cv2.moments(smallester)                        
                small2x = int(small2['m10']/small2['m00']) #**Calculate xcoord**
                small2y = int(small2['m01']/small2['m00']) #**Calculate ycoord**

                x = small2x - smallx
                y = small2y - smally

                print x #These prints return a value
                print y


                if x < 0:                       #Difference = Pointer - Object
                    s1 = right

                if x >0:
                    s1 = left

                if y < 0:
                    s2 = down
                if y >0:
                    s2 = up
                print s1, s2                  #set([]),set([])
                print up,down,left,right      #set([1,2]),set([3,4]),set([1,4]),set([2,3])

                selecty = s1&s2               #set([])


    #Tell the pointer where to go

我什至应该使用集合吗?

【问题讨论】:

  • 如果 s1s2 始终仍然是 0,那么您的任何 if 条件都不会评估 true-y。根据您发布的内容,我们无法告诉您原因。但是当然可以分配一个集合,达到那些行
  • small2x,smallx = 5,3
  • set(1, 2)TypeError。你是说set([1, 2]),还是{1, 2}
  • 我检查了我的 if 条件,我收到了 0 和 -83 之类的值,所以他们应该评估,是的,我做了,让我编辑它
  • 我添加了更多,但我理解的不够多,无法提供可以运行的东西。我希望你能明白我在做什么,大部分其他代码都没有参与这部分,但也许它与范围或我的 python 版本有关?我很困惑,因为它看起来很适合我未经训练的眼睛

标签: python variables opencv set variable-assignment


【解决方案1】:

使用s1 = s2 = set() 代替= 0

至于您的第二个问题,可能有更好和更知名的方法来解决您的问题。例如,使用位逻辑:

right = 1
up = 2
left = 4
down = 8

select = 0


if (small2x - smallx) < 0:  
    select |= right
if (small2x - smallx) >0:
    select |= left

if (small2y - smally) < 0:
    select |= down
if (small2y - smally) >0:
    select |= up

print(select)
print("You chose %s%s%s%s" %("UP " if select & up else "",
                             "DOWN " if select & down else "", 
                             "LEFT " if select & left else "",
                             "RIGHT" if select & right else ""))


#Do things after

【讨论】:

  • |= 运算符像 += 吗?此外,在进行s1=s2=set() 编辑后,我最终得到的是空集而不是 0,所以它更接近一点,但不知何故仍然不合时宜。我也 gt 错误'set' object has no attribute 'intersect'。奇怪,也许我会坚持使用 & 代替。
  • 因为不是intersect,而是intersection。等于a = a | ba |= b 是按位or 运算符;在这种情况下,如果b 中的相应位为1,它将设置a 的位为1。例如51 | 80 变为115,因为0110011 | 1010000 = 1110011
猜你喜欢
  • 2018-11-21
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2019-01-22
  • 2020-11-21
  • 2018-03-14
  • 2021-02-02
  • 1970-01-01
相关资源
最近更新 更多