forked from vision-agh/apse_uav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencv.py
executable file
·73 lines (54 loc) · 2.23 KB
/
opencv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Make sure to have the add-on "ZMQ remote API" running in
# CoppeliaSim and have following scene loaded:
#
# scenes/messaging/synchronousImageTransmissionViaRemoteApi.ttt
#
# Do not launch simulation, but run this script
#
# All CoppeliaSim commands will run in blocking mode (block
# until a reply from CoppeliaSim is received). For a non-
# blocking example, see simpleTest-nonBlocking.py
import time
import numpy as np
import cv2
from zmqRemoteApi import RemoteAPIClient
print('Program started')
client = RemoteAPIClient()
sim = client.getObject('sim')
visionSensorHandle = sim.getObject('/Vision_sensor')
# passiveVisionSensorHandle = sim.getObject('/PassiveVisionSensor')
# When simulation is not running, ZMQ message handling could be a bit
# slow, since the idle loop runs at 8 Hz by default. So let's make
# sure that the idle loop runs at full speed for this program:
defaultIdleFps = sim.getInt32Param(sim.intparam_idle_fps)
sim.setInt32Param(sim.intparam_idle_fps, 0)
# Run a simulation in stepping mode:
client.setStepping(True)
sim.startSimulation()
while (t := sim.getSimulationTime()) < 3:
img, resX, resY = sim.getVisionSensorCharImage(visionSensorHandle)
img = np.frombuffer(img, dtype=np.uint8).reshape(resY, resX, 3)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#img = np.frombuffer(img, dtype=np.uint8)
img = img[:, :, np.newaxis]
img = np.tile(img, (1, 1, 3))
# img = img.ravel()
# img = img.tobytes()
# In CoppeliaSim images are left to right (x-axis), and bottom to top (y-axis)
# (consistent with the axes of vision sensors, pointing Z outwards, Y up)
# and color format is RGB triplets, whereas OpenCV uses BGR:
#img = cv2.flip(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), 0)
# #convert img back to coppelia format
# img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
# img = cv2.flip(img, 0)
# img = img.reshape(resY*resX*3)
# img = img.tobytes()
# sim.setVisionSensorCharImage(passiveVisionSensorHandle, img)
cv2.imshow('captured_img', img)
cv2.waitKey(1)
client.step() # triggers next simulation step
sim.stopSimulation()
# Restore the original idle loop frequency:
sim.setInt32Param(sim.intparam_idle_fps, defaultIdleFps)
cv2.destroyAllWindows()
print('Program ended')