python pyparrot图像处理问题

2024-05-16 12:12:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试构建一个代码,它想要驾驶一架带有摄像头的无人机,下面是demoMamboVisionGUI.py。当代码被执行时,摄像机屏幕出现并按下按钮开始飞行。上述代码显示四个cam屏幕,并在检测指定颜色值蓝色(BGR2HSV)时检测直线。使用这两个代码,相机识别蓝色直线并一点一点向前飞行,以一定角度左右旋转,识别指定颜色(红色)的底部,着陆,然后用另一个按钮开始飞行。我想做一个识别绿色和陆地的代码。如果你能给我一个简单的提示,我将不胜感激

enter image description here

import cv2
import numpy as np

def im_trim(img):
    x = 160
    y = 50
    w = 280
    h = 180
    img_trim = img[y:y + h, x:x + w]
    return img_trim

def go():
    minimum = 9999;
    min_theta=0;
    try:
        cap = cv2.VideoCapture(0)
    except:
        return
    while True:
        ret, P = cap.read()

        img1 = P
        cv2.imshow('asdf',img1)
        img_HSV = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV)
        img_h, img_s, img_v = cv2.split(img_HSV)
        cv2.imshow("HSV", img_HSV)
        lower_b = np.array([100, 80, 100])
        upper_b = np.array([120, 255, 255])
        blue = cv2.inRange(img_HSV, lower_b, upper_b)
        cv2.imshow('root',blue)
        edges = cv2.Canny(blue, 50, 150, apertureSize =3)
        lines = cv2.HoughLines(edges, 1, np.pi/180, threshold = 100)
        if lines is not None:
             for line in lines:
                 r, theta = line[0]
                 #if (r<minimum and r>0) and (np.rad2deg(theta)>-90 and np.rad2deg(theta)<90):
                     #minimum = r
                     #min_theta = theta

                 #if (r > 0 and r < 250) and (np.rad2deg(theta) > 170 or np.rad2deg(theta) < 10):

                  #       self.drone_object.fly_direct(pitch=0, roll=-7, yaw=0, vertical_movement=0,
                   #                                   duration=1)
                         #print("right")



                 #elif (r > 400 and r < 650) and (np.rad2deg(theta) > 170 or np.rad2deg(theta) < 10):

                  #       self.drone_object.fly_direct(pitch=0, roll=7, yaw=0, vertical_movement=0,
                   #                                   duration=1)



                 print(r, np.rad2deg(theta))
                 #이하 if문을 while 문을 통해 반복하여 길 경로를 직진경로로 만든 이후 진행
                 #if(np.rad2deg(min_theta)>=몇도이상 or 이하):
                 # 이하 -> 왼쪽턴, 이상 -> 오른쪽턴, 사이 -> 직진
                 a = np.cos(theta)
                 b = np.sin(theta)
                 x0 = a * r
                 y0 = b * r
                 x1 = int(x0 + 1000 * (-b))
                 y1 = int(y0 + 1000 * a)
                 x2 = int(x0 - 1000 * (-b))
                 y2 = int(y0 - 1000 * a)
                 cv2.line(img1, (x1,y1), (x2,y2), (0,255,0), 3)

        cv2.imshow('hough',img1)
        k = cv2.waitKey(1)
        if k == 27:
            break
    cv2.destroyAllWindows()

if __name__ == "__main__":
    go()
    print("??")

=======================================================================================================================================

"""
Demo of the Bebop vision using DroneVisionGUI that relies on libVLC.  It is a different
multi-threaded approach than DroneVision

Author: Amy McGovern
"""
from pyparrot.Minidrone import Mambo
from pyparrot.DroneVisionGUI import DroneVisionGUI
import cv2


# set this to true if you want to fly for the demo
testFlying = True

class UserVision:
    def __init__(self, vision):
        self.index = 0
        self.vision = vision

    def save_pictures(self, args):
        # print("in save pictures on image %d " % self.index)

        img = self.vision.get_latest_valid_picture()

        if (img is not None):
            filename = "test_image_%06d.png" % self.index
            # uncomment this if you want to write out images every time you get a new one
            #cv2.imwrite(filename, img)
            self.index +=1
            #print(self.index)


def demo_mambo_user_vision_function(mamboVision, args):
    """
    Demo the user code to run with the run button for a mambo

    :param args:
    :return:
    """
    mambo = args[0]

    if (testFlying):
        print("taking off!")
        mambo.safe_takeoff(5)

        if (mambo.sensors.flying_state != "emergency"):
            print("flying state is %s" % mambo.sensors.flying_state)
            print("Flying direct: going up")
            mambo.fly_direct(roll=0, pitch=0, yaw=0, vertical_movement=15, duration=2)

            print("flip left")
            print("flying state is %s" % mambo.sensors.flying_state)
            success = mambo.flip(direction="left")
            print("mambo flip result %s" % success)
            mambo.smart_sleep(5)

        print("landing")
        print("flying state is %s" % mambo.sensors.flying_state)
        mambo.safe_land(5)
    else:
        print("Sleeeping for 15 seconds - move the mambo around")
        mambo.smart_sleep(15)

    # done doing vision demo
    print("Ending the sleep and vision")
    mamboVision.close_video()

    mambo.smart_sleep(5)

    print("disconnecting")
    mambo.disconnect()


if __name__ == "__main__":
    # you will need to change this to the address of YOUR mambo
    mamboAddr = "B0:FC:36:F4:37:F9"

    # make my mambo object
    # remember to set True/False for the wifi depending on if you are using the wifi or the BLE to connect
    mambo = Mambo(mamboAddr, use_wifi=True)
    print("trying to connect to mambo now")
    success = mambo.connect(num_retries=3)
    print("connected: %s" % success)

    if (success):
        # get the state information
        print("sleeping")
        mambo.smart_sleep(1)
        mambo.ask_for_state_update()
        mambo.smart_sleep(1)

        print("Preparing to open vision")
        mamboVision = DroneVisionGUI(mambo, is_bebop=False, buffer_size=200,
                                     user_code_to_run=demo_mambo_user_vision_function, user_args=(mambo, ))
        userVision = UserVision(mamboVision)
        mamboVision.set_user_callback_function(userVision.save_pictures, user_callback_args=None)
        mamboVision.open_video()

=================================================================================================================================


Tags: andthetoselfimgifisnp