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