Raspberry Pi Python

I am a beginner trying to deploy my model on a RPi 3 using Python but I am completely lost. I have already built and tested my model - that was easily enough. Can I use the SDK to run using Python? Or is it easier to download the C++ file from the Deployment page and run that through Python? I’d love some hints as there aren’t any examples available yet.

Hi @hannasmith,

You can definitely use our Python SDK to run your model, you can read more about it in the documentation: https://docs.edgeimpulse.com/docs/linux-python-sdk.

I would also suggest to start with the Raspberry Pi tutorial if you haven’t done it yet, it is for RPi4 but it shoud work on your hardware too: https://docs.edgeimpulse.com/docs/raspberry-pi-4

Aurelien

Thanks for replying.

I have read the docs but I sill don’t understand how to actually use the example code. I can run impulse through my terminal, but how do I actually deploy it from Python? I tried to use the example here:
https://github.com/edgeimpulse/linux-sdk-python/blob/master/examples/image/classify.py but when I run it, I just get an error message because the sys.argv only has one argument.

You need to pass the model as an argument, here’s the format:

python classify.py <path_to_model.eim> <Camera port ID, only required when more than 1 camera is present>

1 Like

Hi Aurel,

I’m trying to execute the classisy.py example on my Nvidia Jetson Nano like you’ve described. However, I’m getting this error:
“Exception: Invalid number of features in ‘classify’, expected 102400 but got 102080”

I can’t really find a solution on how to solve this. Do you have any suggestions?

Much appreciated!

Hi @RawTeaBag,

Could you share the project ID related to this issue?
102400 should be the expected number of features for 320px square images, it looks like there is a row or column missing in the extracted features.

Thanks,
Aurelien

@aurel Thanks you for your quick reply! The project id is: 35630.

@RawTeaBag I am not able to reproduce but I suspect something in the way we resize the image. Can you provide the full output when you run the python classifier (python3 classify.py modelfile.eim)?

In particular I’m interested in the resolution detected by the library, ie:

Looking for a camera in port 0:
Camera AVFOUNDATION (720.0 x 1280.0) found in port 0 
Looking for a camera in port 1:

Also can you confirm it works using the CLI: edge-impulse-linux-runner?

Aurelien

@aurel I am using a virtual env on my Nvidia Jetson Nano which standard runs Python3.8.10 when I run the “python” command. When I run edge-impulse-linux-runner it runs locally without any problems. However, I want to integrate the model in my Python scripts (if you see this object, do this).

The error I am receiving when I run the command python classify.py modelfile.eim is:

(TimEdge) rawteabag@rawteabag-desktop:~/Desktop/EdgeImpulse/TimEdge/linux-sdk-python/examples/image$ python classify.py modelfile.eim
MODEL: /home/rawteabag/Desktop/EdgeImpulse/TimEdge/linux-sdk-python/examples/image/modelfile.eim
Loaded runner for "Tim Verlinde / PlantDetection"
Looking for a camera in port 0:
Camera V4L2 (2464.0 x 3264.0) found in port 0
Looking for a camera in port 1:
[ WARN:0] global /tmp/pip-install-7jdsioil/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video1): can't open camera by index
Looking for a camera in port 2:
[ WARN:0] global /tmp/pip-install-7jdsioil/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video2): can't open camera by index
Looking for a camera in port 3:
[ WARN:0] global /tmp/pip-install-7jdsioil/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video3): can't open camera by index
Looking for a camera in port 4:
[ WARN:0] global /tmp/pip-install-7jdsioil/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video4): can't open camera by index
Camera V4L2 (2464.0 x 3264.0) in port 0 selected.
Traceback (most recent call last):
  File "classify.py", line 123, in <module>
    main(sys.argv[1:])
  File "classify.py", line 94, in main
    for res, img in runner.classifier(videoCaptureDeviceId):
  File "/home/rawteabag/Desktop/EdgeImpulse/TimEdge/lib/python3.8/site-packages/edge_impulse_linux/image.py", line 93, in classifier
    res = self.classify(features)
  File "/home/rawteabag/Desktop/EdgeImpulse/TimEdge/lib/python3.8/site-packages/edge_impulse_linux/image.py", line 45, in classify
    return super(ImageImpulseRunner, self).classify(data)
  File "/home/rawteabag/Desktop/EdgeImpulse/TimEdge/lib/python3.8/site-packages/edge_impulse_linux/runner.py", line 60, in classify
    return self.send_msg(msg)
  File "/home/rawteabag/Desktop/EdgeImpulse/TimEdge/lib/python3.8/site-packages/edge_impulse_linux/runner.py", line 106, in send_msg
    raise Exception(resp["error"])
Exception: Invalid number of features in 'classify', expected 102400 but got 102080

Looks like it is an issue in the resize. To confirm could you please modify the following file in the Python SDK: edge_impulse_linux/image.py:

  • Add from math import ceil at the top of the file
  • Then change the following lines:
resize_size_w = ceil(largest_factor * in_frame_cols)
resize_size_h = ceil(largest_factor * in_frame_rows)

Let me know if that works,

Aurelien

@aurel I’ve implemented your suggested changes in edge_impulse_linux/image.py. However, I’m getting the same error message:

(TimEdge) rawteabag@rawteabag-desktop:~/Desktop/EdgeImpulse/TimEdge/linux-sdk-python/examples/image$ python classify.py modelfile.eim
MODEL: /home/rawteabag/Desktop/EdgeImpulse/TimEdge/linux-sdk-python/examples/image/modelfile.eim
Loaded runner for "Tim Verlinde / PlantDetection"
Looking for a camera in port 0:
Camera V4L2 (2464.0 x 3264.0) found in port 0
Looking for a camera in port 1:
[ WARN:0] global /tmp/pip-install-7jdsioil/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video1): can't open camera by index
Looking for a camera in port 2:
[ WARN:0] global /tmp/pip-install-7jdsioil/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video2): can't open camera by index
Looking for a camera in port 3:
[ WARN:0] global /tmp/pip-install-7jdsioil/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video3): can't open camera by index
Looking for a camera in port 4:
[ WARN:0] global /tmp/pip-install-7jdsioil/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video4): can't open camera by index
Camera V4L2 (2464.0 x 3264.0) in port 0 selected.
Traceback (most recent call last):
  File "classify.py", line 123, in <module>
    main(sys.argv[1:])
  File "classify.py", line 94, in main
    for res, img in runner.classifier(videoCaptureDeviceId):
  File "/home/rawteabag/Desktop/EdgeImpulse/TimEdge/lib/python3.8/site-packages/edge_impulse_linux/image.py", line 93, in classifier
    res = self.classify(features)
  File "/home/rawteabag/Desktop/EdgeImpulse/TimEdge/lib/python3.8/site-packages/edge_impulse_linux/image.py", line 45, in classify
    return super(ImageImpulseRunner, self).classify(data)
  File "/home/rawteabag/Desktop/EdgeImpulse/TimEdge/lib/python3.8/site-packages/edge_impulse_linux/runner.py", line 60, in classify
    return self.send_msg(msg)
  File "/home/rawteabag/Desktop/EdgeImpulse/TimEdge/lib/python3.8/site-packages/edge_impulse_linux/runner.py", line 106, in send_msg
    raise Exception(resp["error"])
Exception: Invalid number of features in 'classify', expected 102400 but got 102080

@RawTeaBag we’ve pushed v1.0.3 of the Python SDK which fixes this issue - thanks a lot for bringing it to our attention :slight_smile:

$ pip3 install edge-impulse-linux --upgrade

Should do the trick.

2 Likes

@janjongboom working like a charm! Thank you guys, much appreciated!! (Y)

Kind regards Tim Verlinde

@janjongboom Sorry, maybe I was cheering to soon. It is now able to run. However, it won’t detect the object that I had trained the model for. It does so when I run the command edge-impulse-linux-runner so I’m guessing it’s somewhere in the Python example script. Maybe some treshold that needs to be tweaked?

@RawTeaBag I tested this on a sample from your project. Are you classifying single image or on a video feed? Can you save the image that the classifier sees? You have img here:

https://github.com/edgeimpulse/linux-sdk-python/blob/master/examples/image/classify.py#L94

And you can save via:

cv2.imwrite('debug.jpg', img)

@janjongboom I’m trying to classify from video feed. I’ve followed up your instructions and captured an image. It is a plain green square. I also printed the output from print(img) which is a static [0 154 0]. I’m using the camera: 8MP Stereo Camera for Jetson Nano

debugJPG

(TimEdge) rawteabag@rawteabag-desktop:~/Desktop/EdgeImpulse/TimEdge/linux-sdk-python/examples/image$ python classify.py modelfile.eim
MODEL: /home/rawteabag/Desktop/EdgeImpulse/TimEdge/linux-sdk-python/examples/image/modelfile.eim
Loaded runner for "Tim Verlinde / PlantDetection"
Looking for a camera in port 0:
Camera V4L2 (2464.0 x 3264.0) found in port 0
Looking for a camera in port 1:
[ WARN:0] global /tmp/pip-install-7jdsioil/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video1): can't open camera by index
Looking for a camera in port 2:
[ WARN:0] global /tmp/pip-install-7jdsioil/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video2): can't open camera by index
Looking for a camera in port 3:
[ WARN:0] global /tmp/pip-install-7jdsioil/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video3): can't open camera by index
Looking for a camera in port 4:
[ WARN:0] global /tmp/pip-install-7jdsioil/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video4): can't open camera by index
Camera V4L2 (2464.0 x 3264.0) in port 0 selected.
{'result': {'bounding_boxes': []}, 'timing': {'anomaly': 0, 'classification': 226, 'dsp': 2, 'json': 13, 'stdin': 1905}}
[[[  0 154   0]
  [  0 154   0]
  [  0 154   0]
  ...
  [  0 154   0]
  [  0 154   0]
  [  0 154   0]]

 [[  0 154   0]
  [  0 154   0]
  [  0 154   0]
  ...
  [  0 154   0]
  [  0 154   0]
  [  0 154   0]]

 [[  0 154   0]
  [  0 154   0]
  [  0 154   0]
  ...
  [  0 154   0]
  [  0 154   0]
  [  0 154   0]]

@janjongboom I feel so stupid. I tought it was an incompatibility between the opencv version and ubuntu. However, it was as simple as that I did not selected the correct camera sensor in the EdgeImpulse setup. That’s why it was not collecting any img data and gave me a green square. Everything works perfectly now. Thanks for the support! :smiley:

2 Likes

please,I‘m really confused about where is the “classify.py” for my own project, i have already run my impluse in the terminal,but now,i need to make these output data be usbable in the python sdk,then i can use serial port to output these data.

Hi @guiyun_zxy,

classify.py is an example script that can be found as part of the demo repo here: linux-sdk-python/classify.py at master · edgeimpulse/linux-sdk-python · GitHub