Created
February 2, 2019 23:47
-
-
Save egore/c7f4e67c5e4c33853468b81b8896636d to your computer and use it in GitHub Desktop.
Read picasa.ini and show the faces detected by picasa using python and opencv
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 configparser | |
import cv2 | |
import sys | |
def coordinates(rect): | |
''' | |
Translate encoded coordinates from picasa.ini into number array | |
''' | |
# From 'rect64(x)' to 'x' | |
rect = rect[7:] | |
rect = rect[:-1] | |
# Left pad with zeros just in case | |
rect = rect.zfill(16) | |
# Convert to int from 0..65535 | |
part1 = int(rect[:4], 16) | |
part2 = int(rect[4:8], 16) | |
part3 = int(rect[8:12], 16) | |
part4 = int(rect[12:16], 16) | |
return [part1, part2, part3, part4] | |
def render_rect(img, img_w, img_h, coords): | |
# Extract variables from parameter | |
(x_min, y_min, x_max, y_max) = coords | |
# Calculate relative positions from | |
x_min = int(img_w * (x_min / 65535)) | |
y_min = int(img_h * (y_min / 65535)) | |
x_max = int(img_w * (x_max / 65535)) | |
y_max = int(img_h * (y_max / 65535)) | |
# Render the rectangle | |
cv2.rectangle(img, (x_min, y_min), (x_max, y_max), (0xff, 0xff, 0xff), 3) | |
def render_name(img, img_w, img_h, text, coords): | |
# Extract variables from parameter | |
(x_min, y_min, x_max, _) = coords | |
# Calculate the textsize | |
textsize = cv2.getTextSize(text, cv2.FONT_HERSHEY_PLAIN, 2, 3)[0] | |
# Determine position | |
x = int(img_w * ((x_min + ((x_max - x_min) / 2)) / 65535)) - int(textsize[0] / 2) | |
y = int(img_h * (y_min / 65535)) + textsize[1] + 10 | |
# Render text | |
cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 2, (0xff, 0xff, 0xff), 3) | |
if len(sys.argv) != 2: | |
print('Pass directory to index as argument') | |
sys.exit(1) | |
dir = sys.argv[1] | |
config = configparser.ConfigParser() | |
config.read(dir + '/.picasa.ini') | |
# Build up a dictionary of ID -> Name | |
dict = {} | |
# Read Contacts2 section into dictionary | |
for (id, name) in config.items('Contacts2'): | |
dict[id] = name.split(';')[0] | |
# Prepare window | |
cv2.namedWindow('image', cv2.WINDOW_NORMAL) | |
cv2.resizeWindow('image', 1024, 768) | |
# Read all sections | |
for s in config.sections(): | |
key = 0 | |
# If it wasn't the Contacts2 section ... | |
if (s != 'Contacts2'): | |
# Read faces info from section | |
faces = config[s]['faces'] | |
if faces: | |
# If we have faces, we will show the image | |
img = cv2.imread(dir + '/' + s) | |
# Read image width and height | |
img_h = len(img) | |
img_w = len(img[0]) | |
# Determine faces from the list (semicolon separated) | |
l = faces.split(';') | |
for face in l: | |
# Determine position and ID from list | |
(rect,id) = face.split(',') | |
# Calculate coordinates | |
coords = coordinates(rect) | |
# Render name and image border | |
render_rect(img, img_w, img_h, coords) | |
render_name(img, img_w, img_h, dict[id], coords) | |
# Show the actual image | |
cv2.imshow('image', img) | |
key = cv2.waitKey() & 0xff | |
# Abort if 'ESC' was pressed, otherwise continue | |
if (key == 27): | |
break | |
# Remove the windows when done | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment