无法将设置分配给变量 python



嗨,我想知道如果这是可能的,因为我尝试过,但我的变量总是空的。在我的项目中,我通过我的树莓派上的piccamera跟踪一个静态物体和一个激光笔,我计算了它们轮廓的质心分别为(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

我应该使用集合吗?

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

最新更新