Get image from vision sensor in Python

Typically: "How do I... ", "How can I... " questions
Post Reply
Anton
Posts: 1
Joined: 09 May 2018, 18:01

Get image from vision sensor in Python

Post by Anton » 13 May 2018, 11:28

Hi everyone,

I want to make a line-follower robot using a camera in V-REP. I'm able to move the robot arround using Python 3.6. Now I'm trying to get the image from the vision sensor in Python, but it gives me an error. This is the code I am using:

Code: Select all

import vrep
import numpy
import cv2
import time
import sys

vrep.simxFinish(-1)
clientID = vrep.simxStart('127.0.0.1',19999,True,True,5000,5) # Connect to V-REP
if clientID != -1:
    print ('Connected to remote API server')
else:
    print ('Failed connecting to remote API server')
    sys.exit()
    
errorCode, camera_handle = vrep.simxGetObjectHandle(clientID, 'cam1', vrep.simx_opmode_blocking)
print (errorCode)
errorCode, resolution, image = vrep.simxGetVisionSensorImage(clientID, camera_handle, 0, vrep.simx_opmode_streaming)
print (resolution)
print (image)
print (errorCode)
The first errorCode is 0, but the second one is 1 and resolution and image are both empty.
What am I doing wrong?

Thanks in advance

coppelia
Site Admin
Posts: 6864
Joined: 14 Dec 2012, 00:25

Re: Get image from vision sensor in Python

Post by coppelia » 15 May 2018, 08:25

Hello,

please carefully read about how to stream data via the remote API.

Here a working example:

Code: Select all

import vrep

print 'Program started'
vrep.simxFinish(-1) # just in case, close all opened connections
clientID=vrep.simxStart('127.0.0.1',19999,True,True,5000,5)
if clientID!=-1:
    print 'Connected to remote API server'
    res,v0=vrep.simxGetObjectHandle(clientID,'Vision_sensor',vrep.simx_opmode_oneshot_wait)
    res,v1=vrep.simxGetObjectHandle(clientID,'PassiveVision_sensor',vrep.simx_opmode_oneshot_wait)

    res,resolution,image=vrep.simxGetVisionSensorImage(clientID,v0,0,vrep.simx_opmode_streaming)
    while (vrep.simxGetConnectionId(clientID)!=-1):
        res,resolution,image=vrep.simxGetVisionSensorImage(clientID,v0,0,vrep.simx_opmode_buffer)
        if res==vrep.simx_return_ok:
            res=vrep.simxSetVisionSensorImage(clientID,v1,image,0,vrep.simx_opmode_oneshot)
    vrep.simxFinish(clientID)
else:
    print 'Failed connecting to remote API server'
print 'Program ended'
Cheers

Post Reply