Import cv2 not executing through ssh

I have set up an ssh connection between tx1 and my laptop and I am trying to run an opencv code from my laptop that is in the ubuntu server (tx1). This is the code I have used:

from flask import Flask, render_template, request
import paramiko
import time

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/execute_opencv', methods=['POST'])
def execute_opencv():
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('<tx1-ip>', username='ubuntu', password='<*******>')
    stdin, stdout, stderr = ssh.exec_command('python /home/ubuntu/Desktop/<my-app>.py')
    #time.sleep(3)
    output = stdout.read()
    ssh.close()
    print("Output from SSH command:", output)
    return f"OpenCV code executed: {output}"

if __name__ == '__main__':
    app.run(debug=True)

Opencv code is in tx1 which is just a simple script that starts the video feed and saves the file in a folder in the ubuntu desktop. When I run the code in ubuntu it executes perfectly but when I try to run the code from my laptop through this flask app it doesn’t execute. It seems to execute every line before import cv2 (I added print statements to see if it shows an output on the webpage of my laptop and it prints all the statement before import cv2). How do I solve this?

I wrote a simple python script on the tx1 to print the current working directory and that seems to execute perfectly. It is only that import cv2 is not working.

I am very new at this so please any kind of help is appreciated!!

crosspost:

there is no import cv2 in the code you presented.

MRE required. remove everything from the script that isn’t OpenCV. then check again.

Hello! Thank you for your response! My opencv code is on the ubuntu server of the tx1. Only the flask app is on my laptop. I was trying to execute the opencv script in the tx1 ubuntu server using this command - stdin, stdout, stderr = ssh.exec_command(‘python /home/ubuntu/Desktop/.py’)
For testing I wrote a simple two liner code in the tx1 ubuntu server like:
import os
print(os.getcwd())
and tried to execute this code from my laptop through this flask app and it generated an output. Then I wrote another python script in the tx1 to test like:
import cv2
print(“Executed”)
and it did not generate any output. So I was thinking that it is not somehow not importing the cv2 library but I don’t know why and what is happening. I am very new at this so please pardon me if I don’t make sense with my questions. I appreciate your help! :slight_smile:

this will not work in the real world.
there are no cameras attached to webservers

“tx1” means nvidia jetson.

there might be a camera.

still, this is no way to debug anything. all the flask and ssh nonsense needs to go for this to be debuggable.

module import should not “halt” or crash anything. I expect error messages at least, or high system load, or being out of RAM, or something.