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.