首页 > 解决方案 > Python中的函数运行两次但只使用一次,在其他问题/答案中没有找到解决方案

问题描述

我在文件 color_shape_detection_1 中有一个名为 color_shape() 的函数,它检测使用 OpenCV 执行此操作的对象的颜色和形状。在同一个文件中,我还有其他功能可以拍照并检查对象是否位于图片中间的某个位置。在同一个文件夹中的另一个文件 control 中,我从 color_shape_detection_1 导入所有函数。但是如果在控制中使用函数 color_shape(),它会运行两次。该函数在 python shell 中执行时也会运行两次。我试过这个,还有这个已经在 stackoverflow 上找到了解决方案,但没有任何帮助。我刚刚上传了这个函数,因为文件很长,但如果你需要整个文件,我也可以上传。我还有一个旧文件,其中检测不在函数中,并且该文件只运行一次。

我希望有人可以帮助我并提前感谢

color_shape() 的代码:

def color_shape():
        
        #picture()
        position()
        while p < 1:
                position()
        
        print("Detecting...")
        cutting()

        font = cv2.FONT_HERSHEY_COMPLEX                 #Font bestimmen

        img = cv2.imread("/home/pi/Final/objekt_1.jpg")         #Bild für Formerkennung laden
        with Image.open("/home/pi/Final/objekt_1.jpg") as im:   #Bild für Farberkennung laden
                px = im.load()

        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)    #Bild in graustufen darstellen

        edged = cv2.Canny(gray, 30, 200, L2gradient = True)                #mit Canny Ecken finden

        contours, hierarchy = cv2.findContours(edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)   #Kontouren finden

        for cnt in contours:
                epsilon = 0.01*cv2.arcLength(cnt, True)
                approx = cv2.approxPolyDP(cnt, epsilon, True)   #Approximation der Formen

                M = cv2.moments(cnt)

                area = cv2.contourArea(cnt)

                if len(approx) == 3 and area > 200:            #Ecken der Konturen werden gezählt, 3 Ecken -> Dreieck
                    shape_name = "Triangle"
                    cv2.drawContours(img, [cnt], 0, (0, 255, 0), 2)     #Kontur ins Bild zeichnen
                    
                    print(area)
                    cx = int(M["m10"] / M["m00"])       #Mittelpunkt der Form finden, um Beschriftung
                    cy = int(M["m01"] / M["m00"])       #richtig zu positionieren
                    cv2.putText(img, shape_name, (cx-50, cy), font, 1, (0, 0, 0), 1)

                    color = px[cx, cy]                  #Farbe in der Mitte der Form entnehmen in RGB
                    print(color)

                    if all(x < y for x,y in zip(red_lower, color)) and all(x < y for x,y in zip(color, red_upper)):
                            cv2.putText(img, "red", (cx-50, cy+25), font, 1, (0, 0, 0), 1)
                            name = "a"
                    if all(x < y for x,y in zip(green_lower, color)) and all(x < y for x,y in zip(color, green_upper)):
                            cv2.putText(img, "green", (cx-50, cy+25), font, 1, (0, 0, 0), 1)
                            name = "b"
                    if all(x < y for x,y in zip(blue_lower, color)) and all(x < y for x,y in zip(color, blue_upper)):
                            cv2.putText(img, "blue", (cx-50, cy+25), font, 1, (0, 0, 0), 1)
                            name = "c"

                elif len(approx) == 4 and area > 200:          #4 Ecken -> Rechteck/Quadrat
                    shape_name = "Rec"
                    cv2.drawContours(img, [cnt], 0, (0, 255, 0), 2)     #Kontur ins Bild zeichnen
                    
                    
                    print(area)
                    cx = int(M["m10"] / M["m00"])       #Mittelpunkt der From finden, um Beschriftung
                    cy = int(M["m01"] / M["m00"])       #richtig zu positionieren
                    cv2.putText(img, shape_name, (cx-25, cy), font, 1, (0, 0, 0), 1)

                    color = px[cx, cy]          #Pixel in der mitte der Form einlesen
                    print(color)                #RGB des Pixels schreiben

                    if all(x < y for x,y in zip(red_lower, color)) and all(x < y for x,y in zip(color, red_upper)):
                            cv2.putText(img, "red", (cx-50, cy+25), font, 1, (0, 0, 0), 1)
                            name = "d"
                    if all(x < y for x,y in zip(green_lower, color)) and all(x < y for x,y in zip(color, green_upper)):
                            cv2.putText(img, "green", (cx-50, cy+25), font, 1, (0, 0, 0), 1)
                            name = "e"
                    if all(x < y for x,y in zip(blue_lower, color)) and all(x < y for x,y in zip(color, blue_upper)):
                            cv2.putText(img, "blue", (cx-50, cy+25), font, 1, (0, 0, 0), 1)
                            name = "f"
                                 
                elif 12 < len(approx) < 200 and area > 200:     #viele Ecken -> Kreis
                    shape_name = "Circle"
                    cv2.drawContours(img, [cnt], 0, (0, 255, 0), 2)     #Kontur ins Bild zeichnen
                    
                    print(area)
                    cx = int(M["m10"] / M["m00"])
                    cy = int(M["m01"] / M["m00"])
                    cv2.putText(img, shape_name, (cx-50, cy), font, 1, (0, 0, 0), 1)

                    color = px[cx, cy]
                    print("Color detected:", color)

                    if all(x < y for x,y in zip(red_lower, color)) and all(x < y for x,y in zip(color, red_upper)):
                            cv2.putText(img, "red", (cx-50, cy+25), font, 1, (0, 0, 0), 1)
                            name = "g"
                    if all(x < y for x,y in zip(green_lower, color)) and all(x < y for x,y in zip(color, green_upper)):
                            cv2.putText(img, "green", (cx-50, cy+25), font, 1, (0, 0, 0), 1)
                            name = "h"
                    if all(x < y for x,y in zip(blue_lower, color)) and all(x < y for x,y in zip(color, blue_upper)):
                            cv2.putText(img, "blue", (cx-50, cy+25), font, 1, (0, 0, 0), 1)
                            name = "i"

                #cv2.imshow("bw", edged)        
                cv2.imshow("shapes", img)       #Fenster mit den eingezeichneten Formen zeigen
                cv2.waitKey(10000)
                sleep(5)                        #nach ca. 10 Sekunden Fenster schließen
                cv2.destroyAllWindows()
                                
                b = bytes(name, "utf-8")
                print(name)                     #Ardu schreiben Objekt+Farbe
                print(shape_name)
                ser.write(b)


并在控制文件中:

import time
import serial
from color_shape_detection_1 import*


ser = serial.Serial(

    port="/dev/ttyAMA0",                #Port für die serielen Übertragung
    baudrate = 9600,
    parity = serial.PARITY_NONE,        #Seriele Übertragung festlegen
    stopbits = serial.STOPBITS_ONE,
    bytesize = serial.EIGHTBITS,
    timeout = 1
    )

try:
    
    while True:
        response = ser.readline()
        print(response)
        if response == b'Testing\r\n':
            print("Going active!")
            color_shape()
               

except KeyboardInterrupt:
    print("Bye")
    ser.close()

标签: python

解决方案


推荐阅读