|
15 | 15 |
|
16 | 16 | import json
|
17 | 17 | import os
|
18 |
| -import sys |
19 | 18 |
|
20 | 19 | # Example of using python crypto to sign data using openssl with Ps256
|
21 | 20 | from cryptography.hazmat.primitives import hashes, serialization
|
@@ -54,19 +53,32 @@ def getitem(d, key):
|
54 | 53 | "format": "image/jpeg",
|
55 | 54 | "identifier": "thumbnail"
|
56 | 55 | },
|
57 |
| - "assertions": [{ |
58 |
| - "label": "cawg.training-mining", |
59 |
| - "data": { |
60 |
| - "entries": { |
61 |
| - "cawg.ai_inference": { |
62 |
| - "use": "notAllowed" |
63 |
| - }, |
64 |
| - "cawg.ai_generative_training": { |
65 |
| - "use": "notAllowed" |
| 56 | + "assertions": [ |
| 57 | + { |
| 58 | + "label": "c2pa.actions", |
| 59 | + "data": { |
| 60 | + "actions": [ |
| 61 | + { |
| 62 | + "action": "c2pa.created", |
| 63 | + "digitalSourceType": "http://cv.iptc.org/newscodes/digitalsourcetype/digitalCreation" |
| 64 | + } |
| 65 | + ] |
| 66 | + } |
| 67 | + }, |
| 68 | + { |
| 69 | + "label": "cawg.training-mining", |
| 70 | + "data": { |
| 71 | + "entries": { |
| 72 | + "cawg.ai_inference": { |
| 73 | + "use": "notAllowed" |
| 74 | + }, |
| 75 | + "cawg.ai_generative_training": { |
| 76 | + "use": "notAllowed" |
| 77 | + } |
66 | 78 | }
|
67 | 79 | }
|
68 | 80 | }
|
69 |
| - }] |
| 81 | + ] |
70 | 82 | }
|
71 | 83 |
|
72 | 84 | ingredient_json = {
|
@@ -109,37 +121,48 @@ def getitem(d, key):
|
109 | 121 |
|
110 | 122 | # Sign the file using the stream-based sign method
|
111 | 123 | with open(testFile, "rb") as source_file:
|
112 |
| - with open(testOutputFile, "wb") as dest_file: |
| 124 | + with open(testOutputFile, "w+b") as dest_file: |
113 | 125 | result = builder.sign(signer, "image/jpeg", source_file, dest_file)
|
114 | 126 |
|
115 |
| -except Exception as err: |
116 |
| - sys.exit(err) |
| 127 | + # As an alternative, you can also use file paths directly during signing: |
| 128 | + # builder.sign_file(testFile, testOutputFile, signer) |
117 | 129 |
|
118 |
| -print("V2: successfully added do not train manifest to file " + testOutputFile) |
| 130 | + # Clean up native resources (using a with statement works too!) |
| 131 | + signer.close() |
| 132 | + builder.close() |
| 133 | + |
| 134 | +except Exception as err: |
| 135 | + print("Exception during signing: ", err) |
119 | 136 |
|
| 137 | +print("\nSuccessfully added do not train manifest to file " + testOutputFile) |
120 | 138 |
|
121 | 139 | # now verify the asset and check the manifest for a do not train assertion...
|
122 | 140 |
|
123 | 141 | allowed = True # opt out model, assume training is ok if the assertion doesn't exist
|
124 | 142 | try:
|
125 |
| - # Create reader using the current API |
| 143 | + # Create reader using the Reader API |
126 | 144 | reader = c2pa.Reader(testOutputFile)
|
| 145 | + |
| 146 | + # Retrieve the manifest store |
127 | 147 | manifest_store = json.loads(reader.json())
|
128 | 148 |
|
| 149 | + # Look at data in the active manifest |
129 | 150 | manifest = manifest_store["manifests"][manifest_store["active_manifest"]]
|
130 |
| - |
131 | 151 | for assertion in manifest["assertions"]:
|
132 | 152 | if assertion["label"] == "cawg.training-mining":
|
133 | 153 | if getitem(assertion, ("data","entries","cawg.ai_generative_training","use")) == "notAllowed":
|
134 | 154 | allowed = False
|
135 | 155 |
|
136 |
| - # get the ingredient thumbnail and save it to a file using resource_to_stream |
| 156 | + # Get the ingredient thumbnail and save it to a file using resource_to_stream |
137 | 157 | uri = getitem(manifest,("ingredients", 0, "thumbnail", "identifier"))
|
138 | 158 | with open(output_dir + "thumbnail_v2.jpg", "wb") as thumbnail_output:
|
139 | 159 | reader.resource_to_stream(uri, thumbnail_output)
|
140 | 160 |
|
| 161 | + # Clean up native resources (using a with statement works too!) |
| 162 | + reader.close() |
| 163 | + |
141 | 164 | except Exception as err:
|
142 |
| - sys.exit(err) |
| 165 | + print("Exception during assertions reading: ", err) |
143 | 166 |
|
144 | 167 | if allowed:
|
145 | 168 | print("Training is allowed")
|
|
0 commit comments