Forum

Author Topic: How to change pixel size after cameras loading with script  (Read 3036 times)

SixenseLamier

  • Newbie
  • *
  • Posts: 7
    • View Profile
How to change pixel size after cameras loading with script
« on: July 03, 2019, 12:25:41 PM »
Hello,
I'm trying to automate my process.
I want to change the pixel_width and pixel_height in camera calibration before alignment.
The code following works without error but nothing change.

Code: [Select]
import Metashape

print(">>> Changement Taille_pixel <<<")

Metashape.Sensor.pixel_height=0.0043169
Metashape.Sensor.pixel_width=0.0043169

Some help please ?

Kind Regard,
Alex

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14851
    • View Profile
Re: How to change pixel size after cameras loading with script
« Reply #1 on: July 03, 2019, 01:51:08 PM »
Hello Alex,

You should access the particular sensor instance within the current chunk/project:

Code: [Select]
import Metashape
chunk = Metashape.app.document.chunk #active chunk
for sensor in chunk.sensors:
    sensor.pixel_size = Metashape.Vector([0.0043169, 0.0043169])
Best regards,
Alexey Pasumansky,
Agisoft LLC

SixenseLamier

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How to change pixel size after cameras loading with script
« Reply #2 on: July 03, 2019, 02:19:27 PM »
Perfect !
Thanks a lot !

cgb09c

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: How to change pixel size after cameras loading with script
« Reply #3 on: February 01, 2022, 01:11:35 AM »
This solution does not appear to work anymore, unless I have done something wrong? I am attempting to set the correct pixel size for my DJI cameras, I access the camera model info, and want to set the pixel size based on the model number. I have pasted my code below, before that point it is exactly as in the general_workflow.py. The code below does not return an error but the values remain unchanged.




a = chunk.sensors
print(a) #returns [< Sensor 'FC330 *3.61mm)'>]

for sensor in chunk.sensors:
    if camera_model == P4:
        sensor.pixel_size = Metashape.Vector([0.0015625, 0.0015625])
    elif camera_model == I2:
        sensor.pixel_size = Metashape.Vector([0.00328, 0.00328])
    else:
        print("pixel size set by agisoft")

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14851
    • View Profile
Re: How to change pixel size after cameras loading with script
« Reply #4 on: February 01, 2022, 12:40:39 PM »
Hello cgb09c,

What is camera_model in your script?
Best regards,
Alexey Pasumansky,
Agisoft LLC

cgb09c

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: How to change pixel size after cameras loading with script
« Reply #5 on: February 01, 2022, 05:02:38 PM »
Sorry, this is the code just above that.

camera = chunk.cameras[0]
camera_model = camera.photo.meta["Exif/Model"] #returns FC330
P4 = "FC330"
I2 = "FC6520"

cgb09c

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: How to change pixel size after cameras loading with script
« Reply #6 on: February 10, 2022, 09:46:07 PM »
Hi Alexey,

Do you have any advice on how to make this work?

Thanks,
Colby

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14851
    • View Profile
Re: How to change pixel size after cameras loading with script
« Reply #7 on: February 11, 2022, 08:36:17 PM »
Hello Coldy,

Can you please add some debugging lines to your script and check the Console pane output:

Code: [Select]
for sensor in chunk.sensors:
    print(camera_model)
    print(chunk.cameras[0].photo.meta["Exif/Model"]
    if camera_model == P4:
        sensor.pixel_size = Metashape.Vector([0.0015625, 0.0015625])
        print(sensor.pixel_size)
    elif camera_model == I2:
        sensor.pixel_size = Metashape.Vector([0.00328, 0.00328])
        print(sensor.pixel_size)
    else:
        print("pixel size set by agisoft")
Best regards,
Alexey Pasumansky,
Agisoft LLC

cgb09c

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: How to change pixel size after cameras loading with script
« Reply #8 on: February 15, 2022, 05:43:11 PM »
Hello,

Thanks for the reply! I added the print lines in there and it indeed changes the values but when I open the project file the values are not changed.  Here is the full output.

Platform: Windows
CPU: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz (server)
CPU family: 6 model: 79 signature: 406F1h
RAM: 127.9 GB
SaveProject: path = D:/Tree models/Models/test.psx
saved project in 0.009 sec
LoadProject: path = D:/Tree models/Models/test.psx
loaded project in 0.001 sec
AddPhotos
SaveProject: path = D:/Tree models/Models/test.psx
saved project in 0.02 sec
FC330
Vector([0.0015619248125310003, 0.0015619248125310003])
Vector([0.0015625, 0.0015625])
SaveProject: path = D:/Tree models/Models/test.psx
saved project in 0.001 sec

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14851
    • View Profile
Re: How to change pixel size after cameras loading with script
« Reply #9 on: February 15, 2022, 09:23:52 PM »
Hello Colby,

Is it possible that you are using doc.save(path) lines in your code?

If so, you should re-assign chunk variable after doc.save(path) lines, as it works similar to Save As operation - the project is saved, closed and re-opened.

Alternatively it is recommended to use doc.save() command instead, without using path argument to save the project under the same name.
Best regards,
Alexey Pasumansky,
Agisoft LLC

cgb09c

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: How to change pixel size after cameras loading with script
« Reply #10 on: February 17, 2022, 07:26:13 PM »
I have started with the general_workflow script. It initially uses

> doc.save(PATH)

to create the project, but after that it is

> chunk = doc.addChunk()
> chunk.addPhotos(photos)
> doc.save()

camera_model = chunk.cameras[0].photo.meta["Exif/Model"]

> for sensor in chunk.sensors...
...

> doc.save()

After it is run the first time, the project is created so I have also just tried running this without the doc.save(PATH)

> doc = Metashape.Document()
> doc.open(Path)
> chunk =doc. chunk
> print(chunk) # returns < Chunk 'Chunk 1'>
> print(chunk.sensors) #returns [Sensor 'FC330 (3.61mm)']>
> for sensor in chunk.sensors...
...

Again, the code sets the values( it prints the newly set values) but when I open the project in the gui, nothing is changed...

Interestingly, I have added a line in the for statement to enable the rolling shutter

> sensor.rolling_shutter = True

and it works. The pixel size is changed to the value set in the code and the rolling shutter is enabled when I open the project in the gui. Not sure what to make of that but it is working so thats cool, thanks again for the help!