1
votes

I have written python code for performing OCR on an image file. The python code involves argparse and needs to be run via powershell. However, I am able to run this code on only 1 image at a time. I want to write a powershell loop to loop through images in a specific folder and run the python code. I am using Windows 10 and python 3. I never used powershell before.

I have tried using the Foreach-Object command over a folder with code as shown below:

Get-ChildItem "Path to the folder with pics"  -Filter *.jpg | Foreach-Object {
       $content = Get-Content $_.FullName
       python ocr3.py --image content
}

The ocr3.py file (with code $ python ocr3.py --image images\image.jpg) returns a word document with the recognised text. I was expecting that the above loop to return a bunch of word documents one for each of the images in the folder

1
I'm not sure you need Get-Content based on what you're saying (in any case you're missing a dollar at the start of 'content')? Get-ChildItem "C:\Temp\pics" -Filter *.jpg | Foreach-Object { python ocr3.py --image $_.FullName }Captain_Planet
Thanks Captain_Planet. That solved the issue. I also just realised about the missing $ when you mentioned. As I mentioned, I am a novice at powershell. I very much appreciate your helpVijay
No probs - I'll add it as an answer.Captain_Planet

1 Answers

0
votes

There is no requirement to use get-content if you just need to pass a filepath as an argument. Try the following instead:

Get-ChildItem "C:\Temp\pics" -Filter *.jpg | Foreach-Object { python ocr3.py --image $_.FullName }