Get picture from ErgoJr camera with API (e.g for Snap!)

Hello,

I was trying out the API Rest and in particular, I tried accessing the frame from a camera sensor but I get a 500 error. I checked the logs in the robot’s web interface and it says that the CVImage is not serializable.

Through the web interface, using the notebooks, it is possible to see the frame… So I imagine the image is being transferred to my computer. Is there a way of getting it through the rest api?

Thanks in advance.

Ok, a bit more detailed.

I’m requesting the frame via the url:
http://:8080/sensor/camera/register/frame

and in the log back in the robot it says:

Traceback (most recent call last):
  File "/home/poppy/miniconda/bin/bottle.py", line 862, in _handle
    return route.call(**args)
  File "/home/poppy/miniconda/bin/bottle.py", line 1733, in wrapper
    json_response = dumps(rv)
  File "/home/poppy/miniconda/lib/python2.7/site-packages/pypot/server/httpserver.py", line 70, in 
    jd = lambda s: json.dumps(s, cls=MyJSONEncoder)
  File "/home/poppy/miniconda/lib/python2.7/json/__init__.py", line 250, in dumps
    sort_keys=sort_keys, **kw).encode(obj)
  File "/home/poppy/miniconda/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/home/poppy/miniconda/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/home/poppy/miniconda/lib/python2.7/site-packages/pypot/server/httpserver.py", line 31, in default
    return json.JSONEncoder.default(self, obj)
  File "/home/poppy/miniconda/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: 155 is not JSON serializable
500 GET /sensor/camera/register/frame (169.254.90.233) 25.03ms

hello @TDTron

Today, there is no fast and efficient way to receive the video signal.

Therefore, no entry in the API has been added.

However you can do it yourself!

Insert these few lines into the file poppy_src / pypot / server / snap.py:

At line 10, insert:
Import cv2

At line 362, insert:
@self.app.get('/frame/<x>/<y>') def frame(x,y): img=cv2.resize(rr.robot.camera.frame,(int(x),int(y))) msg = '|'.join(';'.join(','.join(map(str,px)) for px in l) for l in img) return msg

This will turn the frame into a pixel-array as text.

Use url:
http://robot_name.local:6969/frame/240/180

I join a small blok snap to exploit this url
snap.zip (13.6 KB)

However, we run at 0.1fps (ie 1 image printed every 10 seconds!)

More complex solutions are currently being studied …

1 Like

Oohh ok, thank you !