Exemplo n.º 1
0
def test_bottom_lane_4():
    cube_detector = CubeDetector()
    frame = cv2.imread("../anki_object_detection/images/cube_up_lane_4.jpg")

    max_left_lane = Line(294, 181, 68, 415)
    max_right_lane = Line(490, 654, 61, 400)
    max_horizontal_upper_lane = Line(106, 700, 130, 116)
    max_horizontal_lower_lane = Line(10, 795, 247, 223)

    cube = cube_detector.detect(frame)
    horizontal_lane, vertical_lane = LaneCalculator.get_lane_for_cube(
        frame, cube, max_left_lane, max_right_lane, max_horizontal_upper_lane,
        max_horizontal_lower_lane)

    assert 4 == horizontal_lane
    assert -1 == vertical_lane
def test_bottom_lane_4():
    cube_detector = CubeDetector()
    frame = cv2.imread(
        "../anki_object_detection/images/camera_top_bottom_4.jpg")

    max_left_lane = Line(306, 312, 5, 486)
    max_right_lane = Line(484, 496, 2, 481)
    max_horizontal_upper_lane = Line(154, 638, 154, 143)
    max_horizontal_lower_lane = Line(157, 646, 337, 328)

    cube = cube_detector.detect(frame)
    horizontal_lane, vertical_lane = LaneCalculator.get_lane_for_cube(
        frame, cube, max_left_lane, max_right_lane, max_horizontal_upper_lane,
        max_horizontal_lower_lane)

    assert 4 == horizontal_lane
    assert -1 == vertical_lane
def test_bottom_lane_3():
    cube_detector = CubeDetector()
    frame = cv2.imread(
        "../anki_object_detection/images/camera_top_bottom_3.jpg")

    max_left_lane = Line(306, 312, 5, 486)
    max_right_lane = Line(484, 496, 2, 481)
    max_horizontal_upper_lane = Line(154, 638, 154, 143)
    max_horizontal_lower_lane = Line(157, 646, 337, 328)

    cube = cube_detector.detect(frame)
    horizontal_lane, vertical_lane = LaneCalculator.get_lane_for_cube(
        frame, cube, max_left_lane, max_right_lane, max_horizontal_upper_lane,
        max_horizontal_lower_lane)
    cv2.namedWindow('test', cv2.WINDOW_NORMAL)
    cv2.imshow('test', frame)
    cv2.waitKey(0)

    assert 3 == horizontal_lane
    assert -1 == vertical_lane
Exemplo n.º 4
0
    def run(self, max_left_lane, max_right_lane, max_horizontal_upper_lane,
            max_horizontal_lower_lane, lower_color_range, upper_color_range):
        try:
            print("Running")

            self.video_capture = cv2.VideoCapture(self.cameraDeviceId)
            self.video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1600)
            self.video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1200)

            cv2.waitKey(5000)

            cube_detector = CubeDetector()
            lane_detector = LaneDetector()

            last_horizontal_lane = -1
            last_vertical_lane = -1

            count_failed_frames = 0

            while (True):
                # Capture frame-by-frame
                ret, frame = self.video_capture.read()
                count_failed_frames += 1

                #frame = cv2.imread("anki_object_detection/images/cube_left_lane_1.jpg")
                keyInput = cv2.waitKey(1)
                if keyInput == ord('q'):
                    break
                elif keyInput == ord('w'):
                    lower_color_range[0] += 5
                    print("Lower hue set to ", lower_color_range[0])
                elif keyInput == ord('s'):
                    lower_color_range[0] -= 5
                    print("Lower hue set to ", lower_color_range[0])
                elif keyInput == ord('e'):
                    lower_color_range[1] += 5
                    print("Lower saturation set to ", lower_color_range[1])
                elif keyInput == ord('d'):
                    lower_color_range[1] -= 5
                    print("Lower saturation set to ", lower_color_range[1])
                elif keyInput == ord('r'):
                    lower_color_range[2] += 5
                    print("Lower luminance set to ", lower_color_range[2])
                elif keyInput == ord('f'):
                    lower_color_range[2] -= 5
                    print("Lower luminance set to ", lower_color_range[2])

                if ret:
                    cube = cube_detector.detect(frame, max_left_lane,
                                                max_right_lane,
                                                max_horizontal_upper_lane,
                                                max_horizontal_lower_lane,
                                                lower_color_range,
                                                upper_color_range)

                    if cube.x != 0 and cube.y != 0 and cube.width != 0 and cube.height != 0:
                        horizontal_lane, vertical_lane = LaneCalculator.get_lane_for_cube(
                            frame, cube, max_left_lane, max_right_lane,
                            max_horizontal_upper_lane,
                            max_horizontal_lower_lane)

                        if horizontal_lane != last_horizontal_lane:
                            #positionMessage = json.dumps(PositionUpdateMessage(-1, horizontal_lane).__dict__)
                            positionMessage = PositionUpdateMessage(
                                -1, horizontal_lane).toCsv()
                            print("INFO: Sending message " + positionMessage)
                            try:
                                print("INFO: Sending message " +
                                      positionMessage)
                                self.adasClient.send(positionMessage)
                            except Exception:
                                print("ERROR: Message could not be sent.")
                                self.start_adas_connection_timer()

                            last_horizontal_lane = horizontal_lane

                        if vertical_lane != last_vertical_lane:
                            #positionMessage = json.dumps(PositionUpdateMessage(-2, vertical_lane).__dict__)
                            positionMessage = PositionUpdateMessage(
                                -2, vertical_lane).toCsv()
                            try:
                                print("INFO: Sending message " +
                                      positionMessage)
                                self.adasClient.send(positionMessage)
                            except Exception:
                                self.start_adas_connection_timer()
                                print("ERROR: Message could not be sent.")

                            last_vertical_lane = vertical_lane
                    else:
                        try:
                            # No cube detected reset position
                            horizontal_lane = -1
                            vertical_lane = -1

                            if horizontal_lane != last_horizontal_lane:
                                resetMessage = PositionUpdateMessage(-1, -1)
                                resetMessage.msgTimestamp = datetime.min.isoformat(
                                    timespec='milliseconds') + "Z"
                                resetMessageCsv = resetMessage.toCsv()
                                print("INFO: Sending message " +
                                      resetMessageCsv)
                                self.adasClient.send(resetMessageCsv)

                            if vertical_lane != last_vertical_lane:
                                resetMessage = PositionUpdateMessage(-2, -1)
                                resetMessage.msgTimestamp = datetime.min.isoformat(
                                    timespec='milliseconds') + "Z"
                                resetMessageCsv = resetMessage.toCsv()
                                print("INFO: Sending message " +
                                      resetMessageCsv)
                                self.adasClient.send(resetMessageCsv)

                            last_horizontal_lane = horizontal_lane
                            last_vertical_lane = vertical_lane
                        except Exception:
                            self.start_adas_connection_timer()
                            print("ERROR: Message could not be sent.")
                            traceback.print_exc(file=sys.stdout)

                    # print configured lanes
                    cv2.line(frame, (max_left_lane.x1, max_left_lane.y1),
                             (max_left_lane.x2, max_left_lane.y2), (255, 0, 0),
                             5)
                    cv2.line(frame, (max_right_lane.x1, max_right_lane.y1),
                             (max_right_lane.x2, max_right_lane.y2),
                             (255, 0, 0), 5)
                    cv2.line(frame, (max_horizontal_upper_lane.x1,
                                     max_horizontal_upper_lane.y1),
                             (max_horizontal_upper_lane.x2,
                              max_horizontal_upper_lane.y2), (255, 0, 0), 5)
                    cv2.line(frame, (max_horizontal_lower_lane.x1,
                                     max_horizontal_lower_lane.y1),
                             (max_horizontal_lower_lane.x2,
                              max_horizontal_lower_lane.y2), (255, 0, 0), 5)

                    label = "Lane hor: " + str(
                        last_horizontal_lane) + ", lane vert: " + str(
                            last_vertical_lane)
                    cv2.putText(frame, label, (50, 50),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2,
                                cv2.LINE_AA)
                    print(label)

                    #Send hsv contrast image instead of real image
                    #frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HLS)
                    #frame = cv2.inRange(frame, lower_color_range, upper_color_range)

                    #colorRangeLabel = "h: " + str(lower_color_range[0]) + ", s " + str(lower_color_range[1]) + ", v " + str(lower_color_range[2])
                    #cv2.putText(frame, colorRangeLabel, (50, 1000), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (80, 255, 0), 2, cv2.LINE_AA)

                    #resize image
                    resized_image = cv2.resize(frame, (800, 600))
                    #write image to disk
                    cv2.imwrite("capture.jpg", resized_image)
                    with open("capture.jpg", "rb") as imageFile:
                        base64Image = base64.b64encode(imageFile.read())
                        try:
                            print("INFO: Sending image as base64")
                            self.twinClient.send(base64Image)
                        except Exception:
                            self.start_twin_connection_timer()
                            print("ERROR: Image could not be sent")

                    if settings.enable_debug_images:
                        cv2.namedWindow('test', cv2.WINDOW_NORMAL)
                        cv2.imshow('test', frame)
                    cv2.waitKey(10)
                else:
                    # If we could not initialize the camera after 100 frames, simply exit
                    count_failed_frames += 1
                    if count_failed_frames > 100:
                        print("ERROR: Could not init camera")
                        os.kill(os.getpid(), signal.SIGKILL)
                        sys.exit(0)
        except:
            print("Exception occurred")
            traceback.print_exc(file=sys.stdout)
        finally:
            # When everything done, release the capture
            self.video_capture.release()
            cv2.destroyAllWindows()

            self.terminate()