Python Forum
Picamera2 commands between modules
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Picamera2 commands between modules
#3
(May-01-2025, 07:39 PM)deanhystad Wrote: It looks like you have something you made using app inventor sending a string to a python program. The python program executes the command represented by that string. The most important detail here is that app inventor is sending a string, not a command.

You could use eval() to execute the command.
eval("glvars.cam.start_preview(True)")
That is easy, but potentially dangerous. eval will execute any string it is passed, including commands to delete all files on your computer or install malware from a website. Unless you know for sure that there is no way malicious commands can be sent to your program, you should pass on eval.

Thanks, as a starting point I tried eval(). In my main .py file there is the listening socket:
    recvdata = client_sock.recv(1024)
    stringdata = recvdata.decode('utf-8')
    print ("Received data:", stringdata)
    dataList = stringdata.split('#')
    target = dataList[0]
    cmd = dataList[1]
    myModules.cmdExec(target, client_sock, cmd)
in glvars.py:
from picamera2 import Picamera2, Preview

# Camera init
cam = Picamera2()
...
in myModules.py:
def cmdExec(*args):
    target = args[0]
    clientSock = args[1]
    cmd = args[2]
    if target == "0": # System commands
        try:
            res = subprocess.run([cmd], shell=True, check=True, capture_output=True)
        except FileNotFoundError as exc:
            print(f"FileNotFoundError: {exc}")
            clientSock.send(f"FileNotFoundError: {exc}\n")
        except subprocess.CalledProcessError as exc:
            print(f"CalledProcessError: {exc}")
            clientSock.send(f"CalledProcessError:\n{exc}\nScript killed\n")
        except subprocess.TimeoutExpired as exc:
            print(f"TimeoutExpired: {exc}")
        out = res.stdout.decode("utf-8")
        print('output of command:', out)
        clientSock.send(f"{out}\n")
    elif target == "1": # Camera commands
        camExec(clientSock, cmd)

def camExec(*args): # Camera commands
    clientSock = args[0]
    cmd = args[1]
    print(cmd)
    # eval("glvars.cam.start_preview(True)") # it works
    eval("glvars.cam.cmd")
From App Inventor I send a test text string: "1#start_preview(True)". The script exits with:
AttributeError: 'Picamera2' object has no attribute 'cmd'
Reply


Messages In This Thread
Picamera2 commands between modules - by GigiG - Apr-30-2025, 11:16 AM
RE: Picamera2 commands between modules - by GigiG - May-05-2025, 09:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Picamera2 add text to camera preview GigiG 0 127 May-26-2025, 11:46 AM
Last Post: GigiG
  how to get PID's of linux commands executed through python modules? Manikandan_PS 4 4,529 Mar-12-2020, 07:16 AM
Last Post: Manikandan_PS
  Modules issue, pip3 download modules only to pyhton3.5.2 not the latest 3.6.1 bmohanraj91 6 10,248 May-25-2017, 08:15 AM
Last Post: wavic
  import commands modules not working in python 3.6.0 bmohanraj91 2 19,777 May-01-2017, 10:59 AM
Last Post: bmohanraj91

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020