Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two sets of code. One works fine in Python, but converting to Powershell CPAI returns a error 500 "AttributeError: 'NoneType' object has no attribute".

I am beating my head against the wall trying to figure how to get the Powershell version to work. Anyone have any feedback or examples they could share.

What I have tried:

Python
#Python Working
filename = f"C:\Temp\images\image_31c20dfc-5c63-4d48-bf0e-8d7e405a6743.jpg"
    image_data = open(filename, "rb").read()
    print("---------------------------")
    #print('passing image to CPAI module')
    response = requests.post("http://192.168.99.227:32168/v1/vision/detection",
                            files={"image": image_data}).json()
    response

#Powershell - Returns Error code 500 from server.
$filename = "C:\Temp\images\image_31c20dfc-5c63-4d48-bf0e-8d7e405a6743.jpg"
$image_data = [System.IO.File]::ReadAllBytes($filename)
$headers = @{
    "Content-Type" = "application/octet-stream"
}
$response = Invoke-RestMethod -Uri "http://192.168.99.227:32168/v1/vision/detection" -Method Post -Headers $headers -Body $image_data
$response
Posted
Updated 6-Oct-23 8:37am
v2
Comments
Richard MacCutchan 6-Oct-23 9:53am    
The error message looks to be incomplete, and you also need to explain which line it occurs on.
David Frank Oct2023 6-Oct-23 9:56am    
The full CPAI error:

08:55:44:Object Detection (YOLOv5 6.2): [AttributeError] : Traceback (most recent call last):
File "/app/preinstalled-modules/ObjectDetectionYolo/detect.py", line 141, in do_detection
det = detector(img, size=640)
File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1190, in _call_impl
return forward_call(*input, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
return func(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/yolov5/models/common.py", line 689, in forward
if im.shape[0] < 5: # image in CHW
AttributeError: 'NoneType' object has no attribute 'shape'
Richard MacCutchan 6-Oct-23 10:19am    
OK, so the line that cause the error is:
if im.shape[0] < 5: # image in CHW

which tells us that variable im has not been initialised, or it does not contain an array of elements.

But to find out why, requires debugging the code in the above package, which is not something anyone here can do. It may be a case of a missing or invalid parameter sent from the Invoke-RestMethod, so you will need to check the documentation for the application that processes the data that you are sending.

I don't do Python, but just looking at the code you posted, the Python code is sending a json formatted representation of the file content in the body of the POST, whereas your Powershell code is not doing that. It's sending the actual bytes of the file instead. The API is not expecting that. It's expecting the json formatted data.

How do you convert the "files={"image": image_data}).json" to Powershell? I don't know because I don't do Python and have no idea what exactly that line of code is doing.
 
Share this answer
 
Coding is problem solving. The journey is part of the frustrations and joy. Sometimes, like asking for help in forums like here in Quick Answers, helps get us there. I don't do Python too, so I turn to ChatGPT to shortcut converting languages. It's far from perfect however it gives a good starting point to work the problem to get to the solution.

Here is the question I asked ChatGPT:
convert this python to powershell:

filename = f"C:\Temp\images\image_31c20dfc-5c63-4d48-bf0e-8d7e405a6743.jpg"
image_data = open(filename, "rb").read()
print("---------------------------")
#print('passing image to CPAI module')
response = requests.post("http://192.168.99.227:32168/v1/vision/detection",
						files={"image": image_data}).json()
response

And here is the code that it generated:
PowerShell
# Set the filename
$filename = "C:\Temp\images\image_31c20dfc-5c63-4d48-bf0e-8d7e405a6743.jpg"

# Read the image data
$imageData = [System.IO.File]::ReadAllBytes($filename)

# Print a separator
Write-Host "---------------------------"

# Import the module for making HTTP requests (you might need to install it first)
# Install-Module -Name 'PSReadLine' -Force -AllowClobber
# Import-Module 'PSReadLine' -Force
# If you haven't already installed PSReadLine, you can use Invoke-RestMethod instead.
# Install-Module -Name 'PowerShellGet' -Force -AllowClobber
# Install-Module -Name 'PowerShellGet' -Force -AllowClobber
# If you haven't already installed PowerShellGet, you can use Invoke-RestMethod instead.

# Make the POST request and parse the JSON response
$response = Invoke-RestMethod -Uri "http://192.168.99.227:32168/v1/vision/detection" -Method Post -InFile $filename -ContentType "application/octet-stream"

# Print the response
$response

and the explanation:
Please note that this PowerShell code uses the Invoke-RestMethod cmdlet to make the HTTP POST request, which is available in PowerShell by default. You may need to adjust the content type and headers as per your specific requirements. Also, make sure you have the necessary modules and permissions to execute this code.
 
Share this answer
 
I had already attempted that, and the error is the same, using that code. I tried chatgpt, bing, and copilot. All produce similar code but same results.

At this point, I am just going to have powershell call the python code directly and redirect the output to a variable i can ingest.

11:05:14:Object Detection (YOLOv5 6.2):  [AttributeError] : Traceback (most recent call last):
  File "/app/preinstalled-modules/ObjectDetectionYolo/detect.py", line 141, in do_detection
    det                  = detector(img, size=640)
  File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1190, in _call_impl
    return forward_call(*input, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/yolov5/models/common.py", line 689, in forward
    if im.shape[0] < 5:  # image in CHW
AttributeError: 'NoneType' object has no attribute 'shape'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900