diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 950fe50..0000000 Binary files a/.DS_Store and /dev/null differ diff --git a/.coveragerc b/.coveragerc index 278035e..101ffe2 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,5 +1,5 @@ [run] -source = src +source = avisengine omit = */site-packages/* tests/* diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index e5a517f..aae0960 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-22.04 strategy: matrix: - python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"] steps: - uses: actions/checkout@v4 @@ -22,13 +22,11 @@ jobs: - name: Install system dependencies run: | sudo apt-get update - sudo apt-get install -y libgl1-mesa-glx libglib2.0-0 libsm6 libxrender1 libxext6 + sudo apt-get install -y libgl1 libglib2.0-0 libsm6 libxrender1 libxext6 - name: Install dependencies run: | python -m pip install --upgrade pip - pip install pytest pytest-cov - pip install -r requirements.txt - pip install -e . + pip install -e ".[test]" - name: Run tests run: | python -m pytest tests/ -v --cov=avisengine --cov-config=.coveragerc diff --git a/README.md b/README.md index 47ed228..c5cac4d 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # AVIS Engine Python API -A Python API for AVIS Engine (Autonomous Vehicles Intelligent Simulation Software) - a robust simulation platform for autonomous vehicle development and testing. +A Python API for AVIS Engine (Autonomous Vehicles Intelligent Simulation Software), a robust simulation platform for autonomous vehicle development and testing. It drives both the AVIS Engine desktop simulator and the [AVIS Academy](https://academy.avisengine.com) web Sandbox, which runs in your browser with nothing to download. -[![Python Version](https://img.shields.io/badge/python-3.6+-blue.svg)](https://www.python.org/downloads/) +[![Python Version](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/) [![License](https://img.shields.io/badge/license-MIT-green.svg)](https://opensource.org/licenses/MIT) [![Simulator Version](https://img.shields.io/badge/simulator-v2.1.0--beta-orange.svg)](https://avisengine.com) @@ -21,25 +21,18 @@ AVIS Engine provides a realistic simulation environment for developing and testi ## Installation -### Requirements +#### Option 1: Using pip ```bash -pip install -r requirements.txt +pip install avisengine ``` -### Python API - -#### Option 1: Using git +#### Option 2: Using git ```bash git clone https://github.com/AvisEngine/AVIS-Engine-Python-API cd AVIS-Engine-Python-API -``` - -#### Option 2: Using pip (Coming soon) - -```bash -pip install avisengine +pip install -e . ``` ### Simulator @@ -119,12 +112,69 @@ finally: car.stop() ``` +## The web Sandbox (AVIS Academy) + +The [AVIS Academy](https://academy.avisengine.com) Sandbox is a simulator in your browser. This +library lets you drive it from Python on your own computer, with your own editor and libraries +(OpenCV, PyTorch, ROS). You can write the script in either of two ways. + +### setup() and loop(), like the Sandbox editor + +```python +# my_driver.py +from avisengine import car + +def setup(): + car.setSpeed(30) + +def loop(): + img = car.getImage() # numpy array (120, 160, 3), BGR like OpenCV + sensors = car.getSensors() # [left, middle, right] in cm + car.setSteering(0) +``` + +```bash +python -m avisengine my_driver.py +``` + +This is the same code as the Sandbox's own editor, with the same API (`getImage`, `getDepth`, +`getSemanticLabels`, `getIMU`, `getGNSS`, `setControl` and more). Each Run in the browser loads +the file again, so edits apply on the next Run. See `example_sandbox.py`. + +### A classic loop + +Existing scripts work by changing one line: + +```python +car = avisengine.Car() +car.connectSandbox() # instead of car.connect(ip, port) +while True: + car.setSpeed(20) + car.getData() # one simulation step + image = car.getImage() +``` + +### Connecting + +Both print a port and a six-digit pairing code. In the Sandbox, open **Laptop Python**, enter +them and press **Connect**, then **Run**. The simulator waits for your code at every control step +(each `loop()` call, or each `getData()` call), so results do not depend on how fast your +computer is. + +The connection stays on your computer: the script listens on 127.0.0.1 only, accepts only the +AVIS Academy page, and needs the pairing code. Graded practices always run in the browser. + +In the Sandbox, `setSpeed` and `setSteering` take percent (-100 to 100), `getSpeed()` on the +classic `Car` is km/h as with the desktop simulator, and `setSensorAngle` is set in the Sandbox's +Sensors window instead. + ## API Reference ### Car Class #### Connection Methods -- `connect(ip, port)` - Connect to the simulator +- `connect(ip, port)` - Connect to the desktop simulator +- `connectSandbox(port=8765)` - Wait for the web Sandbox to connect (prints a pairing code) - `getData()` - Retrieve the latest data from the simulator - `stop()` - Stop the car and close the connection @@ -148,6 +198,12 @@ The simulator includes a checkerboard pattern for camera calibration. Access thi ## Changelog +### Python API 0.2.0 +- Drive the AVIS Academy web Sandbox: `python -m avisengine my_driver.py` (setup/loop) or `car.connectSandbox()` (classic loop) +- Installable package with `pip install avisengine` +- Fewer dependencies (numpy, opencv-python, Pillow, websockets) +- Tests for the desktop protocol and the Sandbox link + ### 2.1.0 (Coming Soon) - ZMQ communication protocol - Configuration file support @@ -217,4 +273,4 @@ For questions, issues, or feature requests: --- -Last updated: May 19, 2025 +Last updated: November 26, 2026 diff --git a/avisengine.py b/avisengine.py deleted file mode 100644 index b1f7d45..0000000 --- a/avisengine.py +++ /dev/null @@ -1,220 +0,0 @@ -''' -@ 2025, Copyright AVIS Engine -''' - -import cv2 -import re -import base64 -import time -import socket -import numpy as np -import utils - -__author__ = "Amirmohammad Zarif" -__email__ = "amir@avisengine.com" - - -class Car(): - ''' - AVIS Engine Main Car class - - Attributes - ---------- - - Public: - steering_value - speed_value - sensor_status - image_mode - get_Speed - data_arr - data_str - sock - image - sensors - current_speed - sensor_angle - ''' - - #Attributes to kind of replicate a Pub-sub pattern messaging to request data - steering_value = 0 - speed_value = 0 - sensor_status = 1 - image_mode = 1 - get_Speed = 1 - sensor_angle = 30 - - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - - #Data format for request - data_arr = [speed_value, steering_value, image_mode, sensor_status, get_Speed, sensor_angle] - _data_format = "Speed:{},Steering:{},ImageStatus:{},SensorStatus:{},GetSpeed:{},SensorAngle:{}" - data_str = _data_format.format(data_arr[0], data_arr[1], data_arr[2], data_arr[3], data_arr[4], data_arr[5]) - - image = None - sensors = None - current_speed = None - - def connect(self,server,port): - ''' - Connecting to the simulator (server) - ''' - try: - self.sock.connect((server, port)) - self.sock.settimeout(5.0) - - print("connected to ", server, port) - return True - except: - print("Failed to connect to ", server, port) - return False - - - def recvall(self, socket): - ''' - Function to receive all the data chunks - ''' - BUFFER_SIZE = 131072 # Increased buffer size for better performance - data = bytearray() # Use a bytearray for better performance - - while True: - part = socket.recv(BUFFER_SIZE) - data.extend(part) - - # Use KMP search to find the , KMPSearch() returns -1 if the pattern was not found - # It is 9 times faster than the simple python search - if utils.KMPSearch(b"", data) > -1: # Convert "" to bytes - break - - return data.decode("utf-8") - - - def setSteering(self,steering): - ''' - Setting the steering of the car - - Parameters - ---------- - steering : int - Steering value in degree - ''' - self.steering_value = steering - self.image_mode = 0 - self.sensor_status = 0 - self.updateData() - self.sock.sendall(self.data_str.encode("utf-8")) - time.sleep(0.01) - - def setSpeed(self,speed): - ''' - Setting the speed of the car - - Parameters - ---------- - speed : int - ''' - self.speed_value = speed - self.image_mode = 0 - self.sensor_status = 0 - self.updateData() - self.sock.sendall(self.data_str.encode("utf-8")) - time.sleep(0.01) - - def setSensorAngle(self, angle): - ''' - Setting the angle between each sensor ray - - Parameters - ---------- - angle : int - In degrees - ''' - - self.image_mode = 0 - self.sensor_status = 0 - self.sensor_angle = angle - self.updateData() - self.sock.sendall(self.data_str.encode("utf-8")) - - def getData(self): - ''' - Requesting for the data from the simulator - ''' - self.image_mode = 1 - self.sensor_status = 1 - self.updateData() - self.sock.sendall(self.data_str.encode("utf-8")) - - receive = self.recvall(self.sock) - - imageTagCheck = re.search('(.*?)<\/image>', receive) - sensorTagCheck = re.search('(.*?)<\/sensor>', receive) - speedTagCheck = re.search('(.*?)<\/speed>', receive) - - try: - if(imageTagCheck): - imageData = imageTagCheck.group(1) - im_bytes = base64.b64decode(imageData) - im_arr = np.frombuffer(im_bytes, dtype=np.uint8) # im_arr is one-dim Numpy array - imageOpenCV = cv2.imdecode(im_arr, flags=cv2.IMREAD_COLOR) - self.image = imageOpenCV - - if(sensorTagCheck): - sensorData = sensorTagCheck.group(1) - sensor_arr = re.findall("\d+", sensorData) - sensor_int_arr = list(map(int, sensor_arr)) - self.sensors = sensor_int_arr - else: - self.sensors = [1500,1500,1500] - - if(speedTagCheck): - current_sp = speedTagCheck.group(1) - self.current_speed = int(current_sp) - else: - self.current_speed = 0 - except: - print("Failed to receive data") - - - def getImage(self): - ''' - Returns the image from the camera - ''' - return self.image - - def getSensors(self): - ''' - Returns the sensor data - A List: - [Left Sensor: int, Middle Sensor: int, Right Sensor: int] - ''' - return self.sensors - - def getSpeed(self): - ''' - Returns the speed of the car - ''' - return self.current_speed - - def updateData(self): - ''' - Updating the request data array and data string - ''' - data = [self.speed_value,self.steering_value,self.image_mode,self.sensor_status,self.get_Speed, self.sensor_angle] - self.data_str = self._data_format.format(data[0], data[1], data[2], data[3], data[4], data[5]) - - def stop(self): - ''' - Stoping the car and closing the socket - ''' - self.setSpeed(0) - self.setSteering(0) - self.sock.sendall("stop".encode("utf-8")) - self.sock.close() - print("Process stopped successfully!") - - def __del__(self): - self.stop() - - - diff --git a/avisengine/__init__.py b/avisengine/__init__.py new file mode 100644 index 0000000..6ac6790 --- /dev/null +++ b/avisengine/__init__.py @@ -0,0 +1,50 @@ +""" +AVIS Engine Python API. + +Two ways to drive: + +1. The AVIS Engine desktop simulator, or the web Sandbox, with a classic loop: + + import avisengine + car = avisengine.Car() + car.connect("127.0.0.1", 25001) # desktop simulator + # car.connectSandbox() # or the Sandbox in your browser + while True: + car.setSpeed(20) + car.getData() + image = car.getImage() + +2. The web Sandbox with the same setup() / loop() code as its editor + (python -m avisengine my_driver.py): + + from avisengine import car + + def setup(): + car.setSpeed(30) + + def loop(): + img = car.getImage() +""" + +from ._api import IMAGE_HEIGHT, IMAGE_WIDTH, SEMANTIC_CLASSES, car, imshow +from ._link import Stopped +from .avisengine import Car + +__version__ = "0.2.0" +__author__ = "Amirmohammad Zarif" +__email__ = "amirmohammadzarif@avisengine.com" + +__all__ = ["Car", "car", "imshow", "run", "SEMANTIC_CLASSES", "IMAGE_WIDTH", "IMAGE_HEIGHT", "Stopped"] + + +def run(port=None): + """Serve the script that called this to the web Sandbox (same as python -m avisengine