Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access People album used in Apple's Photos app

Apple's Photo app has facial recognition capabilities. I would like to know if an API exists to access the associated data for photos which have been classified by the app.

(tagged with API, not sure which tags.)

Thanks!

like image 463
MachuPichu Avatar asked Mar 18 '17 01:03

MachuPichu


2 Answers

Apple released an SDK for accessing Apple Photos called Photo Kit. In the documentation, you will find API access to the photos application and photos cloud. However, if you are looking for an HTTP API I do not believe one has been announced publicly.

If you only need access to is data on Photos MacOS app I would recommend digging through the SQLite photos.db located in the photos library database directory.

/Users/[yourUserName]/Pictures/Photos Library.photoslibrary/database/photos.db

I am working on a project this weekend that requires similar face data and will document my progress. Here is a sample SQLite query you can use to get all faces, photos, and corresponding person:

SELECT *, RKFace.modelId AS fId
FROM RKFace
JOIN RKMaster ON RKMaster.modelId = RKFace.imageModelId
LEFT JOIN RKFaceGroup ON RKFaceGroup.modelId = RKFace.faceGroupId
LEFT JOIN RKFaceCrop ON RKFaceCrop.faceId = RKFace.imageModelId
LEFT JOIN RKFacePrint ON RKFacePrint.faceId = RKFace.imageModelId
LEFT JOIN RKPerson ON RKPerson.modelId = RKFace.personId;

I hope this helps. :-)

like image 124
Adrian Avatar answered Sep 25 '22 09:09

Adrian


The file photos.db mentioned by Adrian unfortunately did not contain the data anymore in 2021. However, I found a big file called /Users/[yourUserName]/Pictures/Photos Library.photoslibrary/database/Photos.sqlite that looked promising. Here's an SQL query that will return all photos of myself as an example:

SELECT DISTINCT ZPERSON.ZFULLNAME AS FULL_NAME, ZGENERICASSET.ZFILENAME AS FILENAME, ZGENERICASSET.ZDATECREATED + 978307200 AS DATECREATED
FROM ZPERSON
INNER JOIN ZDETECTEDFACE ON ZPERSON.Z_PK=ZDETECTEDFACE.ZPERSON
INNER JOIN ZGENERICASSET ON ZDETECTEDFACE.ZASSET=ZGENERICASSET.Z_PK
WHERE ZPERSON.ZFULLNAME LIKE "Christian%"
ORDER BY ZGENERICASSET.ZDATECREATED DESC

The returned columns are FULL_NAME, FILENAME and DATECREATED. The resulting date is specified as a Unix timestamp in seconds, though it may also contain a fraction. For example, 1618068987.64944 refers to 3:36:27 pm UTC | Saturday, April 10, 2021.

For reference, 978307200 is the difference between the Apple epoch and the Unix epoch in seconds. I obtained it in Python through this expression: (datetime(2001, 1, 1, 0, 0) - datetime(1970, 1, 1, 0, 0)).total_seconds().

like image 22
Christian Brüggemann Avatar answered Sep 23 '22 09:09

Christian Brüggemann