Forum

Author Topic: Exporting identified shape on images  (Read 3352 times)

armadillo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Exporting identified shape on images
« on: October 23, 2018, 01:40:02 PM »
Hello, I have made a reconstruction (sparse, dense, mesh, texture) of a generally flat surface (a map, but referenced in local coordinates, not georeferenced).

I then display an image and draw a polyline over a feature of interest. Finally, I can right click on the polyline and select "Filter Photos by Shapes" and I get all the images where the shape is present, along with the shape in the correct location. So far so good.

Now, my question, how can I export this? I would like the coordinates of the shape in each image's pixel coordinates, for example (if my polyline has four vertices):

image_A.jpg : x1_A, y1_A, x2_A, y2_A, x3_A, y3_A, x4_A, y4_A
image_B.jpg:  x1_B, y1_B, x2_B, y2_B, x3_B, y3_B, x4_B, y4_B
etc...

I tried it using the python console and I presume it is doable but not sure about the steps. If there is a way using the GUI, I am not aware of it. Any help?

Thanks.

fjgarciam

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Exporting identified shape on images
« Reply #1 on: April 26, 2020, 12:53:52 AM »
I have exactly the same question. Is there any way to know the local coordinates on a image of a shape. Or better export one shape for every proyection on a single image.
Hello, I have made a reconstruction (sparse, dense, mesh, texture) of a generally flat surface (a map, but referenced in local coordinates, not georeferenced).

I then display an image and draw a polyline over a feature of interest. Finally, I can right click on the polyline and select "Filter Photos by Shapes" and I get all the images where the shape is present, along with the shape in the correct location. So far so good.

Now, my question, how can I export this? I would like the coordinates of the shape in each image's pixel coordinates, for example (if my polyline has four vertices):

image_A.jpg : x1_A, y1_A, x2_A, y2_A, x3_A, y3_A, x4_A, y4_A
image_B.jpg:  x1_B, y1_B, x2_B, y2_B, x3_B, y3_B, x4_B, y4_B
etc...

I tried it using the python console and I presume it is doable but not sure about the steps. If there is a way using the GUI, I am not aware of it. Any help?

Thanks.

Paulo

  • Hero Member
  • *****
  • Posts: 1320
    • View Profile
Re: Exporting identified shape on images
« Reply #2 on: April 26, 2020, 05:12:50 AM »
Hi,

the following script would print out camera, vertex number, x and y image coordinates for each vertex of shape (30 vertices) appearing in 4 selected cameras:

Code: [Select]
chunk = Metashape.app.document.chunk
T = chunk.transform.matrix
crs = chunk.shapes.crs
cameras_sel = [camera for camera in chunk.cameras if camera.selected]
shapes = [shape for shape in chunk.shapes if shape.selected]
shape = shapes[0]
print("Image vertex coordinates for shape:",shape.key)
for camera in cameras_sel:
print(camera.label)
num_ver = 0
for vertex in shape.vertices:
num_ver += 1
x = camera.project(T.inv().mulp(crs.unproject(vertex))).x
y = camera.project(T.inv().mulp(crs.unproject(vertex))).y
if (x < 0 or x > camera.sensor.width or y < 0 or y > camera.sensor.height):
continue
print("\t",num_ver,"\t",x,"\t",y)

and the result would be something like:

2020-04-26 04:52:14 Image vertex coordinates for shape: 0
2020-04-26 04:52:14 DJI_0516.JPG
2020-04-26 04:52:14          4       5453.743946402111       222.74381861926574
2020-04-26 04:52:14          5       5436.709416745306       269.3403659912882
2020-04-26 04:52:14          6       5420.49620855976        292.47811552495216
2020-04-26 04:52:14          7       5343.148310226199       311.81425185146117
2020-04-26 04:52:14          8       5281.496274732476       274.0485517086035
2020-04-26 04:52:14          9       5270.234735955541       189.28171042920087
2020-04-26 04:52:14          10      5241.590308505169       122.02298426452973
2020-04-26 04:52:14          11      5201.906692295227       122.19297889122483
2020-04-26 04:52:14          12      5134.898857946424       141.99334040022097
2020-04-26 04:52:14          13      5085.438478906967       105.60425594436147
2020-04-26 04:52:14          14      5085.034369251849       60.84692039321978
2020-04-26 04:52:14          15      5144.0228449162105      7.838497359763436
2020-04-26 04:52:14 DJI_0517.JPG
2020-04-26 04:52:14          2       5386.638683515784       1181.5120947377204
2020-04-26 04:52:14          3       5316.6578823684085      1176.3952952565621
2020-04-26 04:52:14          4       5291.389854523749       1195.644797724307
2020-04-26 04:52:14          5       5274.8489701372855      1241.7422764771925
2020-04-26 04:52:14          6       5258.823552196129       1265.4900429203633
2020-04-26 04:52:14          7       5182.918003413208       1280.540624322772
2020-04-26 04:52:14          8       5121.94962021607        1240.240376996891
2020-04-26 04:52:14          9       5109.907318263296       1157.2579120653324
2020-04-26 04:52:14          10      5081.476673605759       1087.0560926021465
2020-04-26 04:52:14          11      5042.210226657976       1086.9887727657867
2020-04-26 04:52:14          12      4976.1554076993825      1105.9451833197536
2020-04-26 04:52:14          13      4926.888599478631       1069.8535108506933
2020-04-26 04:52:14          14      4926.479117567041       1022.7842815760089
2020-04-26 04:52:14          15      4984.401404256255       970.242157768817
2020-04-26 04:52:14          16      5058.266631630511       936.1891529594453
2020-04-26 04:52:14          17      5104.092274798773       903.7874938243431
2020-04-26 04:52:14          18      5123.049831937411       871.259837085535
2020-04-26 04:52:14          19      5093.296624271075       835.4618874524924
2020-04-26 04:52:14          20      5085.833099677106       767.605894219358
2020-04-26 04:52:14          21      5151.37059673191        726.863173731634
2020-04-26 04:52:14          22      5240.7172444995695      697.7833614369108
2020-04-26 04:52:14          23      5325.9402114134355      710.9612726560171
2020-04-26 04:52:14          24      5394.028082329082       720.9912679258749
2020-04-26 04:52:14          25      5462.1509446421305      773.942547067115
2020-04-26 04:52:14 DJI_0518.JPG
2020-04-26 04:52:14          1       5343.5776947674785      2097.906886807614
2020-04-26 04:52:14          2       5235.952070527379       2117.6328634795395
2020-04-26 04:52:14          3       5166.826489639096       2112.521306211412
2020-04-26 04:52:14          4       5141.825436950572       2133.378791659176
2020-04-26 04:52:14          5       5125.959024393331       2178.9680761094005
2020-04-26 04:52:14          6       5110.258178211332       2203.3325948553643
2020-04-26 04:52:14          7       5035.995667007567       2214.477027256982
2020-04-26 04:52:14          8       4975.763327543942       2171.9798459778885
2020-04-26 04:52:14          9       4962.920485269887       2090.815013955641
2020-04-26 04:52:14          10      4934.622911226999       2017.8855901177858
2020-04-26 04:52:14          11      4895.859996430065       2017.739698788529
2020-04-26 04:52:14          12      4830.860458763899       2036.1602161534292
2020-04-26 04:52:14          13      4781.8206187023825      2000.5801430320716
2020-04-26 04:52:14          14      4781.31721701037        1951.247066738112
2020-04-26 04:52:14          15      4838.052731064362       1898.9147027306808
2020-04-26 04:52:14          16      4910.608996923446       1865.6145305481923
2020-04-26 04:52:14          17      4955.187546475447       1836.1373866732526
2020-04-26 04:52:14          18      4973.141338118379       1807.1980489173955
2020-04-26 04:52:14          19      4944.610341243703       1762.8957417022202
2020-04-26 04:52:14          20      4936.796341311201       1693.990354191347
2020-04-26 04:52:14          21      5001.096731500073       1653.8010286018512
2020-04-26 04:52:14          22      5088.980299966174       1625.586705941213
2020-04-26 04:52:14          23      5173.0077100685185      1640.7926344903
2020-04-26 04:52:14          24      5240.22135288281        1651.9346110703848
2020-04-26 04:52:14          25      5307.817699480746       1706.051317316623
2020-04-26 04:52:14          26      5429.483696931611       1744.576974816624
2020-04-26 04:52:14          28      5463.362668199375       1863.5131526046941
2020-04-26 04:52:14          29      5461.118554480069       1986.080263727048
2020-04-26 04:52:14          30      5413.167601647651       2044.5452642475088
2020-04-26 04:52:14 DJI_0519.JPG
2020-04-26 04:52:14          1       5213.6778631737225      3023.7828397660414
2020-04-26 04:52:14          2       5107.68320887879        3041.5705486829884
2020-04-26 04:52:14          3       5039.2918682202635      3036.3984295921578
2020-04-26 04:52:14          4       5014.522101308937       3058.7413113417138
2020-04-26 04:52:14          5       4999.244478034453       3103.7599212108794
2020-04-26 04:52:14          6       4983.824018141209       3128.6598546096975
2020-04-26 04:52:14          7       4910.948360308057       3135.9466257822314
2020-04-26 04:52:14          8       4851.315913634555       3091.3370769022804
2020-04-26 04:52:14          9       4837.76924456987        3012.046073855523
2020-04-26 04:52:14          10      4809.575068337885       2936.6066249177497
2020-04-26 04:52:14          11      4771.237234304563       2936.350731356655
2020-04-26 04:52:14          12      4707.136828815248       2954.1574186118005
2020-04-26 04:52:14          13      4658.296389551959       2919.099597530514
2020-04-26 04:52:14          14      4657.719814537101       2867.7051821660457
2020-04-26 04:52:14          15      4713.437018729565       2815.722647359388
2020-04-26 04:52:14          16      4784.870419179495       2783.245319466867
2020-04-26 04:52:14          17      4828.3766212167075      2756.641794605126
2020-04-26 04:52:14          18      4845.464811035038       2731.195712135293
2020-04-26 04:52:14          19      4817.990873824569       2678.79590200754
2020-04-26 04:52:14          20      4809.880471095922       2608.9864250920314
2020-04-26 04:52:14          21      4873.095777223584       2569.394630888251
2020-04-26 04:52:14          22      4959.663842391963       2542.067447583815
2020-04-26 04:52:14          23      5042.588242627018       2559.2311385366656
2020-04-26 04:52:14          24      5108.967384823463       2571.4664272799578
2020-04-26 04:52:14          25      5176.050007898206       2626.6856301479897
2020-04-26 04:52:14          26      5295.971282040188       2669.8053534732853
2020-04-26 04:52:14          27      5361.8120375979715      2728.2552997573534
2020-04-26 04:52:14          28      5330.258062192983       2789.5413296711486
2020-04-26 04:52:14          29      5328.9482510480075      2912.2939500370862
2020-04-26 04:52:14          30      5281.913121189106       2971.452836780579

In screen capture, shown is output of image vertex coordinates for a line on 3 selected images...
« Last Edit: April 26, 2020, 01:06:10 PM by Paulo »
Best Regards,
Paul Pelletier,
Surveyor

fjgarciam

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Exporting identified shape on images
« Reply #3 on: April 26, 2020, 01:03:26 PM »
Hi paulo thanks for the response. I have 2 problems look at the pictures. why?
Hi,

the following script would print out camera, vertex number, x and y image coordinates for each vertex of shape (30 vertices) appearing in 4 selected cameras:

Code: [Select]
chunk = Metashape.app.document.chunk
T = chunk.transform.matrix
crs = chunk.shapes.crs
cameras_sel = [camera for camera in chunk.cameras if camera.selected]
shapes = [shape for shape in chunk.shapes if shape.selected]
shape = shapes[0]
print("Image vertex coordinates for shape:",shape.key)
for camera in cameras_sel:
print(camera.label)
num_ver = 0
for vertex in shape.vertices:
num_ver += 1
x = camera.project(T.inv().mulp(crs.unproject(vertex))).x
y = camera.project(T.inv().mulp(crs.unproject(vertex))).y
if (x < 0 or x > camera.sensor.width or y < 0 or y > camera.sensor.height):
continue
print("\t",num_ver,"\t",x,"\t",y)

and the result would be something like:

2020-04-26 04:52:14 Image vertex coordinates for shape: 0
2020-04-26 04:52:14 DJI_0516.JPG
2020-04-26 04:52:14          4       5453.743946402111       222.74381861926574
2020-04-26 04:52:14          5       5436.709416745306       269.3403659912882
2020-04-26 04:52:14          6       5420.49620855976        292.47811552495216
2020-04-26 04:52:14          7       5343.148310226199       311.81425185146117
2020-04-26 04:52:14          8       5281.496274732476       274.0485517086035
2020-04-26 04:52:14          9       5270.234735955541       189.28171042920087
2020-04-26 04:52:14          10      5241.590308505169       122.02298426452973
2020-04-26 04:52:14          11      5201.906692295227       122.19297889122483
2020-04-26 04:52:14          12      5134.898857946424       141.99334040022097
2020-04-26 04:52:14          13      5085.438478906967       105.60425594436147
2020-04-26 04:52:14          14      5085.034369251849       60.84692039321978
2020-04-26 04:52:14          15      5144.0228449162105      7.838497359763436
2020-04-26 04:52:14 DJI_0517.JPG
2020-04-26 04:52:14          2       5386.638683515784       1181.5120947377204
2020-04-26 04:52:14          3       5316.6578823684085      1176.3952952565621
2020-04-26 04:52:14          4       5291.389854523749       1195.644797724307
2020-04-26 04:52:14          5       5274.8489701372855      1241.7422764771925
2020-04-26 04:52:14          6       5258.823552196129       1265.4900429203633
2020-04-26 04:52:14          7       5182.918003413208       1280.540624322772
2020-04-26 04:52:14          8       5121.94962021607        1240.240376996891
2020-04-26 04:52:14          9       5109.907318263296       1157.2579120653324
2020-04-26 04:52:14          10      5081.476673605759       1087.0560926021465
2020-04-26 04:52:14          11      5042.210226657976       1086.9887727657867
2020-04-26 04:52:14          12      4976.1554076993825      1105.9451833197536
2020-04-26 04:52:14          13      4926.888599478631       1069.8535108506933
2020-04-26 04:52:14          14      4926.479117567041       1022.7842815760089
2020-04-26 04:52:14          15      4984.401404256255       970.242157768817
2020-04-26 04:52:14          16      5058.266631630511       936.1891529594453
2020-04-26 04:52:14          17      5104.092274798773       903.7874938243431
2020-04-26 04:52:14          18      5123.049831937411       871.259837085535
2020-04-26 04:52:14          19      5093.296624271075       835.4618874524924
2020-04-26 04:52:14          20      5085.833099677106       767.605894219358
2020-04-26 04:52:14          21      5151.37059673191        726.863173731634
2020-04-26 04:52:14          22      5240.7172444995695      697.7833614369108
2020-04-26 04:52:14          23      5325.9402114134355      710.9612726560171
2020-04-26 04:52:14          24      5394.028082329082       720.9912679258749
2020-04-26 04:52:14          25      5462.1509446421305      773.942547067115
2020-04-26 04:52:14 DJI_0518.JPG
2020-04-26 04:52:14          1       5343.5776947674785      2097.906886807614
2020-04-26 04:52:14          2       5235.952070527379       2117.6328634795395
2020-04-26 04:52:14          3       5166.826489639096       2112.521306211412
2020-04-26 04:52:14          4       5141.825436950572       2133.378791659176
2020-04-26 04:52:14          5       5125.959024393331       2178.9680761094005
2020-04-26 04:52:14          6       5110.258178211332       2203.3325948553643
2020-04-26 04:52:14          7       5035.995667007567       2214.477027256982
2020-04-26 04:52:14          8       4975.763327543942       2171.9798459778885
2020-04-26 04:52:14          9       4962.920485269887       2090.815013955641
2020-04-26 04:52:14          10      4934.622911226999       2017.8855901177858
2020-04-26 04:52:14          11      4895.859996430065       2017.739698788529
2020-04-26 04:52:14          12      4830.860458763899       2036.1602161534292
2020-04-26 04:52:14          13      4781.8206187023825      2000.5801430320716
2020-04-26 04:52:14          14      4781.31721701037        1951.247066738112
2020-04-26 04:52:14          15      4838.052731064362       1898.9147027306808
2020-04-26 04:52:14          16      4910.608996923446       1865.6145305481923
2020-04-26 04:52:14          17      4955.187546475447       1836.1373866732526
2020-04-26 04:52:14          18      4973.141338118379       1807.1980489173955
2020-04-26 04:52:14          19      4944.610341243703       1762.8957417022202
2020-04-26 04:52:14          20      4936.796341311201       1693.990354191347
2020-04-26 04:52:14          21      5001.096731500073       1653.8010286018512
2020-04-26 04:52:14          22      5088.980299966174       1625.586705941213
2020-04-26 04:52:14          23      5173.0077100685185      1640.7926344903
2020-04-26 04:52:14          24      5240.22135288281        1651.9346110703848
2020-04-26 04:52:14          25      5307.817699480746       1706.051317316623
2020-04-26 04:52:14          26      5429.483696931611       1744.576974816624
2020-04-26 04:52:14          28      5463.362668199375       1863.5131526046941
2020-04-26 04:52:14          29      5461.118554480069       1986.080263727048
2020-04-26 04:52:14          30      5413.167601647651       2044.5452642475088
2020-04-26 04:52:14 DJI_0519.JPG
2020-04-26 04:52:14          1       5213.6778631737225      3023.7828397660414
2020-04-26 04:52:14          2       5107.68320887879        3041.5705486829884
2020-04-26 04:52:14          3       5039.2918682202635      3036.3984295921578
2020-04-26 04:52:14          4       5014.522101308937       3058.7413113417138
2020-04-26 04:52:14          5       4999.244478034453       3103.7599212108794
2020-04-26 04:52:14          6       4983.824018141209       3128.6598546096975
2020-04-26 04:52:14          7       4910.948360308057       3135.9466257822314
2020-04-26 04:52:14          8       4851.315913634555       3091.3370769022804
2020-04-26 04:52:14          9       4837.76924456987        3012.046073855523
2020-04-26 04:52:14          10      4809.575068337885       2936.6066249177497
2020-04-26 04:52:14          11      4771.237234304563       2936.350731356655
2020-04-26 04:52:14          12      4707.136828815248       2954.1574186118005
2020-04-26 04:52:14          13      4658.296389551959       2919.099597530514
2020-04-26 04:52:14          14      4657.719814537101       2867.7051821660457
2020-04-26 04:52:14          15      4713.437018729565       2815.722647359388
2020-04-26 04:52:14          16      4784.870419179495       2783.245319466867
2020-04-26 04:52:14          17      4828.3766212167075      2756.641794605126
2020-04-26 04:52:14          18      4845.464811035038       2731.195712135293
2020-04-26 04:52:14          19      4817.990873824569       2678.79590200754
2020-04-26 04:52:14          20      4809.880471095922       2608.9864250920314
2020-04-26 04:52:14          21      4873.095777223584       2569.394630888251
2020-04-26 04:52:14          22      4959.663842391963       2542.067447583815
2020-04-26 04:52:14          23      5042.588242627018       2559.2311385366656
2020-04-26 04:52:14          24      5108.967384823463       2571.4664272799578
2020-04-26 04:52:14          25      5176.050007898206       2626.6856301479897
2020-04-26 04:52:14          26      5295.971282040188       2669.8053534732853
2020-04-26 04:52:14          27      5361.8120375979715      2728.2552997573534
2020-04-26 04:52:14          28      5330.258062192983       2789.5413296711486
2020-04-26 04:52:14          29      5328.9482510480075      2912.2939500370862
2020-04-26 04:52:14          30      5281.913121189106       2971.452836780579

In screen capture, shown is output of image vertex coordinates for a line on 3 selected images...

Paulo

  • Hero Member
  • *****
  • Posts: 1320
    • View Profile
Re: Exporting identified shape on images
« Reply #4 on: April 26, 2020, 01:36:57 PM »
I think from https://www.agisoft.com/forum/index.php?topic=12094.0,
Quote
.... that this shape has markers attached to its vertices. So the vertex locations are defined by the markers' projections on the individual images.

In this case to get the coordinates you should check the .position of the related markers with the required .key value. The following code should give you the coordinates of the vertices, providing that the chunk is georeferenced:
Code: [Select]
vertices =  [(i.position) for i in chunk.markers if i.key in shape.vertex_ids]]
so the code in case of attached markers to shape would be:
Code: [Select]
chunk = Metashape.app.document.chunk
T = chunk.transform.matrix
crs = chunk.shapes.crs
cameras_sel = [camera for camera in chunk.cameras if camera.selected]
shapes = [shape for shape in chunk.shapes if shape.selected]
shape = shapes[0]
vertices =  [(i.position) for i in chunk.markers if i.key in shape.vertex_ids]
print("Image vertex coordinates for shape:",shape.key)
for camera in cameras_sel:
print(camera.label)
num_ver = 0
for vertex in vertices:
num_ver += 1
x = camera.project(vertex).x
y = camera.project(vertex).y
if (x < 0 or x > camera.sensor.width or y < 0 or y > camera.sensor.height):
continue
print("\t",num_ver,"\t",x,"\t",y)

Note that images corresponding to selected shape need to be selected in order for code to work...
Best Regards,
Paul Pelletier,
Surveyor

Paulo

  • Hero Member
  • *****
  • Posts: 1320
    • View Profile
Re: Exporting identified shape on images
« Reply #5 on: April 26, 2020, 02:13:21 PM »
Modified code with 2 selected shapes, one with attached markers and other without....
Best Regards,
Paul Pelletier,
Surveyor

fjgarciam

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Exporting identified shape on images
« Reply #6 on: April 26, 2020, 02:15:34 PM »
hi, now works. Can be exported to a shape with local coordinates. A shape for every image proyection of the shape? The text list is very very good but to work with this is  not easy..

armadillo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Exporting identified shape on images
« Reply #7 on: April 26, 2020, 11:56:50 PM »
Oh hi guys, I thought this thread was dead a long time ago. I eventually solved my problem but neglected to post an update. The script I wrote can be found here. It is ugly and uses a hard coded path for the export file (line 5) which needs to be modified but, other than that, it does the job.

Paulo

  • Hero Member
  • *****
  • Posts: 1320
    • View Profile
Re: Exporting identified shape on images
« Reply #8 on: April 27, 2020, 06:09:13 AM »
Armadillo,

thanks for the input... I looked at your script and modified it to take into account that some shapes may have some markers and thus vertices are extracted differently:
Code: [Select]
import Metashape
import json

output_file = 'C:/Users/paul.pelletier/Desktop/polygons.json'
def shape2image(shape, camera, chunk):
"""
Converts model coordinates of passed shape to image coordinates of passed camera and chunk.
If the shape does not lie completely in the camera image, returns None.
"""
image_coords = list()
T = chunk.transform.matrix #4x4 transformation matrix

if shape.vertex_ids:
    vertices = [(i.position) for i in chunk.markers if i.key in shape.vertex_ids]
else:
    vertices = [(T.inv().mulp(chunk.shapes.crs.unproject(vertex))) for vertex in  shape.vertices]
for vertex in vertices:
if not camera.project(vertex):
return None
pixel_coords = camera.project(vertex)
pixel_coords[0] = round(pixel_coords[0],3)
pixel_coords[1] = round(pixel_coords[1],3)
if pixel_coords[0] > camera.sensor.width or pixel_coords[0] < 0 or pixel_coords[1] > camera.sensor.height or pixel_coords[1] < 0:  # check coords lie inside image
return None
image_coords.append(pixel_coords)
return image_coords


def get_all_shapes_in_camera(camera, chunk):
""" Returns a list of lists.
Each list contains coordinates in image (pixel) coordinates of a shape.
Every shape in the chunk that is completely inside the camera's image is in the list.
"""
pixel_shapes = list() # list to hold the shapes in pixel coords
for shape in chunk.shapes:
ps = shape2image(shape, camera, chunk)
if ps is not None:
pixel_shapes.append({"label": shape.label, "type": shape.type, "vertices": ps})
return pixel_shapes


chunk = Metashape.app.document.chunk
image_data = list()
cameras = [camera for camera in chunk.cameras if camera.type == Metashape.Camera.Type.Regular]
for i in range(len(cameras)):
if not cameras[i].enabled:
continue
image_data.append({"image": cameras[i].label, "shapes": get_all_shapes_in_camera(cameras[i], chunk)})


with open(output_file, 'w') as f:
json.dump(image_data, f, indent=4, ensure_ascii=True, skipkeys=True)

print("Polygons saved in file: " + output_file)
Now I run it and get error in json.dump line as see in following:
Code: [Select]
2020-04-26 21:58:35 Traceback (most recent call last):
2020-04-26 21:58:35   File "C:/Users/paul.pelletier/Documents/Photoscan/Scripts/Export_Shape_imagecoords.py", line 55, in <module>
2020-04-26 21:58:35     json.dump(image_data, f, indent=4, sort_keys=True)
2020-04-26 21:58:35   File "C:\Program Files\Agisoft\Metashape Pro\python\lib\json\__init__.py", line 178, in dump
2020-04-26 21:58:35     for chunk in iterable:
2020-04-26 21:58:35   File "C:\Program Files\Agisoft\Metashape Pro\python\lib\json\encoder.py", line 427, in _iterencode
2020-04-26 21:58:35     yield from _iterencode_list(o, _current_indent_level)
2020-04-26 21:58:35   File "C:\Program Files\Agisoft\Metashape Pro\python\lib\json\encoder.py", line 324, in _iterencode_list
2020-04-26 21:58:35     yield from chunks
2020-04-26 21:58:35   File "C:\Program Files\Agisoft\Metashape Pro\python\lib\json\encoder.py", line 403, in _iterencode_dict
2020-04-26 21:58:35     yield from chunks
2020-04-26 21:58:35   File "C:\Program Files\Agisoft\Metashape Pro\python\lib\json\encoder.py", line 324, in _iterencode_list
2020-04-26 21:58:35     yield from chunks
2020-04-26 21:58:35   File "C:\Program Files\Agisoft\Metashape Pro\python\lib\json\encoder.py", line 403, in _iterencode_dict
2020-04-26 21:58:35     yield from chunks
2020-04-26 21:58:35   File "C:\Program Files\Agisoft\Metashape Pro\python\lib\json\encoder.py", line 324, in _iterencode_list
2020-04-26 21:58:35     yield from chunks
2020-04-26 21:58:35   File "C:\Program Files\Agisoft\Metashape Pro\python\lib\json\encoder.py", line 436, in _iterencode
2020-04-26 21:58:35     o = _default(o)
2020-04-26 21:58:35   File "C:\Program Files\Agisoft\Metashape Pro\python\lib\json\encoder.py", line 179, in default
2020-04-26 21:58:35     raise TypeError(repr(o) + " is not JSON serializable")
2020-04-26 21:58:35 TypeError: Vector([2400.865, 985.495]) is not JSON serializable
2020-04-26 21:58:35 Error: Vector([2400.865, 985.495]) is not JSON serializable

Note that image_data dict is correctly built as seen in:
Code: [Select]
image_data
Out[1]: 2020-04-26 21:59:16
2020-04-26 21:59:16 [{'image': '000001.png',
2020-04-26 21:59:16   'shapes': [{'label': '1',
2020-04-26 21:59:16     'vertices': [Vector([2400.865, 985.495]), Vector([2489.325, 1522.156])]},
2020-04-26 21:59:16    {'label': '2',
2020-04-26 21:59:16     'vertices': [Vector([2254.728, 1070.235]),
2020-04-26 21:59:16      Vector([2089.051, 960.86]),
2020-04-26 21:59:16      Vector([2057.241, 1165.198]),
2020-04-26 21:59:16      Vector([2261.582, 1216.922])]}]},
2020-04-26 21:59:16  {'image': '000002.png',
2020-04-26 21:59:16   'shapes': [{'label': '1',
2020-04-26 21:59:16     'vertices': [Vector([2396.742, 1024.154]), Vector([2456.715, 1559.424])]},
2020-04-26 21:59:16    {'label': '2',
2020-04-26 21:59:16     'vertices': [Vector([2243.45, 1106.666]),
2020-04-26 21:59:16      Vector([2081.281, 990.555]),
2020-04-26 21:59:16      Vector([2041.085, 1196.629]),
2020-04-26 21:59:16      Vector([2243.052, 1253.72])]}]},

As I am not very knowledgeable in python, what would be the problem.....
Attached is the json file generated....
« Last Edit: April 27, 2020, 08:20:39 AM by Paulo »
Best Regards,
Paul Pelletier,
Surveyor

armadillo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Exporting identified shape on images
« Reply #9 on: April 28, 2020, 05:32:41 PM »
Hello Paulo, it's been a long time and I don't currently, have access to the same environment I wrote the scripts in but, like you say, json.dump throws a hissy fit.

I am not terribly familiar with the json encoder myself, I just got it working and didn't bother too much with it. Perhaps you could try making some Vector objects and see if you can export them as json and then take it from there?