diff --git a/LoraViewer/main.py b/LoraViewer/main.py new file mode 100644 index 0000000..61feb03 --- /dev/null +++ b/LoraViewer/main.py @@ -0,0 +1,71 @@ +# img_viewer.py + +import PySimpleGUI as sg +import os.path + +# First the window layout in 2 columns + +file_list_column = [ + [ + sg.Text("Image Folder"), + sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"), + sg.FolderBrowse(), + ], + [ + sg.Listbox( + values=[], enable_events=True, size=(40, 20), key="-FILE LIST-" + ) + ], +] + +# For now will only show the name of the file that was chosen +image_viewer_column = [ + [sg.Text("Choose an image from list on left:")], + [sg.Text(size=(40, 1), key="-TOUT-")], + [sg.Image(key="-IMAGE-")], +] + +# ----- Full layout ----- +layout = [ + [ + sg.Column(file_list_column), + sg.VSeperator(), + sg.Column(image_viewer_column), + ] +] + +window = sg.Window("Image Viewer", layout) + +# Run the Event Loop +while True: + event, values = window.read() + if event == "Exit" or event == sg.WIN_CLOSED: + break + # Folder name was filled in, make a list of files in the folder + if event == "-FOLDER-": + folder = values["-FOLDER-"] + try: + # Get list of files in folder + file_list = os.listdir(folder) + except: + file_list = [] + + fnames = [ + f + for f in file_list + if os.path.isfile(os.path.join(folder, f)) + and f.lower().endswith((".png", ".gif")) + ] + window["-FILE LIST-"].update(fnames) + elif event == "-FILE LIST-": # A file was chosen from the listbox + try: + filename = os.path.join( + values["-FOLDER-"], values["-FILE LIST-"][0] + ) + window["-TOUT-"].update(filename) + window["-IMAGE-"].update(filename=filename) + + except: + pass + +window.close() \ No newline at end of file diff --git a/sim_control.py b/sim_control.py index f1775f1..e8cf567 100644 --- a/sim_control.py +++ b/sim_control.py @@ -1,5 +1,6 @@ import airsim import os +from utils.path_detect import read_image # connect to the AirSim simulator client = airsim.MultirotorClient() @@ -18,8 +19,8 @@ client.moveByVelocityAsync(0, 0, 0, 1).join() # take video from bottom camera and show using cv2 raw = client.simGetImage("bottom_center", airsim.ImageType.Scene) -airsim.write_file(os.path.normpath("./data/004.png"), raw) - +airsim.write_file(os.path.normpath("./data/temp.png"), raw) +read_image("./data/temp.png") client.landAsync().join() # lock diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/path_detect.py b/utils/path_detect.py index 9888010..406480f 100644 --- a/utils/path_detect.py +++ b/utils/path_detect.py @@ -4,6 +4,38 @@ import numpy as np # https://www.cnblogs.com/meikaiyuan/p/16805539.html +def mid_line_fit(contours_, mask, raw): + middle_s = [] + + for i in range(0, 143): + count = 0 + sum_ = 0 + for j in range(0, 255): + if contours_[1][j][0][1] == i: + sum_ += contours_[1][j][0][0] + count += 1 + middle = sum_ / count + print(count) + middle_s.append([[middle, i]]) + + contours_middle = np.array(middle_s).astype(np.int32) + + cv2.drawContours(mask, contours_middle, -1, (0, 255, 0), 2) + + output = cv2.fitLine(contours_middle, cv2.DIST_L2, 0, 0.01, 0.01) + k = output[1] / output[0] + b = output[3] - k * output[2] + x_0 = -b / k + x_480 = (479 - b) / k + cv2.line(mask, (int(x_0), 0), (int(x_480), 479), (255, 0, 0), 4) + cv2.line(raw, (int(x_0), 0), (int(x_480), 479), (255, 0, 0), 4) + + cv2.imshow("img_c", raw) + cv2.imshow("line", mask) + # cv2.imshow("mask", mask_c) + cv2.waitKey(0) + + def read_image(path): raw = cv2.imread(path) # 设定颜色HSV范围,假定为红色 @@ -25,13 +57,15 @@ def read_image(path): dilation = cv2.dilate(binary, kernel, iterations=1) # # img2 = cv2.bitwise_and(img, img, mask=mask) - cv2.imshow('image', dilation) - cv2.waitKey(0) - cv2.destroyAllWindows() + # cv2.imshow('image', dilation) + # cv2.waitKey(0) + # cv2.destroyAllWindows() # 获取图像轮廓坐标,其中contours为坐标值,此处只检测外形轮廓 contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) - cv2.approxPolyDP(contours[0], 0.01, True) + # cv2.approxPolyDP(contours[0], 0.01, True) + print(contours) + mid_line_fit(contours, mask, raw)