'How do i work on venv remotely through Visual Studio Code on Raspberry Pi

I'm trying very hard to get debugging to work remotely in VS Code. I'm working on a project that's meant to recognize dominant color of the picture from camera and send data to control RGB LED strip. The problem is i can't start debugging. I installed openCv following this tutorial: https://www.pyimagesearch.com/2018/09/26/install-opencv-4-on-your-raspberry-pi/ The opencv package i installed is linked to the venv "cv" that i created. Later on i installed pyserial package inside of this venv.

Anyways i can run the code remotely through command window and ssh. I just activate the envoirment through commands: "cd ~/.virtualenvs/; source cv/bin/activate" and then i can run it. It works perfectly fine but i want to have acces to debugger to make some tweaks in the code and make final version of it. When i try to run it or debug it through Visual studio code it doesn't work couse it can't import opencv package. My guess is it doesn't work inside virtualenv. I also can't run the code from raspberry locally. I only know how to activate virtual envoirnment through ssh.

I tried modyfing launch.json file in Visual Studio to put a bash command in it before it runs. It doesn't work either. Raspberry Pi and python is totally new for me. I have some experience with embedded C and STM32 but this is totally different and frustrating that i can't run my code in any other way then through ssh and i can't run debugger. Here's how i tried it:

{
    // Użyj funkcji IntelliSense, aby uzyskać informacje o możliwych atrybutach.
    // Najedź kursorem, aby wyświetlić opisy istniejących atrybutów.
    // Aby uzyskać więcej informacji, odwiedź stronę: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        
        {
            
            "command":"cd; source .virtualenvs/cv/bin/activate",
            "name": "Python: venv_cv",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ],
    
}

Obviously it didn't work. I think making Visual studio to send this command before running debugger should work but most probably i just don't know how to do this.

And the code I'm trying to debug looks like this:

import cv2 as cv
import numpy as np
import serial
import picamera
import time

for i in range(10):
    with picamera.PiCamera() as camera:
        camera.resolution = (1280,720)
        camera.start_preview()
        time.sleep(0.1)
        camera.capture('testowe.jpg')
        camera.stop_preview()

    img = cv.imread('testowe.jpg')
    Z = img.reshape((-1,3))
    # convert to np.float32
    Z = np.float32(Z)
    # define criteria, number of clusters(K) and apply kmeans()
    criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 5, 1.0)
    K = 8
    ret,label,center=  cv.kmeans(Z,K,None,criteria,1,cv.KMEANS_RANDOM_CENTERS)
    # Now convert back into uint8, and make original image
    center = np.uint8(center)
    #res = center[label.flatten()]
    #res2 = res.reshape((img.shape))
    #cv.imshow('res2',res2)
    #cv.waitKey(0)
   # cv.destroyAllWindows()
    print("\r\nCentroidy", center[2,2])
    print("\r\nCentroidy", center[2])


    ser = serial.Serial('/dev/rfcomm0')  # open serial port
    ser.write(b'R000G000B000\r\n')
    ser.close()  

As i mentioned it does work, but the problem is i can't run the debugger inside the virtual envoirnment and i can't run it in any other way then through ssh. When i try to debug it the error is "Import "cv2" could not be resolved". I would be really grateful if someone showed me a way how to make it debug inside the virtual env. It's my first project that I'm using Raspberry Pi and python, and especially venv so i hope you can explain it in a simple manner. Not being able to access the debugger and to run the code easily is frustrating. Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source