the keefe commited on
Commit
e2fa5c5
·
verified ·
1 Parent(s): d1d3291

Upload 9 files

Browse files
DeepFakeDetectionSoftware/DeepFakeDetection/LispScripts/feature_extraction.lisp ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (in-package :deep-fake-feature-extraction)
2
+
3
+ (defun load-image (path)
4
+ "Load an image from the given path."
5
+ (opticl:load-image path))
6
+
7
+ (defun extract-perceptual-hash (image)
8
+ "Extract a perceptual hash from an image."
9
+ (let* ((width (opticl:image-width image))
10
+ (height (opticl:image-height image))
11
+ (pixels (mapcar #'list (mapcar #'array-element (opticl:image-pixels image)))))
12
+ (map 'vector (lambda (i) (hash (subseq pixels i 0 width))) (list-seq (iota height))))))
13
+
14
+ (defun extract-color-histogram (image)
15
+ "Extract the color histogram from an image."
16
+ ;; Use Opticl functions to analyze the image's color distribution
17
+ (let* ((colors (opticl:color-histogram image))
18
+ (counts (mapcar #'length colors)))
19
+ (list (mapcar (lambda (c) (list (car c) (cdr c))) colors) counts)))
20
+
21
+ (defun extract-texture-features (image)
22
+ "Extract texture features from an image."
23
+ ;; Placeholder for texture feature extraction
24
+ ;; This could involve edge detection, texture analysis, etc.
25
+ (let* ((width (opticl:image-width image))
26
+ (height (opticl:image-height image))
27
+ (pixels (mapcar #'list (mapcar #'array-element (opticl:image-pixels image)))))
28
+ (list (edgedetect pixels)
29
+ (haralick-texture-features pixels)
30
+ (wavelet-transform (opticl:image-pixels image)))))
31
+
32
+ (defun extract-features (image-path)
33
+ "Extract various features from an image."
34
+ (let* ((image (load-image image-path))
35
+ (perceptual-hash (extract-perceptual-hash image))
36
+ (color-histogram (extract-color-histogram image))
37
+ (texture-features (extract-texture-features image)))
38
+ (list perceptual-hash color-histogram texture-features)))
39
+
40
+ (defun main ()
41
+ (let ((sample-image-path "path/to/sample-image.jpg"))
42
+ (format t "Extracted Features: ~A~%" (extract-features sample-image-path))))
43
+
44
+ (main)

45
+
DeepFakeDetectionSoftware/DeepFakeDetection/LispScripts/gan.lisp ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (in-package :deep-fake-gan)
2
+
3
+ (use-package :pyui)
4
+
5
+ (defun create-generator ()
6
+ (py:call-function 'tensorflow.keras.Sequential (list :name "flatten" :input_shape '(28 28 1) :output_shape '(256,) :activation 'tanh)
7
+ (py:call-function 'tensorflow.keras.layers.Conv2D (list :name "conv1" :kernel_size 3 :channels 32 :activation 'relu) (py:call-function 'tensorflow.keras.layers.Conv2D (list :name "conv2" :kernel_size 3 :channels 64 :activation 'relu) (py:call-function 'tensorflow.keras.layers.Flatten))
8
+ (py:call-function 'tensorflow.keras.layers.Dense (list :units 10 :activation 'softmax))
9
+ :build)
10
+ )
11
+
12
+ (defun create-discriminator ()
13
+ (py:call-function 'tensorflow.keras.Sequential (list :name "flatten" :input_shape '(28 28 1) :output_shape '(10,) :activation 'relu)
14
+ (py:call-function 'tensorflow.keras.layers.Conv2D (list :kernel_size 3 :channels 32 :activation 'relu) (py:call-function 'tensorflow.keras.layers.Conv2D (list :kernel_size 3 :channels 64 :activation 'relu) (py:call-function 'tensorflow.keras.layers.Flatten))
15
+ (py:call-function 'tensorflow.keras.layers.Dense (list :units 1 :activation 'sigmoid))
16
+ :build)
17
+ )
18
+
19
+ (defun train-generator (generator discriminator training-data)
20
+ (py:call-function 'tensorflow.keras.models.train_on_batch generator training-data :model generator :targets discriminator)
21
+ (py:call-function 'tensorflow.keras.metrics.categorical_crossentropy (list :name "ce" :targets discriminator) (py:call-function 'tensorflow.keras.metrics.sparse_categorical_accuracy (list :name "accuracy" :targets discriminator))
22
+ :generator generator)
23
+ )
24
+
25
+ (defun train-discriminator (discriminator generator training-data)
26
+ (py:call-function 'tensorflow.keras.models.train_on_batch generator training-data :model discriminator :targets generator)
27
+ (py:call-function 'tensorflow.keras.metrics.categorical_crossentropy (list :name "ce" :targets generator) (py:call-function 'tensorflow.keras.metrics.sparse_categorical_accuracy (list :name "accuracy" :targets generator))
28
+ :discriminator discriminator)
29
+ )
30
+
31
+ (defun main ()
32
+ (setf generator (create-generator))
33
+ (setf discriminator (create-discriminator))
34
+ (save-model generator "path/to/save/generator-model")
35
+ (save-model discriminator "path/to/save/discriminator-model")
36
+ (train-generator generator discriminator (path "path/to/training-data")))
37
+ (train-discriminator discriminator generator (path "path/to/training-data")))
38
+ )
39
+
40
+ (main)
DeepFakeDetectionSoftware/DeepFakeDetection/LispScripts/image_restoration.lisp ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (defun load-corrupted-image (path)
2
+ "Load a corrupted image from the given path using CL-OPENCV or another image processing library."
3
+ (let ((image (open-image path))
4
+ (height (image-height image))
5
+ (width (image-width image)))
6
+ (if (> height 0)
7
+ (values image height width)
8
+ (error "Failed to load the image ~A" path))))
9
+ (defun restore-image-with-gan (corrupted-image-path gan-model)
10
+ "Restore a corrupted image using a pre-trained GAN model."
11
+ (let ((image (open-image corrupted-image-path))
12
+ (height (image-height image))
13
+ (width (image-width image)))
14
+ (if (> height 0)
15
+ (let ((output (run-gan gan-model image)))
16
+ (save-image output (strcat corrupted-image-path "-restored.jpg")))
17
+ (error "Failed to load the corrupted image ~A" corrupted-image-path))))
18
+
19
+ Finally, you can use the functions defined above to restore a corrupted image using GANs:
20
+
21
+ (defun restore-image (image-path)
22
+ "Main function to restore the image using various techniques, including GANs."
23
+ (let ((image (load-corrupted-image image-path)))
24
+ (restore-image-with-gan image-path "path/to/pre-trained/gan/model")))
25
+ (restore-image "path/to/corrupted-image.jpg")
DeepFakeDetectionSoftware/DeepFakeDetection/LispScripts/inpainting.lisp ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (in-package :deep-fake-inpainting)
2
+
3
+ (defpackage :cl-pac
4
+ (:use :cl)
5
+ (:export :load-image :detect-corrupted-areas :save-image))
6
+
7
+ (defpackage :tensorflow
8
+ (:use :cl)
9
+ (:export :load-tensorflow :initialize-tensorflow-session))
10
+
11
+ (defun load-tensorflow ()
12
+ "Load the TensorFlow library."
13
+ (ql:quickload :tensorflow))
14
+
15
+ (defun initialize-tensorflow-session ()
16
+ "Initializes a TensorFlow session."
17
+ (tf:with-session ()
18
+ (tf:reset-default-graph 21)
19
+ (tf:initialize-keras-backend)
20
+ (tf:enable-all-gather)))
21
+
22
+ (defun load-image (path)
23
+ "Load an image from the given path."
24
+ (load-image path))
25
+
26
+ (defun detect-corrupted-areas (image)
27
+ "Detect corrupted or missing areas in the image."
28
+ (detect-corrupted-areas image))
29
+
30
+ (defun inpaint-image (image corrupted-areas)
31
+ "Apply inpainting techniques to restore corrupted areas of the image using a GAN."
32
+ (inpaint-image image corrupted-areas))
33
+
34
+ (defun save-image (image path)
35
+ "Save the processed image to the given path."
36
+ (save-image image path))
37
+
38
+ (defun restore-image (image-path)
39
+ "Main function to restore the image using GAN-based inpainting."
40
+ (let ((image (load-image image-path)))
41
+ (setf image (detect-corrupted-areas image))
42
+ (setf image (inpaint-image image (detect-corrupted-areas image)))
43
+ (save-image image (truename image-path))))
44
+
45
+ (load-tensorflow)
46
+ (initialize-tensorflow-session)
47
+ (in-package :tensorflow)
48
+ (defun load-tensorflow-image (path)
49
+ "Load an image from the given path using TensorFlow."
50
+ (load-image path))
51
+
52
+ (defun gan-inpainting (image corrupted-areas)
53
+ "Apply GAN-based inpainting to restore corrupted areas of the image."
54
+ (let ((gan (make-instance 'tensorflow:gan))
55
+ (session (tensorflow:get-session gan)))
56
+ (dolist (area corrupted-areas)
57
+ (let ((patch (make-instance 'tensorflow:patch :x (truename (first area)) :y (truename (second area)))
58
+ (tf:gather (tf:constant (make-array (list 3 3) :element '(tf:int32)) session) (list 0 0 0))))
59
+ (dolist (i (range 3))
60
+ (dolist (j (range 3))
61
+ (tf:assign (slot-value patch '(truename i) session) (slot-value patch (truename j) session) :persistent-true))
62
+ (tf:assign (slot-value patch '(truename i) session) (slot-value gan '(truename i) session) :persistent-true))))
63
+ (gan-forward-propagation gan session)
64
+ (gan-backward-propagation gan session)
65
+ (gan-optimize gan session)
66
+ (dolist (area corrupted-areas)
67
+ (setf (slot-value image (first area) (second area)) (slot-value patch '(truename (first area)) (truename (second area)) session))))
68
+ image)
69
+ )
70
+
71
+ (defun gan-forward-propagation (gan session)
72
+ "Perform forward propagation through the GAN."
73
+ (dolist (layer (tensorflow:layers gan))
74
+ (when (typep layer 'tensorflow:dense)
75
+ (dolist (i (range (slot-value layer 'units) session))
76
+ (tf:assign (slot-value layer 'output i session) (tf:add (slot-value layer 'kernel i session) (slot-value layer 'bias i session) session))))
77
+ (when (typep layer 'tensorflow:convolutional)
78
+ (dolist (filter (slot-value layer 'filters))
79
+ (dolist (i (range (slot-value layer 'kernel-size) (slot-value layer 'kernel-size) session))
80
+ (dolist (j (range (slot-value layer 'kernel-size) (slot-value layer 'kernel-size) session))
81
+ (tf:assign (slot-value layer 'output i j session) (tf:add (slot-value layer 'kernel i j session) (slot-value layer 'bias i j session) session))))
82
+ (dolist (i (range (slot-value layer 'kernel-size) (slot-value layer 'kernel-size) session))
83
+ (dolist (j (range (slot-value layer 'kernel-size) (slot-value layer 'kernel-size) session))
84
+ (tf:assign (slot-value layer 'output i j session) (tf:add (slot-value layer 'kernel i j session) (slot-value layer 'bias i j session) session)))))
85
+ (dolist (i (range (slot-value layer 'kernel-size) (slot-value layer 'kernel-size) session))
86
+ (dolist (j (range (slot-value layer 'kernel-size) (slot-value layer 'kernel-size) session))
87
+ (tf:assign (slot-value layer 'output i j session) (tf:add (slot-value layer 'kernel i j session) (slot-value layer 'bias i j session) session)))))
88
+ (dolist (i (range (slot-value layer 'kernel-size) (slot-value layer 'kernel-size) session))
89
+ (dolist (j (range (slot-value layer 'kernel-size) (slot-value layer 'kernel-size) session))
90
+ (tf:assign (slot-value layer 'output i j session) (tf:add (slot-value layer 'kernel i j session) (slot-value layer 'bias i j session) session)))))))
91
+ (dolist (layer (tensorflow:layers gan))
92
+ (when (typep layer 'tensorflow:upsampling)
93
+ (dolist (i (range (slot-value layer 'size) session))
94
+ (dolist (j (range (slot-value layer 'size) session))
95
+ (tf:assign (slot-value layer 'output i j session) (tf:add (slot-value layer 'kernel i j session) (slot-value layer 'bias i j session) session))))))
96
+ (when (typep layer 'tensorflow:convolutional)
97
+ (dolist (filter (slot-value layer 'filters))
98
+ (dolist (i (range (slot-value layer 'kernel-size) (slot-value layer 'kernel-size) session))
99
+ (dolist (j (range (slot-value layer 'kernel-size) (slot-value layer 'kernel-size) session))
100
+ (tf:assign (slot-value layer 'output i j session) (tf:add (slot-value layer 'kernel i j session) (slot-value layer 'bias i j session) session))))
101
+ (dolist (i (range (slot-value layer 'kernel-size) (slot-value layer 'kernel-size) session))
102
+ (dolist (j (range (slot-value layer 'kernel-size) (slot-value layer 'kernel-size) session))
103
+ (tf:assign (slot-value layer 'output i j session) (tf:add (slot-value layer 'kernel i j session) (slot-value layer 'bias i j session) session)))))
104
+ (dolist (i (range (slot-value layer 'kernel-size) (slot-value layer 'kernel-size) session))
105
+ (dolist (j (range (slot-value layer 'kernel-size) (slot-value layer 'kernel-size) session))
106
+ (tf:assign (slot-value layer 'output i j session) (tf:add (slot-value layer 'kernel i j session) (slot-value layer 'bias i j session) session)))))))
107
+ (dolist (layer (tensorflow:layers gan))
108
+ (when (typep layer 'tensorflow:fully-connected)
109
+ (dolist (i (range (slot-value layer 'size) session))
110
+ (dolist (j (range (slot-value layer 'size) session))
111
+ (tf:assign (slot-value layer 'output i j session) (tf:add (slot-value layer 'kernel i j session) (slot-value layer 'bias i j session) session))))))
112
+ (when (typep layer 'tensorflow:dense)
113
+ (dolist (i (range (slot-value layer 'size) session))
114
+ (dolist (j (range (slot-value layer 'size) session))
115
+ (tf:assign (slot-value layer 'output i j session) (tf:add (slot-value layer 'kernel i j session) (slot-value layer 'bias i j session) session)))))))
116
+ (dolist (layer (tensorflow:layers gan))
117
+ (when (typep layer 'tensorflow:regression)
118
+ (dolist (i (range (slot-value layer 'size) session))
119
+ (dolist (j (range (slot-value layer 'size) session))
120
+ (tf:assign (slot-value layer 'output i j session) (tf:add (slot-value layer 'kernel i j session) (slot-value layer 'bias i j session) session)))))))
121
+ (dolist (layer (tensorflow:layers gan))
122
+ (when (typep layer 'tensorflow:classification)
123
+ (dolist (i (range (slot-value layer 'size) session))
124
+ (dolist (j (range (slot-value layer 'size) session))
125
+ (tf:assign (slot-value layer 'output i j session) (tf:add (slot-value layer 'kernel i j session) (slot-value layer 'bias i j session) session))))))))
DeepFakeDetectionSoftware/DeepFakeDetection/LispScripts/main.lisp ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (in-package :deep-fake-preprocessing)
2
+ (defpackage :preprocessing
3
+ (:use :cl)
4
+ (:export :preprocess-image))
5
+
6
+ (in-package :preprocessing)
7
+ defun preprocess-image (image-path)
8
+ "Main function to preprocess the image."
9
+ let* ((image (load-image image-path))
10
+ (normalized-image (normalize-image image))
11
+ (resized-image (resize-image normalized-image 1024 1024))
12
+ (image-path (merge-pathnames (pathname-namestring resized-image) :temp))
13
+ (save-image-function (intern (string-append "save-image-" (1+ (random 1000)) "." (pathname-extension resized-image))))
14
+ (setf (gd:image-save-function resized-image) save-image-function)
15
+ (funcall (symbol-function save-image-function) resized-image image-path)
16
+ (format t "Preprocessed image saved to ~A" image-path)
17
+ (values resized-image))
18
+
19
+ (defun load-image (pathname)
20
+ "Load an image from a pathname."
21
+ (let ((image (gd:make-image (pathname-type pathname) :load-from pathname)))
22
+ (when (null image)
23
+ (error "Unable to load image ~A." pathname)
24
+ (return-from load-image nil))
25
+ (values image)))
26
+
27
+ defun normalize-image (image)
28
+ "Normalize the image to a standard format and size."
29
+ let* ((width (gd:image-width image))
30
+ (height (gd;image-height image))
31
+ (new-width 1024)
32
+ (new-height 1024)
33
+ (new-image (make-gd-image :width new-width :height new-height :depth (gd:image-depth image))))
34
+ (loop for x from 0 below new-width
35
+ for y from 0 below new-height
36
+ for pixmap = (gd:get-pixmap-data image)
37
+ for new-pixmap = (gd:get-pixmap-data new-image)
38
+ do (setf (aref new-pixmap x y) (aref pixmap (* (mod x width) (/ width width)) (* (mod y height) (/ height height)))))
39
+ (setf (gd:image-pixmap new-image) new-image)
40
+ new-image))
41
+
42
+ (defun resize-image (image new-width new-height)
43
+ "Resize the image to the given width and height."
44
+ (let* ((image-width (gd:image-width image))
45
+ (image-height (gd:image-height image))
46
+ (new-image (make-gd-image :width new-width :height new-height :depth (gd:image-depth image))))
47
+ (cond ((< image-width new-width) (gd:fill-rectangle new-image 0 0 image-width (1- new-height) (min (1- (/ image-width image-width)) (1- (/ image-height new-height)))))
48
+ ((> image-width new-width) (gd:fill-rectangle new-image 0 0 (1- new-width) image-height (min (1- (/ image-width image-width)) (1- (/ image-height new-height)))))
49
+ ((< image-height new-height) (gd:fill-rectangle new-image 0 0 image-width (1- new-height) (min (1- (/ image-width image-width)) (1- (/ image-height new-height)))))
50
+ ((> image-height new-height) (gd:fill-rectangle new-image 0 0 (1- new-width) image-height (min (1- (/ image-width image-width)) (1- (/ image-height new-height))))))
51
+ (gd:image-save-copy new-image (pathname-name (merge-pathnames (pathname-namestring image) :temp)))
52
+ (setf (gd:image-pixmap new-image) new-image)
53
+ new-image))
54
+
55
+ defun save-image (image-name)
56
+ "Save an image to a file."
57
+ (let* ((image (gd:make-image (pathname-type image-name) :load-from image-name)))
58
+ (when (null image)
59
+ (error "Unable to load image ~A." image-name)
60
+ (return-from save-image nil))
61
+ (let* ((new-image (make-gd-image :width 1024 :height 1024 :depth (gd:image-depth image))))
62
+ (loop for x from 0 below 1024
63
+ for y from 0 below 1024
64
+ for pixmap = (gd:get-pixmap-data image)
65
+ for new-pixmap = (gd:get-pixmap-data new-image)
66
+ do (setf (aref new-pixmap x y) (aref pixmap (/ (mod x (gd:image-width image)) (gd:image-width image)) (/ (mod y (gd:image-height image)) (gd:image-height image)))))
67
+ (setf (gd:image-pixmap new-image) new-image)
68
+ (gd:image-save new-image (pathname-name (merge-pathnames image-name :temp)))
69
+ (gd:image-free image)
70
+ (gd:image-free new-image)
71
+ t))
72
+
73
+ (defun display-image (image-name)
74
+ "Display an image on the screen."
75
+ (let* ((image (gd:make-image (pathname-type image-name) :load-from image-name)))
76
+ (when (null image)
77
+ (error "Unable to load image ~A." image-name)
78
+ (return-from display-image nil))
79
+ (let* ((screen (gd:gd-open-display 1 1))
80
+ (screen-width (gd:gd-display-width screen))
81
+ (screen-height (gd:gd-display-height screen))
82
+ (image-width (gd:image-width image))
83
+ (image-height (gd:image-height image)))
84
+ (when (and (> image-width screen-width) (> image-height screen-height))
85
+ (error "Image is too large to fit on screen."))
86
+ (gd:gd-image-copy-to-display image screen 0 0)
87
+ (gd:gd-image-free image)
88
+ (gd:gd-close-display screen)))
89
+ t)
90
+
91
+ defun display-image-for-seconds (image-name seconds &key (x 0) (y 0))
92
+ "Display the image for the given number of seconds."
93
+ let* ((image (gd:make-image (pathname-type image-name) :load-from image-name)))
94
+ (when (null image)
95
+ (error "Unable to load image ~A." image-name)
96
+ (return-from display-image-for-seconds nil))
97
+ let* ((screen (gd:gd-open-display 1 1))
98
+ (screen-width (gd:gd-display-width screen))
99
+ (screen-height (gd:gd-display-height screen))
100
+ (image-width (gd:image-width image))
101
+ (image-height (gd:image-height image)))
102
+ (when (and (> image-width screen-width) (> image-height screen-height))
103
+ (error "Image is too large to fit on screen."))
104
+ (setf (gd:gd-image-colormap screen) (gd:gd-default-colormap))
105
+ (gd:gd-image-copy-to-display image screen x y)
106
+ (gd:gd-image-free image)
107
+ (gd:usleep (/ 1000 seconds))
108
+ (gd:gd-image_flip_page screen)
109
+ (gd:gd-image_set_palette screen 1)
110
+ (gd:gd-image_set_resolution screen 1)
111
+ (gd:gd-image_set_color_map screen (gd:gd-default-colormap))
112
+ (gd:gd-image_set_bpp screen 8)
113
+ (gd:gd-image_set_max_red screen 0)
114
+ (gd:gd-image_set_max_green screen 0)
115
+ (gd:gd-image_set_max_blue screen 0)
116
+ (gd:gd-image_set_gd_flags screen)
117
+ (gd:gd-image_set_dither screen)
118
+ (gd:gd-image_set_type screen)
119
+ (gd:gd-image_set_x_pixels screen 0)
120
+ (gd:gd-image_set_y_pixels screen 0)
121
+ (gd:gd-image_set_x_spacing screen 0)
122
+ (gd:gd-image_set_y_spacing screen 0)
123
+ (gd:gd-image_set_x_resolution screen 0)
124
+ (gd:gd-image_set_y_resolution screen 0)
125
+ (gd:gd-image_set_alpha screen 0)
126
+ (gd:gd-image_set_interlace screen)
127
+ (gd:gd-image_set_transparent_color screen 0 0 0)
128
+ (gd:gd-image_set_palettes screen)
129
+ (gd:gd-image_set_palette_n_colors screen)
130
+ (gd:gd-image_set_palette_red screen)
131
+ (gd:gd-image_set_palette_green screen)
132
+ (gd:gd-image_set_palette_blue screen)
133
+ (gd:gd-image_set_palette_flag screen)
134
+ (gd:gd-image_set_palette_entry screen)
135
+ (gd:gd-image_set_palette_entry_flags screen)
136
+ (gd:gd-image_set_palette_rle screen)
137
+ (gd:gd-image_set_palette_info screen)
138
+ (gd:gd-image_set_palette_info_flags screen)
139
+ (gd:gd-image_set_palette_info_raw screen)
140
+ (gd:gd-image_set_palette_info_raw_flags screen)
141
+ (gd:gd-image_set_palette_raw screen)
142
+ (gd:gd-image_set_palette_raw_flags screen)
143
+ (gd:gd-image_set_palette_info_raw_flags screen)
144
+ (gd:gd-image_set_palette_info_flags screen)
145
+ (gd:gd-image_set_palette_rle screen)
146
+ (gd:gd-image_set_palette_flags screen)
147
+ (gd:gd-image_set_palette_entry_flags screen)
148
+ (gd:gd-image_set_palette_entry screen)
149
+ (gd:gd-image_set_palette_entry_color screen)
150
+ (gd:gd-image_set_palette_entry_flags screen)
151
+ (gd:gd-image_set_palette_rle_flag screen)
152
+ (gd:gd-image_set_palette_info_raw_flag screen)
153
+ (gd:gd-image_set_palette_info_flags screen)
154
+ (gd:gd-image_set_palette_info_raw screen)
155
+ (gd:gd-image_set_palette_info screen)
156
+ (gd:gd-image_set_palette_entry_raw screen)
157
+ (gd:gd-image_set_palette_entry_flags screen)
158
+ (gd:gd-image_set_palette_entry_color screen)
159
+ (gd:gd-image_set_palette_entry screen)
160
+ (gd:gd-image_set_palette_entry_raw screen)
161
+ (gd:gd-image_set_palette_entry_flags screen)
162
+ (gd:gd-image_set_palette_entry_color screen)
163
+ (gd:gd-image_set_palette_entry screen)
164
+ (gd:gd-image_set_palette_entry_raw screen)
165
+ (gd:gd-image_set_palette_entry_flags screen)
166
+ (gd:gd-image_set_palette_entry_color screen)
167
+ (gd:gd-image_set_palette_entry screen)
168
+ (gd:gd-image_set_palette_entry_raw screen)
169
+ (gd:gd-image_set_palette_entry_flags screen)
170
+ (gd:gd-image_set_palette_entry_color screen)
171
+ (gd:gd-image_set_palette_entry screen)
172
+ (gd:gd-image_set_palette_entry_raw screen)
173
+ (gd:gd-image_set_palette_entry_flags screen)
174
+ (gd:gd-image_set_palette_entry_color screen)
175
+ (gd:gd-image_set_palette_entry screen)
176
+ (gd:gd-image_set_palette_entry_raw screen)
177
+ (gd:gd-image_set_palette_entry_flags screen)
178
+ (gd:gd-image_set_palette_entry_color screen)
179
+ (gd:gd-image_set_palette_entry screen)
180
+ (gd:gd-image_set_palette_entry_raw screen)
181
+ (gd:gd-image_set_palette_entry_flags screen)
182
+ (gd:gd-image_set_palette_entry_color screen)
183
+ (gd:gd-image_set_palette_entry screen)
184
+ (gd:gd-image_set_palette_entry_raw screen)
185
+ (gd:gd-image_set_palette_entry_flags screen)
186
+ (gd:gd-image_set_palette_entry_color screen)
187
+ (gd:gd-image_set_palette_entry screen)
188
+ (gd:gd-image_set_palette_entry_raw screen)
DeepFakeDetectionSoftware/DeepFakeDetection/LispScripts/normalize.lisp ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (in-package :deep-fake-preprocessing)
2
+ (defpackage :cl-gd
3
+ (:use :cl)
4
+ (:export :load-gd-image
5
+ :load-gd-image-with-data
6
+ :save-gd-image))
7
+
8
+ (defun normalize-image (image)
9
+ "Normalize the image to a standard format and size."
10
+ (let* ((width (gd:image-width image))
11
+ (height (gd:image-height image))
12
+ (new-width 1024)
13
+ (new-height 1024)
14
+ (new-image (make-gd-image :width new-width :height new-height :depth (gd:image-depth image))))
15
+ (loop for x from 0 below new-width
16
+ for y from 0 below new-height
17
+ for pixmap = (gd:get-pixmap-data image)
18
+ for new-pixmap = (gd:get-pixmap-data new-image)
19
+ do (setf (aref new-pixmap x y) (aref pixmap (* (mod x width) (/ width width)) (* (mod y height) (/ height height)))))
20
+ (setf (gd:image-pixmap new-image) new-image)
21
+ new-image))
DeepFakeDetectionSoftware/DeepFakeDetection/LispScripts/preprocessing.lisp ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (defpackage :deep-fake-preprocessing
2
+ (:use :cl)
3
+ (:import-from :cl-image-processing :*image-readers* :read-image))
4
+ (in-package :deep-fake-preprocessing)
5
+
6
+ (defparameter *image-readers* (append *image-readers* '("gan-image-reader" :description "Reads images using GAN preprocessing.")))
7
+
8
+ (defun load-image (path &key (reader (find-if #'stringp (car *image-readers*)) :key-restart (error "Unknown image reader: ~A" reader)) (options (car (assoc reader *image-readers*))) :allow-extension t)
9
+ "Load an image from the given path using the specified image reader."
10
+ (read-image (car *image-readers*) path reader options))
11
+
12
+ (defun normalize-image (image)
13
+ "Normalize the image to a standard format and size."
14
+ (let ((new-image (make-array (car (image-size image)) :initial-contents nil :element-type (type-difference (car (image-pixel-type image)) 'double-float))))
15
+ (loop for y below (cadr (image-size image))
16
+ for x below (car (image-size image))
17
+ do (setf (schar new-image x y) (schar image x y))))
18
+ new-image))
19
+
20
+ (defun enhance-image (image)
21
+ "Apply enhancements to the image to improve its quality for analysis."
22
+ (let ((new-image (make-array (car (image-size image)) :initial-contents nil :element-type (type-difference (car (image-pixel-type image)) 'double-float))))
23
+ (loop for y below (cadr (image-size image))
24
+ for x below (car (image-size image))
25
+ do (setf (schar new-image x y) (schar image x y))))
26
+ new-image))
27
+
28
+ (defun preprocess-image (image-path)
29
+ "Main function to preprocess the image using GAN-based techniques."
30
+ (let ((image (load-image image-path :reader "gan-image-reader" :options '(:restore t))))
31
+ (setf image (normalize-image image))
32
+ (let ((new-image (deep-fake-enhance image)))
33
+ (setf new-image (enhance-image new-image))
34
+ (save-image new-image "path/to/preprocessed-image.jpg")))
35
+
36
+ (defun deep-fake-enhance (image)
37
+ "Enhance the image using GAN-based techniques."
38
+ (let* ((generator (make-instance 'gan-generator))
39
+ (discriminator (make-instance 'gan-discriminator)))
40
+ (dolist (batch (deep-fake-generator-training-batch generator discriminator))
41
+ (deep-fake-generator-train generator batch))
42
+ (deep-fake-discriminator-train discriminator image)
43
+ (let ((new-image (make-array (car (image-size image)) :initial-contents nil :element-type (type-difference (car (image-pixel-type image)) 'double-float))))
44
+ (loop for y below (cadr (image-size image))
45
+ for x below (car (image-size image))
46
+ do (setf (schar new-image x y) (schar image x y))))
47
+ new-image))
48
+
49
+ (defun deep-fake-generator-training-batch (generator &key (batch-size 1024) (num-iter 1000) (noise-scale 0.05))
50
+ "Generate a training batch for the generator using GANs."
51
+ (loop with rnd = (make-instance 'random-state)
52
+ for i from 1 below batch-size
53
+ collect (let ((x (make-array (car (image-size generator)) :initial-contents nil :element-type (type-difference (car (image-pixel-type generator)) 'double-float))))
54
+ (loop for y below (cadr (image-size generator))
55
+ for x below (car (image-size generator))
56
+ do (setf (schar x x y) (random rnd :minus 1 :plus 1))))))
57
+
58
+ (defun deep-fake-discriminator-train (discriminator batch &key (num-iter 1000) (noise-scale 0.05))
59
+ "Train the discriminator using GANs."
60
+ (loop for i from 1 below num-iter
61
+ do (let ((loss (deep-fake-discriminator-loss discriminator batch)))
62
+ (when (> (random-real *standard-random* :a 0.5 :b 1.0) loss)
63
+ (deep-fake-generator-train (generator discriminator) batch (random-keyword-arguments (random-keywords :noise-scale 0.05 :num-iter 1000) :noise-scale (1-noise-scale) :num-iter (1-num-iter)))))))
64
+
65
+ (defun deep-fake-discriminator-loss (discriminator batch)
66
+ "Calculate the loss of the discriminator when trained against the batch."
67
+ (let ((true-images (mapcar #'array-to-image batch))
68
+ (predicted-images (mapcar (lambda (x) (mapcar (lambda (y) (schar x y)) x)) true-images)))
69
+ (correct-predictions (mapcar (lambda (x) (mapcar (lambda (y) (zerop (schar x y)))) x)) true-images))
70
+ (incorrect-predictions (mapcar (lambda (x) (mapcar (lambda (y) (minusp (schar x y)))) x)) true-images)))
71
+ (let ((true-sum (apply #'sum (map (lambda (x) (map (lambda (y) (schar x y)) x)) true-images)))))
72
+ (predicted-sum (apply #'sum (map (lambda (x) (map (lambda (y) (schar x y)) x)) predicted-images)))))
73
+ (correct-count (apply #'sum (map (lambda (x) (map (lambda (y) (zerop (schar x y)))) x)) true-images)))
74
+ (incorrect-count (apply #'sum (map (lambda (x) (map (lambda (y) (minusp (schar x y)))) x)) true-images)))
75
+ (loss (- (/ predicted-sum true-sum) (/ correct-count (length true-images)) (/ incorrect-count (length true-images)))))))
DeepFakeDetectionSoftware/DeepFakeDetection/LispScripts/resize.lisp ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (in-package :DEEPFACE)
2
+
3
+ (defpackage :deep-fake-resizing
4
+ (:use :cl))
5
+ (in-package :deep-fake-resizing)
6
+
7
+ (defun resize-image (input-path output-path)
8
+ "Resize the input image to the output size while maintaining its aspect ratio."
9
+ (let* ((image (load-image input-path))
10
+ (w (car image) :in-pattern :sew)
11
+ (h (cddr image) :in-pattern :sew)
12
+ (new-w (car output-path))
13
+ (new-h (cddr output-path)))
14
+ (assert (and (numberp new-w) (numberp new-h)))
15
+ (assert (<= new-w (expt 2 8)))
16
+ (assert (<= new-h (expt 2 8)))
17
+ (let ((new-image (make-array (list (ceiling (truncate new-w / (expt 2 8))) (ceiling (truncate new-h / (expt 2 8)))) :element-type '(unsigned-byte 8))))
18
+ (dotimes (i (truncate new-w / (expt 2 8)))
19
+ (dotimes (j (truncate new-h / (expt 2 8)))
20
+ (setf (aref new-image i j) (aref (aref image 0) i j))))
21
+ (save-image new-image output-path :filter 'cmu-fast-lanczos-resample)
22
+
23
+
24
+ (release-image new-image))))
25
+
26
+ (defun save-image (image path &key (filter 'bicubic) (color-space 'srgb2))
27
+ "Save the image to the given path."
28
+ (with-open-file (stream path
29
+ :direction :output
30
+ :if-exists :replace
31
+ :formatting :unreadable
32
+ :element-type '(unsigned-byte 8))
33
+ (dotimes (i (length image))
34
+ (write-sequence (aref image i) stream))))
35
+
36
+ (defun release-image (image)
37
+ "Release the memory used by the image."
38
+ (setf image nil)
39
+ (garbage-collect))
40
+
41
+ (defun load-image (path)
42
+ "Load an image from the given path."
43
+ (when (probe-file path)
44
+ (with-open-file (stream path
45
+ :direction :input
46
+ :if-does-not-exist :error
47
+ :element-type '(unsigned-byte 8))
48
+ (let ((image (make-array (file-length stream) :element-type '(unsigned-byte 8))))
49
+ (dotimes (i (file-length stream))
50
+ (setf (aref image i) (read-sequence (unsigned-byte 8) stream)))
51
+ image))))
52
+
53
+ (defun main ()
54
+ "The main function for the resize script."
55
+ (interactive)
56
+ (let* ((input-path (read-from-string "Enter input image path: ") :in-package :DEEPFACE)
57
+ (output-path (read-from-string "Enter output image path: ") :in-package :DEEPFACE))
58
+ (if (null input-path)
59
+ (progn
60
+ (print "You must specify an input image path." :in-package :DEEPFACE)
61
+ (return-from main nil))
62
+ (if (null output-path)
63
+ (progn
64
+ (print "You must specify an output image path." :in-package :DEEPFACE)
65
+ (return-from main nil))
66
+ (progn
67
+ (resize-image input-path output-path))))))
68
+
69
+
70
+
71
+ (defun cmu-fast-lanczos-resample (image)
72
+ "Perform a Lanczos resampling filter on the image."
73
+ (when (consp image)
74
+ (let ((image (cdr image))
75
+ (filter (caar image)))
76
+ (if (eq filter 'cmu-fast-lanczos-resample)
77
+ image
78
+ (error "Unknown filter: ~s" filter))))
79
+
80
+ (if (zerop (car image))
81
+ (error "Zero size image." :in-package :DEEPFACE)
82
+ (let ((new-size (expt 2 (1- (log (car image)) 8))))
83
+ (setf (car image) new-size)
84
+ (cmu-fast-lanczos-resample image))))
85
+
86
+ (in-package :deep-fake-preprocessing)
87
+ (defpackage :cl-gd
88
+ (:use :cl)
89
+ (:export :load-gd-image
90
+ :load-gd-image-with-data
91
+ :save-gd-image))
92
+
93
+ defun resize-image (image new-width new-height)
94
+ "Resize the image to the given width and height."
95
+ (let* ((image-width (gd:image-width image))
96
+ (image-height (gd:image-height image))
97
+ (new-image (make-gd-image :width new-width :height new-height :depth (gd:image-depth image))))
98
+ (flet ((fill-area (x y w h)
99
+ (declare (type fixnum x y w h))
100
+ (dotimes (n h)
101
+ (dotimes (m w)
102
+ (setf (aref (gd:get-pixmap-data new-image) m n)
103
+ (min (1- (/ w image-width))
104
+ (1- (/ h image-height))))))))
105
+ (cond ((< image-width new-width) (fill-area (1- new-width) 0 (image-width) image-height))
106
+ ((> image-width new-width) (fill-area 0 0 (image-width) (1- new-height)))
107
+ ((< image-height new-height) (fill-area 0 0 image-width (1- new-height)))
108
+ ((> image-height new-height) (fill-area (1- new-width) 0 image-width (1- new-height))))
109
+ (setf (gd:image-pixmap new-image) new-image)
110
+ new-image))
DeepFakeDetectionSoftware/DeepFakeDetection/main.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import json
3
+
4
+ def call_lisp_script(script_path, input_data):
5
+ # Convert input data to JSON
6
+ input_json = json.dumps(input_data)
7
+
8
+ # Call Lisp script with subprocess
9
+ result = subprocess.run(['lisp', script_path], input=bytes(input_json, 'utf-8'), capture_output=True)
10
+
11
+ # Parse the output from Lisp script
12
+ output_data = json.loads(result.stdout)
13
+
14
+ return output_data
15
+
16
+ def load_image(image_path):
17
+ # Call preprocessing Lisp script
18
+ return call_lisp_script('LispScripts/preprocessing.lisp', {'image_path': image_path})
19
+
20
+ def resize_image(image, width, height):
21
+ # Call resize Lisp script
22
+ return call_lisp_script('LispScripts/resize.lisp', {'image': image, 'width': width, 'height': height})
23
+
24
+ def normalize_image(image):
25
+ # Call normalize Lisp script
26
+ return call_lisp_script('path_to_normalize.lisp', {'image': image})
27
+
28
+ def inpaint_image(image, mask):
29
+ # Call inpainting Lisp script
30
+ return call_lisp_script('LispScripts/inpainting.lisp', {'image': image, 'mask': mask})
31
+ def inpaint_image(image, mask):
32
+ # Call inpainting Lisp script
33
+ return call_lisp_script('LispScripts/main.lisp', {'image': image, 'mask': mask})
34
+
35
+ def restore_image(image):
36
+ # Call restoration Lisp script
37
+ return call_lisp_script('LispScripts/image_restoration.lisp', {'image': image})
38
+
39
+ def generate_image_with_gan(gan_input_data):
40
+ # Call GAN Lisp script
41
+ return call_lisp_script('LispScripts/gan.lisp', {'gan_input_data': gan_input_data})
42
+
43
+ def extract_features(image):
44
+ # Call feature extraction Lisp script
45
+ return call_lisp_script('LispScripts/feature_extraction.lisp', {'image': image})
46
+
47
+ def main():
48
+ # Path to the image file
49
+ image_path = 'path_to_image.jpg'
50
+
51
+ # Load and preprocess the image
52
+ image = load_image(image_path)
53
+ resized_image = resize_image(image, 256, 256)
54
+ normalized_image = normalize_image(resized_image)
55
+
56
+ # Inpainting and restoration
57
+ mask = 'path_to_mask.png'
58
+ inpainted_image = inpaint_image(normalized_image, mask)
59
+ restored_image = restore_image(inpainted_image)
60
+
61
+ # GAN generation and feature extraction
62
+ gan_input_data = 'path_to_gan_input_data.txt'
63
+ generated_image = generate_image_with_gan(gan_input_data)
64
+ features = extract_features(restored_image)
65
+
66
+ return features
67
+ def call_lisp_script(script_path, input_data):
68
+ # Convert input data to JSON
69
+ input_json = json.dumps(input_data)
70
+
71
+ # Replace 'lisp' with the correct command for your Lisp interpreter (e.g., 'sbcl')
72
+ result = subprocess.run(['sbcl', '--script', script_path], input=bytes(input_json, 'utf-8'), capture_output=True)
73
+
74
+ # Parse the output from Lisp script
75
+ output_data = json.loads(result.stdout)
76
+
77
+ return output_data
78
+
79
+ if __name__ == '__main__':
80
+ main()