Skip to content

Commit e490327

Browse files
First commit on AVIS Engine
0 parents  commit e490327

File tree

4 files changed

+396
-0
lines changed

4 files changed

+396
-0
lines changed

AVISEngine.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# @ 2020, Copyright Amirmohammad Zarif
2+
# AVIS Engine
3+
import cv2
4+
import os
5+
import io
6+
import re
7+
import time
8+
import math
9+
import base64
10+
import socket
11+
import numpy as np
12+
from PIL import Image
13+
from array import array
14+
15+
#Take in base64 string and return PIL image
16+
def stringToImage(base64_string):
17+
imgdata = base64.b64decode(base64_string)
18+
return Image.open(io.BytesIO(imgdata))
19+
20+
#convert PIL Image to an RGB image( technically a numpy array ) that's compatible with opencv
21+
def toRGB(image):
22+
return cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
23+
24+
class car():
25+
steering_value = 0
26+
speed_value = 0
27+
sensor_status = 1
28+
image_mode = 1
29+
get_Speed = 1
30+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
31+
data_arr = [speed_value,steering_value,image_mode,sensor_status,get_Speed]
32+
data_str = "Speed:" + str(data_arr[0]) + ",Steering:" + str(data_arr[1]) + ",ImageStatus:" + str(data_arr[2]) + ",SensorStatus:" + str(data_arr[3]) + ",GetSpeed:" + str(data_arr[4])
33+
image = None
34+
sensors = None
35+
current_speed = None
36+
def connect(self,server,port):
37+
try:
38+
self.sock.connect((server, port))
39+
self.sock.settimeout(5.0)
40+
print("connected to ", server, port)
41+
return True
42+
except:
43+
print("Failed to connect to ", server, port)
44+
return False
45+
def setSteering(self,steering):
46+
self.steering_value = steering
47+
self.image_mode = 0
48+
self.sensor_status = 0
49+
self.updateData()
50+
self.sock.sendall(self.data_str.encode("utf-8"))
51+
time.sleep(0.01)
52+
53+
def setSpeed(self,speed):
54+
self.speed_value = speed
55+
self.image_mode = 0
56+
self.sensor_status = 0
57+
self.updateData()
58+
self.sock.sendall(self.data_str.encode("utf-8"))
59+
time.sleep(0.01)
60+
61+
def move(self):
62+
self.updateData()
63+
self.sock.sendall(self.data_str.encode("utf-8"))
64+
65+
def getData(self):
66+
self.image_mode = 1
67+
self.sensor_status = 1
68+
self.updateData()
69+
self.sock.sendall(self.data_str.encode("utf-8"))
70+
recive = self.sock.recv(80000).decode("utf-8")
71+
imageTagCheck = re.search('<image>(.*?)<\/image>', recive)
72+
sensorTagCheck = re.search('<sensor>(.*?)<\/sensor>', recive)
73+
speedTagCheck = re.search('<speed>(.*?)<\/speed>', recive)
74+
75+
76+
try:
77+
if(imageTagCheck):
78+
imageData = imageTagCheck.group(1)
79+
im_bytes = base64.b64decode(imageData)
80+
im_arr = np.frombuffer(im_bytes, dtype=np.uint8) # im_arr is one-dim Numpy array
81+
imageOpenCV = cv2.imdecode(im_arr, flags=cv2.IMREAD_COLOR)
82+
self.image = imageOpenCV
83+
84+
if(sensorTagCheck):
85+
sensorData = sensorTagCheck.group(1)
86+
sensor_arr = re.findall("\d+", sensorData)
87+
sensor_int_arr = list(map(int, sensor_arr))
88+
self.sensors = sensor_int_arr
89+
else:
90+
self.sensors = [1500,1500,1500]
91+
if(speedTagCheck):
92+
current_sp = speedTagCheck.group(1)
93+
self.current_speed = int(current_sp)
94+
else:
95+
self.current_speed = 0
96+
97+
98+
except:
99+
pass
100+
#print("Unvalid Recive!")
101+
102+
103+
def getImage(self):
104+
return self.image
105+
106+
def getSensors(self):
107+
return self.sensors
108+
109+
def getSpeed(self):
110+
return self.current_speed
111+
112+
def updateData(self):
113+
data = [self.speed_value,self.steering_value,self.image_mode,self.sensor_status,self.get_Speed]
114+
self.data_str = "Speed:" + str(data[0]) + ",Steering:" + str(data[1]) + ",ImageStatus:" + str(data[2]) + ",SensorStatus:" + str(data[3]) + ",GetSpeed:" + str(data[4])
115+
116+
def stop(self):
117+
self.setSpeed(0)
118+
self.setSteering(0)
119+
self.sock.sendall("stop".encode("utf-8"))
120+
self.sock.close()
121+
print("done")
122+
123+
def __del__(self):
124+
self.stop()
125+
126+
127+

Example.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# @ 2020, Copyright Amirmohammad Zarif
2+
# Compatible with firasimulator version 1.0.1 or higher
3+
import AVISEngine
4+
import time
5+
import cv2
6+
7+
#Calling the class
8+
car = AVISEngine.car()
9+
10+
#connecting to the server (Simulator)
11+
car.connect("127.0.0.1", 25001)
12+
13+
#Counter variable
14+
counter = 0
15+
16+
debug_mode = False
17+
#sleep for 2 second to make sure that client connected to the simulator
18+
time.sleep(3)
19+
try:
20+
while(True):
21+
#Counting the loops
22+
23+
counter = counter + 1
24+
25+
#Set the power of the engine the car to 20, Negative number for reverse move, Range [-100,100]
26+
car.setSpeed(20)
27+
28+
#Set the Steering of the car -10 degree from center
29+
car.setSteering(-10)
30+
31+
#Get the data. Need to call it every time getting image and sensor data
32+
car.getData()
33+
34+
#Start getting image and sensor data after 4 loops. for unclear some reason it's really important
35+
if(counter > 4):
36+
#returns a list with three items which the 1st one is Left sensor data, the 2nd one is the Middle Sensor data, and the 3rd is the Right one.
37+
sensors = car.getSensors()
38+
#EX) sensors[0] returns an int for left sensor data in cm
39+
40+
#returns an opencv image type array. if you use PIL you need to invert the color channels.
41+
image = car.getImage()
42+
43+
#returns an integer which is the real time car speed in KMH
44+
carSpeed = car.getSpeed()
45+
46+
#Don't print data for better performance
47+
if(debug_mode):
48+
print("Speed : ",carSpeed)
49+
#currently the angle between the sensors is 30 degree TODO : be able to change that from conf.py
50+
print("Left : " + str(sensors[0]) + " | " + "Left : " + str(sensors[1]) +" | " + "Left : " + str(sensors[2]))
51+
52+
#showing the opencv type image
53+
cv2.imshow('frames', image)
54+
#break the loop when q pressed
55+
if cv2.waitKey(10) == ord('q'):
56+
break
57+
time.sleep(0.001)
58+
#A brief sleep to make sure everything
59+
60+
finally:
61+
car.stop()
62+
63+
64+
65+
66+

README.md

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# AVIS Engine Python API
2+
A Python API Implemented for AVIS Engine(Autonomous Vehicles Intelligent simulation Software).
3+
4+
# Changelog (Simulator)
5+
### - 1.2.0
6+
- Added localization and multilingual support. (English, Persian, Russian, France, German, Chinese, Italian, Spanish, Japanese, Korean, Turkish)
7+
- Visual Improvements.
8+
- Performance Improvements.
9+
- Main Menu redesign.
10+
- Added "About this simulator".
11+
- Added "Terms of use".
12+
13+
### - 1.0.7
14+
- Improved Performance.
15+
- Added TopSpeed Slider.
16+
- Added "Right lane Only" Toggle.
17+
- Added "Visible Sensor detection lines" Toggle.
18+
- Added a Slider to set the angle between sensors.
19+
- UI/UX Improvements and Updates on Top Panel for better experience. (Headers in each section)
20+
- Improved lighting
21+
- Replaced the vehicle's 3d model with a low-poly version of it to Improve the performance.
22+
- New method of counting checkpoints and laps. No order-sensitive counting system in this new version.
23+
- New Skyboxes on each map.
24+
- New Terrain map on each map.
25+
- Updated Textures.
26+
27+
### - 1.0.6
28+
- Changed the Tags material contrast to make it easier to read (Urban).
29+
- Added a border to signs and tags (Urban).
30+
31+
### - 1.0.5
32+
- Fixed Raycast hit on the second checkpoint in "Race Track 1"
33+
- Added Camera Calibration Checkerboard.
34+
- Added an Urban Track.
35+
36+
# Installation
37+
## Simulator
38+
The Simulator is accessible from AVIS Engine Website.
39+
| Simulator |
40+
|---|
41+
|[Download Version 1.2.0](https://AVISEngine.com) |
42+
43+
## Packages
44+
### Python
45+
#### &emsp; I) Using git
46+
&emsp;&emsp;&emsp; Open up your terminal and clone the python repository using git.
47+
```bash
48+
git clone https://github.com/AvisEngine/AVIS-Engine-Python-API
49+
```
50+
Everything is ready to drive your simulated car.
51+
52+
## Requirements
53+
install requirements using the command below
54+
55+
pip install -r requirements.txt
56+
57+
## Simulator
58+
Actually the most important thing you have to install is the simulator. It's your car and you have to drive it.
59+
The Simulator is the *'Server'* part of the Communication.
60+
61+
# Drive!
62+
Open up the simulator and make sure it's running correctly as you run it.
63+
- 1. Choose a Track
64+
- 2. Click on "Open Info Panel"
65+
- 3. Type in the local ip you want the simulator to run on. (Default : 127.0.0.1)
66+
- 4. Type in the port you want the simulator to run on. (Default : 25001)
67+
- 5. Click on "Start Server"
68+
69+
![Image of Connection](http://avisengine.com/wp-content/uploads/2021/01/Screen-Shot-2021-01-25-at-1.01.41-AM.png)
70+
71+
### Python
72+
Go to the files you downloaded before. Open up Example.py
73+
74+
``` python
75+
# Compatible with AVIS Engine version 1.0.3 or higher
76+
import AVISEngine
77+
import time
78+
import cv2
79+
80+
#Calling the class
81+
car = AVISEngine.car()
82+
83+
#connecting to the server (Simulator)
84+
car.connect("127.0.0.1", 25001)
85+
86+
#Counter variable
87+
counter = 0
88+
89+
debug_mode = True
90+
#sleep for 2 second to make sure that client connected to the simulator
91+
time.sleep(2)
92+
try:
93+
while(True):
94+
#Counting the loops
95+
96+
counter = counter + 1
97+
98+
#Set the power of the engine of the car to 20, Negative number for reverse movement, Range [-100,100]
99+
car.setSpeed(50)
100+
101+
#Set the Steering of the car -10 degree from center
102+
car.setSteering(-10)
103+
104+
#Get the data. Need to call it every time getting image and sensor data
105+
car.getData()
106+
107+
#Start getting image and sensor data after 4 loops. for unclear some reason it's really important
108+
if(counter > 4):
109+
#returns a list with three items which the 1st one is Left sensor data, the 2nd one is the Middle Sensor data, and the 3rd is the Right one.
110+
sensors = car.getSensors()
111+
#EX) sensors[0] returns an int for left sensor data in cm
112+
113+
#returns an opencv image type array. if you use PIL you need to invert the color channels.
114+
image = car.getImage()
115+
116+
#returns an integer which is the real time car speed in KMH
117+
carSpeed = car.getSpeed()
118+
119+
#Don't print data for better performance
120+
if(debug_mode):
121+
print("Speed : ",carSpeed)
122+
#currently the angle between the sensors is 30 degree TODO : be able to change that from conf.py
123+
print("Left : " + str(sensors[0]) + " | " + "Left : " + str(sensors[1]) +" | " + "Left : " + str(sensors[2]))
124+
125+
#Showing the opencv type image
126+
cv2.imshow('frames', image)
127+
#break the loop when q pressed
128+
if cv2.waitKey(10) == ord('q'):
129+
break
130+
time.sleep(0.001)
131+
#A brief sleep to make sure everything
132+
133+
finally:
134+
car.stop()
135+
```
136+
137+
#### Getting Started
138+
Import the AVISEngine Class
139+
``` python
140+
import AVISEngine
141+
```
142+
143+
Call the class
144+
``` python
145+
car = AVISEngine.car()
146+
```
147+
148+
149+
Replace the server "Server IP" and "port" with yours here. and Now you're connected to the Simulator.
150+
151+
``` python
152+
car.connect("127.0.0.1", 25001)
153+
154+
```
155+
156+
#### Controlling the Car
157+
Set the thottle and steering of the car by adding
158+
>1. Speed value Range : [-100,100] - "0 turns off the engine"
159+
>2. Steering value Range : [-100,100] - "0 is Center"
160+
161+
``` python
162+
#Set the speed of the car
163+
car.setSpeed(50)
164+
165+
#Set the steering of the car
166+
car.setSteering(0)
167+
```
168+
169+
#### Getting the Data
170+
You Can get access to the data such as "Sensors", "Current Speed" and "Image" by adding these lines.
171+
>1. car.getSensors() : returns a list with three items which the 1st one is Left sensor data, the 2nd one is the Middle Sensor data, and the 3rd is the Right one. the values are in cm.
172+
>2. car.getImage() : returns an opencv image type array. if you are using PIL you'll need to invert the color channels.
173+
>3. car.getSpeed() : returns an integer which is the real time car speed in KMH
174+
175+
``` python
176+
sensors = car.getSensors()
177+
#EX) sensors[0] returns an int for left sensor data in cm
178+
179+
image = car.getImage()
180+
181+
carSpeed = car.getSpeed()
182+
```
183+
184+
warning "It's highly recommended to add your Algorithms to the Example.py Code."
185+
186+
# Calibrate your Camera (Version 1.0.5 or higher).
187+
You can calibrate your camera with a Simulated Checkerboard.
188+
189+
Camera Calibration Test 1 | Camera Calibration Test 2
190+
------------ | -------------
191+
![Image of Calibration](http://avisengine.com/wp-content/uploads/2021/01/Screen-Shot-2020-08-11-at-12.35.39-AM.png) | ![Image of Calibration2](http://avisengine.com/wp-content/uploads/2021/01/Screen-Shot-2020-08-11-at-12.35.52-AM.png)
192+
193+
194+
last update : January 25, 2021

requirements.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
numpy==1.18.3
2+
opencv-contrib-python==4.2.0.34
3+
opencv-python==4.2.0.34
4+
Pillow==7.1.2
5+
PySocks==1.7.1
6+
PyYAML==5.3.1
7+
regex==2020.4.4
8+
requests==2.22.0
9+

0 commit comments

Comments
 (0)