How do I run a python script with a video recall - python

I'm new to Python and I've a problem. I'm currently trying to run a python script in the console with a specific code:
python analysis.py --video <'C:/Users/name/folder/video.mp4'> which reports an error:
python analysis.py --video <'C:/Users/name/folder/video.mp4'>
^
SyntaxError: invalid syntax
but it was not working for me so I've found the code exec(open('analysis.py').read())
Actually it runs the .py script but I do not know how to let him select the specific video because the part "--video" doesn't work.
If I just run the analysis.py with the previous code, it opens my laptop camera, but the code should be used to upload a specific video (following the rest of the code).
I hope you can help me, thank you.
This is what's inside the analysis.py
import argparse
from utils.exercise_utils import Exercise
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-v', "--video", required=False, default=int(0),
help="Path to video source file", type=str)
parser.add_argument('-e', "--exercise", required=False, default="predict",
help="Type of exercise in video source",
type=str, choices=['predict', 'pushup', 'plank', 'squat', 'jumpingjack'])
args = parser.parse_args()
video = args.video
exercise = args.exercise
pose = Exercise(video, exercise)
pose.estimate_exercise()

Related

argparse args.accumulate isn't being recognised as a valid attribute in Python

Context:
I am attempting to make a Youtube command line video downloader as a small project for myself to understand python better. I am using argparse to get the args from the command line.
Issue: When I ran it the error came up AttributeError: 'Namespace' object has no attribute 'accumulate' How can I fix this?
Python Version: 3.10
OS: Windows 11
...
parser = argparse.ArgumentParser(description='Download a Youtube video')
parser.add_argument('video_url', type=str, help='Type a URL for a YouTube video.')
parser.add_argument('save_dir', type=str, help='Type the directory you want the YouTube video to be saved to be saves.')
args = parser.parse_args()
if __name__ == '__main__':
video_url = args.accumulate(args.video_url)
save_dir = args.accumulate(args.save_dir)
print(video_url, save_dir)
I am doing this because it said this in the docs https://docs.python.org/3.10/library/argparse.html

How to make python argcomplete run with PowerShell

I am trying to see if I can use argument autocompletion in Windows Powershell for my python script. Powershell allegedly supports argument completion.
Here's a minimal example which does not work:
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
import argparse
import argcomplete
from argcomplete.completers import EnvironCompleter
def argument_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Minimal app where arcomplete should work")
parser.add_argument("--version", action="store_true", help="print out version").completer = EnvironCompleter
parser.add_argument("--do-this", action="store_true", help="do this").completer = EnvironCompleter
parser.add_argument("--do-that", action="store_true", help="do that").completer = EnvironCompleter
return parser
if __name__ == "__main__":
parser = argument_parser()
argcomplete.autocomplete(parser)
cli_args = parser.parse_args()
Then in Powershell I try: to type python -i minimal - and then press <TAB>, Nothing happens. What am I doing wrong? Maybe I should mention that I did not enable global autocompletion. Somehow when I run activate-global-python-argcomplete in powershell, I get an "Open With ..." dialogue.
It is possible, you just need a wrapper script and mind the correct encoding
parser = argument_parser()
output_stream = None
if "_ARGCOMPLETE_POWERSHELL" in os.environ:
output_stream = codecs.getwriter("utf-8")(sys.stdout.buffer)
argcomplete.autocomplete(parser, output_stream=output_stream)
args = parser.parse_args()
I have composed a minimal working example. It's based on Tibor's mat example, but that one is actually not working because it is missing the utf-8 encoding of the buffer.

Cmd Does Not Prompt tThe Output of The Python File

I am trying to run a simple python document which takes --n or --name attribute to display a simple output.
My code is below
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-n", "--name", required=True,
help="name of the user")
args = vars(ap.parse_args())
print("Hi there {}, it's nice to meet you!".format(args["name"]))
When I run this python code with cmd (D:\User\Name> python filename.py --name Name) I don't get any output, while I aim to get the output Hi there Name, it's nice to meet you! What am I missing here?

Take input from user and execute the python script at the same time

i have a python script name as neural_net.py . It classify the mnist dataset.
What i want to do is to run it via command line by taking input from user.
The following snippet is taking input from user
file=input()
from PIL import Image
im = Image.open(file).convert('L')
imr=np.array(im).T
single_test = imr.reshape(1,400)
plt.figure(figsize=(5,5))
plt.imshow(imr)
print("value is",nn.predict(single_test))
in command prompt i have to run it as following
python neural_net.py
execute the above line and then give the input
pic_0.png
and it return me the output.
What i want is to do the both of above things as a single command such as
python neural_net.py pic_0.png
Use in code
import sys
file = sys.argv[1]
to get it.
Now you can run it as
python neural_net.py pic_0.png
and file will be pic_0.png
If you run with more arguments
python neural_net.py pic_0.png pic_1.png pic_2.png
then you will have sys.argv[2], sys.argv[3], etc. with values pic_1.png, pic_2.png
If you need more complex soluton like
script.py --input pic_0.png --output image.png
then see module argparse
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input')
parser.add_argument('-o', '--output', default='output.png')
args = parser.parse_args()
file = args.input
output_file = args.output
EDIT:
I think I realized why you are unhappy. You need to provide a path in addition to the file name, you can either do this with args like
python run.py "c:/users/user/desktop/pictures/pngs/file.png"
and use the original answers. Or simply just put a general path in the code and use the arg for the specific file.
IMAGE_FOLDER = "c:/users/user/desktop/pictures/pngs/"
file = IMAGE_FOLDER + sys.argv[1]
Original:
This is pretty much straight from google results:
CML:
python neural_net.py pic_0.png
Code:
import sys
print sys.argv[1]
Result:
"pic_0.png"
Many thanks for Google and
https://www.pythonforbeginners.com/system/python-sys-argv

argparse says "error: argument is required" even though script is called with argument

I'm running into trouble when trying to load images from this Python script.
It's running fine in Windows 10. I'm not sure why it's not working in my Windows 7 system.
from __future__ import print_function
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help = "Path to the image")
args = vars(ap.parse_args())
In CMD I run,
showimage.py --image syiling.png
then it returns
showimage.py: error: argument is required

Categories

Resources