Created
February 18, 2019 20:14
-
-
Save mustafaakin/805c81c6227af3dd2acd01b1a4e77707 to your computer and use it in GitHub Desktop.
Finding blurred photos by album
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sqlite3 | |
import cv2 | |
import numpy as np | |
def score(filename): | |
image = cv2.imread(filename) | |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
fm = cv2.Laplacian(gray, cv2.CV_64F).var() | |
return fm | |
if __name__ == '__main__': | |
p = "/Users/mustafa/Pictures/Photos Library.photoslibrary/resources/proxies/derivatives" | |
# Backup of the DB to not corrupt it | |
conn = sqlite3.connect('/Users/mustafa/photos.db') | |
# Find out the albums first | |
for row in conn.execute("SELECT RKAlbum.modelId, RKAlbum.name FROM RKAlbum GROUP BY 2"): | |
album_id = row[0] | |
album_name = row[1] | |
scores = [] | |
# Select 100 Random photos from each album | |
query = """select RKVersion.modelId FROM RKAlbumVersion, RKVersion, RKMaster | |
WHERE RKVersion.modelId = RKAlbumVersion.versionId | |
AND RKMaster.modelId = RKVersion.masterId | |
AND RKAlbumVersion.albumId = %d | |
ORDER BY RANDOM() | |
LIMIT 100""" % album_id | |
for photo in conn.execute(query): | |
# Magic.. Kidding | |
# 0x831a --> derivatives/83/00/831a | |
h = hex(photo[0]) | |
q = os.path.join(p, h[2:4], "00", h[2:]) | |
try: | |
files = os.listdir(q) | |
for f in files: | |
if "mini" in f: | |
s = score(os.path.join(q, f)) | |
scores.append(s) | |
break | |
except OSError: | |
# Sometimes files are not there | |
pass | |
print "Album :", album_name | |
print " Mean :", np.mean(scores) | |
print " StdDev :", np.std(scores) | |
print "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment