Пример #1
0
    def process_image(self, obs):

        if not self.lane_detection:
            if self.vae_used:
                return obs
            obs = skimage.color.rgb2gray(obs)
            obs = skimage.transform.resize(obs, (img_rows, img_cols))
            return obs
        else:
            obs = cv2.cvtColor(obs, cv2.COLOR_BGR2GRAY)
            obs = cv2.resize(obs, (img_rows, img_cols))
            edges = my_cv.detect_edges(obs,
                                       low_threshold=50,
                                       high_threshold=150)

            rho = 0.8
            theta = np.pi / 180
            threshold = 25
            min_line_len = 5
            max_line_gap = 10

            hough_lines = my_cv.hough_lines(edges, rho, theta, threshold,
                                            min_line_len, max_line_gap)

            left_lines, right_lines = my_cv.separate_lines(hough_lines)

            filtered_right, filtered_left = [], []
            if len(left_lines):
                filtered_left = my_cv.reject_outliers(left_lines,
                                                      cutoff=(-30.0, -0.1),
                                                      lane='left')
            if len(right_lines):
                filtered_right = my_cv.reject_outliers(right_lines,
                                                       cutoff=(0.1, 30.0),
                                                       lane='right')

            lines = []
            if len(filtered_left) and len(filtered_right):
                lines = np.expand_dims(np.vstack(
                    (np.array(filtered_left), np.array(filtered_right))),
                                       axis=0).tolist()
            elif len(filtered_left):
                lines = np.expand_dims(np.expand_dims(np.array(filtered_left),
                                                      axis=0),
                                       axis=0).tolist()
            elif len(filtered_right):
                lines = np.expand_dims(np.expand_dims(np.array(filtered_right),
                                                      axis=0),
                                       axis=0).tolist()

            ret_img = np.zeros((80, 80))

            if len(lines):
                try:
                    my_cv.draw_lines(ret_img, lines, thickness=1)
                except:
                    pass

            return ret_img
    def process_image(self, obs, e, im_count):
        # when lane_detection is false
        if not agent.lane_detection:
            # resize image and grayscale
            obs = skimage.color.rgb2gray(obs)
            obs = skimage.transform.resize(obs, (img_rows, img_cols))

            # view resized output
            #cv2.imshow("obs", obs)
            #cv2.waitKey(0)

            # save output as a jpg
            #file_name = "/" + str(LANE_DETECT) + "_" + str(THROTTLE) + "_" + str(e+1) + "_" + str(im_count) + ".jpg"
            ##print(file_name)
            #cv2.imwrite(IM_PATH+file_name, obs*255)

            return obs

        # with lane detection
        else:
            obs = cv2.cvtColor(obs, cv2.COLOR_BGR2GRAY)
            obs = cv2.resize(obs, (img_rows, img_cols))
            #obs = cv2.GaussianBlur(obs, (15, 15), 0)

            #cv2.imshow("obs", obs)
            #cv2.waitKey(0)

            #file_name_a = "/" + str(LANE_DETECT) + "_" + str(THROTTLE) + "_" + str(e+1) + "_" + str(im_count) + "_a" + ".jpg"
            #print(file_name)
            #cv2.imwrite(IM_PATH+file_name_a, obs)

            low_threshold = 50
            high_threshold = 150
            edges = my_cv.detect_edges(obs, low_threshold=50, high_threshold=150)
            #cv2.imshow("edges", edges)
            #cv2.waitKey(0)

            rho = 0.8
            theta = np.pi/180
            threshold = 25
            min_line_len = 5
            max_line_gap = 10

            # perform original CV processing
            hough_lines = my_cv.hough_lines(edges, rho, theta, threshold, min_line_len, max_line_gap)
            
            left_lines, right_lines = my_cv.separate_lines(hough_lines)

            filtered_right, filtered_left = [],[]
            if len(left_lines):
                filtered_left = my_cv.reject_outliers(left_lines, cutoff=(-30.0, -0.1), lane='left')
            if len(right_lines):
                filtered_right = my_cv.reject_outliers(right_lines,  cutoff=(0.1, 30.0), lane='right')

            lines = []
            if len(filtered_left) and len(filtered_right):
                lines = np.expand_dims(np.vstack((np.array(filtered_left),np.array(filtered_right))),axis=0).tolist()
            elif len(filtered_left):
                lines = np.expand_dims(np.expand_dims(np.array(filtered_left),axis=0),axis=0).tolist()
            elif len(filtered_right):
                lines = np.expand_dims(np.expand_dims(np.array(filtered_right),axis=0),axis=0).tolist()

            ret_img = np.zeros((80,80))

            if len(lines):
                try:
                    my_cv.draw_lines(ret_img, lines, thickness=1)
                except:
                    pass

            #cv2.imshow("ret_img", ret_img)
            #cv2.waitKey(0)

            #file_name_b = "/" + str(LANE_DETECT) + "_" + str(THROTTLE) + "_" + str(e+1) + "_" + str(im_count) + "_b" + ".jpg"
            #print(file_name)
            #cv2.imwrite(IM_PATH+file_name_b, ret_img)

            return ret_img