开发者社区> 问答> 正文

python-3.x如何使用用户输入运行多种颜色识别?

我有一个学校项目,在该项目中,我必须制作一条跟车路线,必须根据用户输入遵循特定的颜色线。当我仅用一种颜色并且没有用户输入来测试汽车的路线跟随能力时,该代码就可以正常工作,但是当我添加多种颜色和用户输入时,汽车不再希望遵循一条路线而只是驶入日落。我试图尝试代码,但没有运气。这是代码的while循环。

try: while(True): user = int(input("1 for blue line")) time.sleep(1)

    ret, frame = cap.read()

    crop_img = frame[60:120, 0:160]

    _, img = cap.read()

    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

Tracking blue color

    if user == 1:
        blue_lower = np.array([99,115,150],np.uint8)
        blue_upper = np.array([110,255,255],np.uint8)
        blue = cv2.inRange(hsv, blue_lower, blue_upper)
        kernal = np.ones((5 ,5), "uint8")
        blue=cv2.dilate(blue,kernal)
        res=cv2.bitwise_and(img, img, mask = blue)

        (contours,hierarchy)=cv2.findContours(blue,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

        for pic, contour in enumerate(contours):
            area = cv2.contourArea(contour)
            if(area>300):

                    x,y,w,h = cv2.boundingRect(contour)
                    img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

        Controls()

Tracking black color

    else:
        black_lower = np.array([0,0,0],np.uint8)
        black_upper = np.array([50,50,100],np.uint8)
        black = cv2.inRange(hsv, black_lower, black_upper)
        kernal = np.ones((5 ,5), "uint8")
        black = cv2.dilate(black,kernal)
        res2 = cv2.bitwise_and(img, img, mask = black)

        (contours,hierarchy)=cv2.findContours(black,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

        for pic, contour in enumerate(contours):
            area = cv2.contourArea(contour)
            if(area>300):

                    x,y,w,h = cv2.boundingRect(contour)
                    img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

        Controls()

展开
收起
被纵养的懒猫 2019-09-30 14:57:32 408 0
0 条回答
写回答
取消 提交回答
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载