Python : Creation de PNG à base de CSV, avec des EXIF de localisation

J’ai fait un petit programme pour faire des PNG à l’aide d’un fichier CSV. Le but est de mettre des balises sur Google Picture.

Voici un exemple de fichier CSV :

$ cat list.csv 
Nom,Année,Mois,Jours,Lieux,Pays
Deplacement sur Paris,2018,11,01,Paris,France
Deplacement sur Londres,2011,11,01,London,UK

Voici le programme :

from PIL import Image, ImageDraw, ImageFont
import piexif
from datetime import datetime
import csv
from geopy.geocoders import Nominatim
from GPSPhoto import gpsphoto

geolocator = Nominatim(user_agent="Your_Name")

with open('list.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            line_count += 1
        else:
            name = '%s \n Le %d/%d/%d \n a %s,%s . :' % (str(row[0]),int(row[1]),int(row[2]),int(row[3]),str(row[4]),str(row[5]))
            print('\t %s ' % name)
            line_count += 1
            filename = 'image-%d.jpg' % (line_count)

            img = Image.new('RGB', (1024, 800), color = (73, 109, 137))

            d = ImageDraw.Draw(img)
            fontsize = 80 
            font = ImageFont.truetype('/usr/share/fonts/truetype/msttcorefonts/Arial.ttf', fontsize)
            d.text((10,10), name, font=font)
            img.save(filename)

            exif_dict = piexif.load(filename)
            new_date = datetime(int(row[1]), int(row[2]), int(row[3]), 0, 0, 0).strftime("%Y:%m:%d %H:%M:%S")
            exif_dict['0th'][piexif.ImageIFD.DateTime] = new_date
            exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = new_date
            exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized] = new_date
            exif_bytes = piexif.dump(exif_dict)
            piexif.insert(exif_bytes, filename)
            address= '%s,%s' % (row[4], row[5])
            location = geolocator.geocode(address)
            print('\t\t %f %f %d' % (location.latitude, location.longitude, location.altitude))
            photo = gpsphoto.GPSPhoto(filename)
            info = gpsphoto.GPSInfo((location.latitude, location.longitude), alt=int(location.altitude), timeStamp=new_date)
            photo.modGPSData(info, filename)

    print('Processed %d lines.' % line_count)


Pour qu’il fonctionne il faut avoir installé :

# pip install geopy
# pip install GPSPhoto
# pip install exifread

Pour l’installation des fonts sous Ubuntu :

# sudo apt-get install gsfonts gsfonts-other gsfonts-x11 ttf-mscorefonts-installer t1-xfree86-nonfree fonts-alee ttf-ancient-fonts fonts-arabeyes fonts-arphic-bkai00mp fonts-arphic-bsmi00lp fonts-arphic-gbsn00lp fonts-arphic-gkai00mp fonts-atarismall fonts-dustin fonts-f500 fonts-sil-gentium ttf-georgewilliams ttf-isabella fonts-larabie-deco fonts-larabie-straight fonts-larabie-uncommon ttf-sjfonts ttf-staypuft ttf-summersby fonts-ubuntu-title ttf-xfree86-nonfree xfonts-intl-european xfonts-jmk xfonts-terminus

Pour vérifier :

# exiftool image-2.jpg 
ExifTool Version Number         : 11.88
File Name                       : image-2.jpg
Directory                       : .
File Size                       : 36 kB
File Modification Date/Time     : 2021:02:22 14:55:29+01:00
File Access Date/Time           : 2021:02:22 14:55:30+01:00
File Inode Change Date/Time     : 2021:02:22 14:55:29+01:00
File Permissions                : rw-rw-r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
JFIF Version                    : 1.01
Resolution Unit                 : None
X Resolution                    : 1
Y Resolution                    : 1
Exif Byte Order                 : Big-endian (Motorola, MM)
Modify Date                     : 2018:11:01 00:00:00
Date/Time Original              : 2018:11:01 00:00:00
Create Date                     : 2018:11:01 00:00:00
GPS Latitude Ref                : North
GPS Longitude Ref               : East
GPS Altitude Ref                : Above Sea Level
GPS Time Stamp                  : 00:00:00
GPS Processing Method           : GPS
GPS Date Stamp                  : 2018:11:01
Image Width                     : 1024
Image Height                    : 800
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:2:0 (2 2)
Image Size                      : 1024x800
Megapixels                      : 0.819
GPS Altitude                    : 0 m Above Sea Level
GPS Date/Time                   : 2018:11:01 00:00:00Z
GPS Latitude                    : 48 deg 51' 24.11" N
GPS Longitude                   : 2 deg 21' 5.26" E
GPS Position                    : 48 deg 51' 24.11" N, 2 deg 21' 5.26" E

 

Quand Python vient corriger un problème de script … Merci

Je me suis fait des doublons de fichiers dans mes photos, heureusement Python c’est simple et convivial :

import os
import sys
import re
import PIL
from PIL import Image

walk_dir = "./Nextcloud/Photos/"

print('walk_dir = ' + walk_dir)
print('walk_dir (absolute) = ' + os.path.abspath(walk_dir))

for root, subdirs, files in os.walk(walk_dir):
    list_file_path = os.path.join(root, 'my-directory-check.txt')
    print('list_file_path = ' + list_file_path)
    with open(list_file_path, 'wb') as list_file:
        #for subdir in subdirs:
        #   print('\t- subdirectory ' + subdir)
        for filename in files:
            file_path = os.path.join(root, filename)
            # print('\t- file %s (full path: %s)' % (filename, file_path))
            base = os.path.splitext(filename)[0]
            if (base.endswith('_1')):
                    #print('\t- Doublon probable %s %s (full path: %s)' % (filename, base, file_path))
                    double = re.sub('_1$', '', base)
                    double = double + '.jpg'
                    double2 = re.sub('_1$', '', base)
                    double2 = double2 + '.JPG'
                    file_path_double = os.path.join(root, double)
                    file_path_double2 = os.path.join(root, double2)
                    if os.path.isfile(file_path_double):
                        #print('\t- Doublon OK %s & %s' % (file_path, file_path_double))
                        img1 = PIL.Image.open(file_path)
                        img2 = PIL.Image.open(file_path_double)
                        wid1, hgt1 = img1.size 
                        wid2, hgt2 = img2.size 
                        if (wid1 == wid2) and (hgt1 == hgt2):
                            #print('Meme resolution')
                            size1 = os.path.getsize(file_path)
                            size2 = os.path.getsize(file_path_double)
                            if (size1 > size2):
                                    print("%s > %s" % (file_path, file_path_double));
                                    os.remove(file_path_double);
                            else:
                                    print("%s < %s" % (file_path, file_path_double));
                                    os.remove(file_path);
                    elif os.path.isfile(file_path_double2):
                        img1 = PIL.Image.open(file_path)
                        img2 = PIL.Image.open(file_path_double2)
                        wid1, hgt1 = img1.size
                        wid2, hgt2 = img2.size
                        if (wid1 == wid2) and (hgt1 == hgt2):
                            #print('Meme resolution')
                            size1 = os.path.getsize(file_path)
                            size2 = os.path.getsize(file_path_double2)
                            if (size1 > size2):
                                    print("%s > %s" % (file_path, file_path_double2));
                                    os.remove(file_path_double2);
                            else:
                                    print("%s < %s" % (file_path, file_path_double2));
                                    os.remove(file_path);


Ensuite pour faire un clean des fichiers txt :

find ./Nextcloud/Photos/ -name my-directory-check.txt -exec /bin/rm -f {} \;

Vive les boulettes ..

(Draft) Ubuntu, Jupyter Notebook, Python3, Tensorflow, OpenCV : Comptage des cyclistes avec yolo model et floutage des visages

J’ai enfin mon process complet, pour compter les cyclistes et flouter les visages dans la vidéo :

Etape 1 : Faire la vidéo avec la GoPRO.

  • Le résultat est un MP4.
  • La taille : 91 Mo
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp42isomavc1
creation_time : 2020-10-04T13:52:54.000000Z
encoder : HandBrake 0.9.8 2012071800
Duration: 00:01:51.44, start: 0.000000, bitrate: 6813 kb/s
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 6810 kb/s, 23.98 fps, 23.98 tbr, 90k tbn, 180k tbc (default)
Metadata:
creation_time : 2020-10-04T13:52:54.000000Z
encoder : JVT/AVC Coding

Etape 2 : Passer Jupyter Notebook, Python3, Tensorflow, OpenCV.

  • Le résultat est un AVI.
  • La commande : tracking-yolo-model.ipynb
  • La taille : 400 Mo
Metadata:
encoder : Lavf58.58.100
Duration: 00:01:29.07, start: 0.000000, bitrate: 37644 kb/s
Stream #0:0: Video: mjpeg (MJPG / 0x47504A4D), yuvj420p(pc, bt470bg/unknown/unknown), 1280x720, 37651 kb/s, 30 fps, 30 tbr, 30 tbn, 30 tbc

Etape 3 : Convertir le AVI en MP4.

  • Le résultat est un MP4.
  • La commande : ffmpeg -i output-bicyle.avi output-bicyle.mp4
  • La taille : 60 Mo
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf57.83.100
Duration: 00:01:29.07, start: 0.000000, bitrate: 5605 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc), 1280x720, 5602 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler

Etape 4 : Flouter les visages via https://github.com/ORB-HD/deface

  • Le résultat est un MP4.
  • La commande : deface output-bicyle.mp4
  • La taille : 35 Mo.
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Duration: 00:01:29.07, start: 0.000000, bitrate: 3241 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720, 3238 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler

Etape 5 : Compresser la video pour le Web.

  • Le résultat est un MP4.
  • La commande : ffmpeg -i output-bicyle_anonymized.mp4 -c:v libx264 -b:v 1.5M -c:a aac -b:a 128k -filter:v scale=540×480 output-bicyle_anonymized_compress.mp4
  • La taille : 16 Mo.
 Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf57.83.100
Duration: 00:01:29.07, start: 0.000000, bitrate: 1477 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 540x480, 1474 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler

Le résultat final :

Python facetool : Modification d’un visage par une image dans un MP4.

J’ai téléchargé https://github.com/hay/facetool afin de faire la modification des visages via une commande du type :

./facetool.py swap -i smiley.jpg -t output-bicyle.mp4 -o swap-bicyle.mp4

D’abord j’ai eu un premier problème que j’ai fixé ainsi :
cd /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

Et maintenant j’ai un bug dans le programme :

No faces found, could not swap (Faceswapping smiley.jpg on head-tmp-jdrmcn3j/2357.jpg, saving to out-tmp-jdrmcn3j/2357.jpg)
100%|███████████████████████████████████████████████████████████████████▉| 2671/2672 [25:15<00:00, 2.30it/s]
No faces found, could not swap (Faceswapping smiley.jpg on head-tmp-jdrmcn3j/1180.jpg, saving to out-tmp-jdrmcn3j/1180.jpg)
Traceback (most recent call last):
File "./facetool.py", line 591, in <module>
main(args)
File "./facetool.py", line 565, in main
swapper.swap_image_to_video(args.target, args.input, args.output)
File "./github/facetool/facetool/swapper.py", line 206, in swap_image_to_video
combineframes(self.tempdirs.out, out)
File "./github/facetool/facetool/media.py", line 49, in combineframes
first_file = list(glob(f"{inp}/*"))[0]
IndexError: list index out of range

Bref j’ai l’impression que cela ne détecte aucun visage :

~/github/facetool$ ls -al head-tmp-jdrmcn3j/*.jpg | wc -l
2672
~/github/facetool$ ls -al out-tmp-jdrmcn3j/*.jpg | wc -l
ls: cannot access 'out-tmp-jdrmcn3j/*.jpg': No such file or directory
0

Je vais devoir chercher ailleur.