Lost Newbie has Returned: ArUco Detection Edition

Hello again :grimacing:

I had some errors pop up in a code I was following for ArUco marker detection, I talked to my professor about it and he advised me to start new with the raspberry pi and follow another tutorial from PyImageSearch but I wanted a second opinion before doing so.

The code I’ve been following is this

and I’ve been getting a lot of ‘Attribute Errors’, I had managed to fix the following:
#aruco_type = “DICT_6x6_100”
#arucoDict = cv2.aruco.Dictionary_get(ARUCO_DICT[aruco_type])
#arucoParams = cv2.aruco.DetectorParameters_create()
fixed to:
arucoDict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_100)
arucoParams = cv2.aruco.DetectorParameters()
detector = cv2.aruco.ArucoDetector(arucoDict, arucoParams)

but now I’m getting and attribute error for
h, w, _ = img.shape
AttributeError: ‘Nonetype’ object has no attribute ‘shape’

How could I make this code work without having to start from zero with my raspberry pi (again :smiling_face_with_tear:) ?

If you query your error message you get quite a lot of hits. The consensus seems to be that you are querying an object that does not exist. This is because a number of functions (eg, imread()) return ‘none’ instead of an error if they fail to find an object. That is, the calling code is expected to check whether or not an object really exists before trying to use it. So the real problem is in the code that gets the img - you need to test that the image exists before trying to use it, and if it doesn’t exist either skip processing for that image, or trace back through the code and find out why it doesn’t exist.

2 Likes

Just adding some code to @Jeff105671 excellent answer

# I'd want to know what my capturing device is.
print(f"Capturing device is : {cap}")
# Now we check for a bad image.
ret, img = cap.read()
if img is none:
   print("I say, how rude!")
   exit()
2 Likes

So it seems like the reason that the codes aren’t working is because I was using commands that are not compatible with my camera, so the camera didn’t activate and hence the attribute error. Copilot said I need to import Libcamera BUT using Libcamera doesn’t seem to be working either, current error:
camera = lc.CameraManager(0)
^^^^^^^^^^^^^^^^^^^
TypeError: libcamera._libcamera.CameraManager: No constructor defined!

buuuut I have one more trick up my sleeve (importing picamera2 which has been successfully activating the camera) :grimacing: hopefully it’ll finally work this time.

I will not be defeated by this Raspberry Pi.

No constructor? That’ll do it. :stuck_out_tongue:
If your other solution doesn’t work you might be able to copy a constructor for another camera and only slightly tweak it to suit your hardware.

Go get 'em!

2 Likes

How can I do that??

It didn’t work :smiling_face_with_tear: Error given:
rawCapture = pc.array.PiRGBArray(camera, size=(640, 480))
^^^^^^^^
AttributeError: module ‘picamera2’ has no attribute ‘array’

1 Like

We could look into writing a constructor but before we do I just want to query that error you pasted.

rawCapture = pc.array.PiRGBArray(camera, size=(640, 480))
^^^^^^^^
AttributeError: module ‘picamera2’ has no attribute ‘array’

Instead of pc.array.PiRGBArray did you want capture_array()?/

from picamera2 import Picamera2
import numpy as np

picam2 = Picamera2()
picam2.configure(picam2.preview_configuration({"size": (640, 480)}))
# Returns a numpy array of float32
np_array = picam2.capture_array()
# Returns a raw array
raw_np_array = picam2.capture_array("raw")

I think picamera.array is no longer available in PiCamera2.

1 Like

I’m unsure if I integrated it correctly into the code. Should I also change “rawCapture.truncate(0)”?
Code here:
picam Mod.pdf (33.6 KB)

Also I noticed that I only have a folder that says picamera in my venv, I tried uninstalling picamera and it hasn’t let me. I tried installing picamera2 and it says I already have it :face_exhaling:

1 Like

I want to tackle this first, let’s make sure we either have picamera or picamera2. I imagine they are both currently installed on the machine.
I’m assuming you want to rock picamera2 since this is currently supported.
What have you tried so far uninstalling picamera? For example are you using pip to install your python modules? :slight_smile:

1 Like

I’ve used:

• sudo pip3 uninstall picamera

• sudo apt-get remove python-picamera python3-picamera

• pip3 list | grep picamera> sudo pip3 uninstall picamera
Using this command, i received an error: Error: externally-managed-environment

• sudo apt purge python3-picamera

The rest gives me:

Package python3-picamera is not installed so not removed

Package python-picamera is not installed so not removed

1 Like

checking installed packages

“[…] Is not installed so not removed” makes it seem like you have already succeeded.
Let’s run a test and figure out exactly what you’re supposed to have on your machine.

# pip list will return all of the pip packages you 
# currently have installed on your machine.
# We pipe that list into a command called grep. 
# grep searches that list of packages for a string
# that starts with "Picam" (* is wild card). 
pip list | grep "Picam*"

You’re looking for one module, hopefuly picamera2.

quick aside

(p.s. I apologize if I’m over simplifying all this to you, I just don’t know where you’re at so I’m assuming no prior knowledge).

testing picamera2 is working.

Thanks for pasting this through, looking good :+1:
Just before we jump into that can I suggest we run a super simple program that pulls out some raw data from your camera and prints it to the consol. It will be a nice “woohoo” moment and will give you some solid ground to stand on. :tada:

Below is my code from earlier.
I haven’t tested it because I don’t have the hardware, however, it’s code I’ve pulled out the docs for you so I expect it to work. Let’s try running it checking that the camera is detected, and gives us some yummy data. :bar_chart:

from picamera2 import Picamera2
import numpy as np

picam2 = Picamera2()
config = picam2.preview_configuration({"size": (640, 480)})
picam2.configure(config)

pycam2.start()
sweet_sweet_data = picam2.capture_array("raw")
print(sweet_sweet_data)

I predict we will get an error when calling the picamera2() constructor. However, hopefully we get a nice clear error message.

Hope this is all helping.
I know debugging can be super frustrating but I’m stoked your pushing through. :smiley:
Pix :heavy_heart_exclamation:

Don’t apologize, I appreciate it! I really do not know what I’m doing lol

drumroll pleaaase!
drumroll :drum:
config = picam2.preview_configuration({“size”: (640, 480)})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: ‘CameraConfiguration’ object is not callable

I had started the OS from zero and downloaded a whole bunch of stuff :grimacing: so I’m gonna start over again and run the code again to see if it changes anything

1 Like

Oh wait! Before you do, this was not the error I was expecting. We actually managed to call the constructor, we just gave it bad configurations. I googled the error and this came up.

Tim-Brown-NZ August 22
In the latest release all the xxx_configuration methods were renamed to to create_xxx_configuration.
So you need to call create_preview_configuration() as in the updated example here:
picamera2/examples/preview.py at main · raspberrypi/picamera2 · GitHub

Maybe I was supposed to use:

config = picam2.create_preview_configuration({"size": (640, 480)})

Wanna try that first?
How did you go listing out your installed packages using pip list and grep
Pix :heavy_heart_exclamation:

2 Likes

The sweet sweet data!
Sensor: /base/soc/i2c0mux/i2c@1/imx708@1a - Selected sensor format: 1536x864-SBGGR10_1X10 - Selected unicam format: 1536x864-pBAA
[[ 17 22 17 … 21 30 241]
[ 21 19 22 … 31 26 35]
[ 17 22 18 … 21 31 50]

[ 22 19 22 … 29 24 67]
[ 18 22 17 … 19 28 136]
[ 24 20 22 … 27 23 173]]

I’ve been trying different things in different MicroSD cards, I didn’t try the pip list | greg on the previous sd card I was about to erase, but I tried it one this new SD card and got no results :woman_shrugging:t4:

2 Likes

:tada: :partying_face: :confetti_ball:

Nice One!
Now that you have some working code, do you feel confident that you can integrate it into your project?


listing modules

Ah! That might be because grep is case sensitive.
Try this.

pip list | grep "picam" --ignore-case
1 Like

I thought I did but apparently not :smiling_face_with_tear:

Project_info.pdf (2.0 MB)
This is how I integrated it but I’m getting a Type error →

picam2.configure(picam2.preview_configuration({“size”: (640, 480)}))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: ‘CameraConfiguration’ object is not callable

I have a feeling that I’m still missing lines of code to make it complete? But I’m unsure, I’m still looking into it.
Copilot has suggested that I should modify the code to →
camera = Picamera2()
camera.configure(camera.preview_configuration({“size”: (640, 480)}))
np_array = camera.capture_array()
raw_np_array = camera.capture_array(“raw”)

but it doesn’t work either.

Hi @Caroline269834

picam2.configure(picam2.preview_configuration({“size”: (640, 480)}))

I think you’ve copied and pasted your way back into code purgatory :stuck_out_tongue:
It’s fine, it happens.

You want this command from our conversation 3 days ago.

Note that the picam2 syntactic sugar has been modified to create_preview_configuration()

Maybe you would find it helpful to talk about what the error is trying to say. :slight_smile:

When the python interpreter says “Type Error” it’s trying to tell you that the object, the “code thingy” that the function preview_configuration() is creating, has the wrong Type and is incompatible with picam2.
It would be compatible with picam1 but you don’t want picam1, you want picam2

create_preview_configuration() which I predict will return the right flavor of the CameraConfiguation object: the one compatible with the modern version of picam2.

Give that a shot :slight_smile:
Pix :heavy_heart_exclamation:

1 Like

:woman_facepalming:t4: No wonder, sorry about that! Thank you for being so patient with me.

I corrected it, it gave me no errors but nothing really happened? It made me save the file and that’s it. I’m at a lost for real now

1 Like

Yay! :partying_face:
Gotta celebrate every win.

That kinda makes sense … it is what you asked the computer to do.

# the code below saves the numpy array to disk.
raw_np_array = camera.capture_array(“raw”)

Have a look at chapter 6.1.4 of the picamera2 manual.

I can’t even remember what you were trying to do? :stuck_out_tongue:
You uploaded your code earlier.
Do you want to talk us through that?

Sooooo chill. :+1:
Code is hard. Gotta learn somehow. :slight_smile:

2 Likes

I forgot that this manual was supposed to be my bible!

Yeah! So I’ve been breaking down my project into smaller and more achievable goals and right now my goal is to get my camera to detect an ArUco marker, whether it be by a photo, video or, the ideal one to achieve, live video footage.

My end goal is to be able to detect an ArUco marker 4ft away and have some sort of feedback (it would be preferred if it was an LED) to indicate when the camera aligns perfectly (or as close to perfect) with the ArUco marker. There’s more components within the project, I have an Arduino set with a distance sensor that would need to communicate with the raspberry pi but I wanted to experiment to see if I could run both modules with the raspberry (I was told by one of my professors that it wouldn’t be possible but I saw a video by Core Electronics that did it!) and yeah, a drone is also involved :sweat_smile: there’s a lot going on

1 Like