[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

(no)test[clari8] 04 collecting training samples from Photos (in iOS device)






"Ensure we have access to Photos to collect test samples"
Smalltalk nativeObjectAt: #PHPhotoLibrary ifAbsent: [
    self print: 'Require Photos framework.'.
    self abortPage
].


"Ensure the Photos.framework is bound to the system"
[ PHPhotoLibrary authorizationStatus ] on: Error do: [:error|
	self print: 'Photos.framework must be bound to application to collect sample images from Photos'.
	self abortPage
].


"Ensure Training set is initialized"
Smalltalk at: #TrainingSet ifAbsent: [
	TrainingSet := PoolDictionary new.
]


"Collecting samples from Photos. Moments collection"
| maxAssetsCount count concepts extractConcepts assets register |
maxAssetsCount := 5. count := 0.
extractConcepts := [:title| | stream line add |
	concepts := HashedSet new.
	add := [:string| concepts
		add: string;
		addAll: (string asArrayOfSubstrings
			select: [:word| word first isUppercase ])
	].
	stream := title stream.
	[stream atEnd] whileFalse: [ line := (stream upTo: $-) trimBlanks.
		(line includes: $&) ifFalse: [ add value: line ]
		ifTrue: [ add value: (line upTo: $&) trimBlanks; value: (line fromLast: $&) trimBlanks ]
	].
	concepts := concepts asArray sorted.
].
register := [:tag :assets|
	(TrainingSet at: tag ifAbsentPut: [#()]) addAll: assets
].
PHAssetCollection moments do: [:each|
	(each title notNil and: [each estimatedCount > 0]) ifTrue: [
		self print: each title asString ,'[',each estimatedCount,']'.
		assets := each assets select: [:asset| asset mediaTypeSelector = #image ].
		assets notEmpty ifTrue: [
			count := count + assets size.
			extractConcepts value: each title asString.
			concepts do: [:tag| register value: tag value: assets ].
			"self print: '// ', assets size,'  images with ', concepts asLiteral."
			count > maxAssetsCount ifTrue: [
				^self print: 'Enough images has been registered'
			]
		]
	].
].
self print: 'Training set contains ',TrainingSet size,' concepts'.