Python changing command line argument with a script - python

I'm new to coding so this "obvious" action isn't really obvious to me. I've read on W3schools.com and other forums on the web about my problem but can't figure if I'm looking at the wrong solution or just can't do it.
I was able to run this python script from that site https://www.pyimagesearch.com/2018/11/12/yolo-object-detection-with-opencv/
It basically opens a command-line terminal which in you do insert argument and the script performs an object detection action.
What I want to do, is creating a script that automatically gets the image input path and then pastes it in the command line.
import os
imageFile = test1
os.system("python yolo.py --image {}.jpg --yolo yolo-coco").format(imageFile)
Furthermore, I'm asking myself how to get rid of the command line argument to paste arguments. Is there any way to accomplish this by directly putting the image's path in the base script?
Thanks in advance for the help.

Related

Running python file with arguments works in terminal, but I need to use it in a different script

Running the following works perfectly when I do it in my terminal
python3 number_detection.py --train_predict 'P' --file 'Images/2/_newsize_1.png'
However, I need to be able to perform this task in a different script. I'm looping through images and want to return what they're recognized as.
So running the above in the terminal works fine, but how do I translate that code to work in a different .py file?
Maybe there is a way to advise you without seeing your number_detection.py file, but it would definitely allow me to give you more concrete advice if you post that.
Right now I would say, most likely you can put everything in your number_detection.py file, and turn it into a number_detection.py function that takes in your arguments
def number_detection(train_predict, file):
#put your original code here
Then in your image processing file,
from number_detection import number_detection
and now you can use that other function in your new file

Can I make python read a pdf without giving the path but by having the file open on the computer instead?

I am very new to python and it seems that every tutorial mentions the need for a path when opening and reading a pdf in python.
The pdf that I want to read can be located in different folders and have different names and I would like to avoid relying on user input. Instead I was hoping that I could open the pdf in Acrobat Reader on the computer and code python to read the pdf file that is currently open. Is this possible?
One solution is to make use of the console arguments that can be accessed via the 'sys' module, the first argument is the path to the python executable, and the rest are arguments passed by the user like this:
python someScript.py arga argb
Then you can access them like this via the argv array:
import sys
for arg in sys.argv:
print(arg)
In this example it would print:
path/to/someScript.py
arga
argb
You could make it so you take the 1st user argument as path like this:
python someScript.py some/path/to/some/file.txt
And then you can get that path using
import sys
path = sys.argv[1]
If you dislike the user having to type the path in the terminal when launching the python script, or prefer launching it by double clicking the py file, you can just drag and drop the file you want the path of onto the python file and then it will work just fine.
I do realise that its not exactly what you asked, but finding an open file in some process on the pc is way harder and more unrealistic, if its even possible.
Hence i am proposing this alternative solution
It's possible but paths are so much easier, lol. checkout pyautogui. After you configure the script which navigates to the window you're looking for you're going to need to take a screenshot and use optical content recognition, checkout pytesseract

Opening a file using a specific program doesn't produce output

First of all, I am new to coding, and I have looked for similar questions and I know some commands which can be used in Python and in CMD to open a file using an executable. The problem I'm having is that when I try to use those commands, the program runs without any error, but it doesn't give the output file that should give. On the other hand, when I just double click the file, which is set to open with *.exe, it works and produces the output.
I tried the CMD command:
start "path of .exe" "filepath"
also just:
"path of .exe" "filepath"
Then I tried the os and subprocess modules in python:
subprocess.Popen(...)
os.system(...)
subprocess.run(...)
and many other solution I found on the internet. The point is that all of these solution don't give errors and should work, but they don't produce the wanted file for me. I used the same commands on another file with a different .exe and they work.
This is the step that is not working in my attempt to automate a whole process. If someone is willing to take a look at the files, you can download them from the following link:
https://gofile.io/?c=5TJtS9
The files are as follows:
1. running the rdam.grd file with hist_dam2d.exe produces the hist.plotps file
2. then running the hist.plotps with plotps.exe produces the wanted diagram
It should be an easy task that doesn't work for me.
For more information... This is part a random finite element software which is freely distributed by the authors. You can see the whole documentation and download all parts of the program from this link:
http://random.engmath.dal.ca/rfem/
The parts that are causing the problem are used just for extracting and showing results.
It is an old software so maybe there is some problem with that.
I don't know what exactly you did with your Python code since you didn't provide the exact code snippets. I also don't have the reputation to comment. So I'll just provide code examples for the 3 methods you listed (CMD, Powershell, Python). All three methods worked on my machine.
1) CMD
start "" "plotps.exe" hist.plotps
The double quotes after the start keyword are there to specify an optional title. What went wrong in your CMD example is that windows thought "plotps.exe" was the title. You don't need to specify a title, but you need to write the quotes. More info on this keyword can be found here: https://ss64.com/nt/start.html
Also note that start is asynchronous
A synchronous way of doing it would be:
plotps.exe hist.plotps
2) Powershell
Start-Process -FilePath "plotps.exe" -ArgumentList "hist.plotps"
I would strongly recommend using powershell over CMD if you have access to both.
This method is synchronous.
More info on Start-Process: https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Management/Start-Process?view=powershell-5.0
3) Python
I'm not a python expert but this worked for me:
import subprocess
subprocess.call(['plotps.exe', 'hist.plotps'])

Running python code from a text file into your python program

I Want to save my python code as a text file then make a python launcher that once you log in the program starts executing code from the text file. Is This possible to do and if so can someone please help
The request is a bit obscure but if I got it right you want create a python script that is able to launch other python scripts.
I do not understand the use of this but here's a possible solution, keeping in mind that your "text file" was saved as "yourscript.py":
from subprocess import call
call(["python", "yourscript.py"])
This code will act like if you'd run the saved "yourscript.py" from the command line.
Consider using runpy, e.g.:
import runpy
runpy.run_path("yourscript.py", init_globals={'global_name':some_value})
With optional init_globals parameter you can control global objects within the runtime of the script you are calling.

How to write the output of a python cgi script to user-specified file

I have a client/server arrangement which allows the user to submit a data file (picked via a filebrowser) and run the python cgi script. This script writes the output to a file with a default hardcoded filename which the user then moves to wherever they want.
I would prefer to let the user specify the filename themselves using the usual filebrowser dialogue box but cannot get this to work. I would greatly appreciate an example of how to make this work.
thanks

Categories