# Load required libraries
library(tiff)
library(imager)
## Loading required package: magrittr
## 
## Attaching package: 'imager'
## The following object is masked from 'package:magrittr':
## 
##     add
## The following objects are masked from 'package:stats':
## 
##     convolve, spectrum
## The following object is masked from 'package:graphics':
## 
##     frame
## The following object is masked from 'package:base':
## 
##     save.image
library(e1071)

#Supress Grey Scale warning
options(warn=-1)

# Set the path to the fingerprint database
fingerprint_path <- "/Users/cynthiamcginnis/Downloads/DB1_B/"

# Load and preprocess the fingerprint images
fingerprint_files <- list.files(fingerprint_path, pattern = "\\.tif$", full.names = TRUE)

fingerprints <- lapply(fingerprint_files, function(file) {
  img <- readTIFF(file)
  img_imager <- as.cimg(img)
  img_gray <- grayscale(img_imager)
  as.vector(img_gray)
})

# Prepare the data for SVM
num_samples <- length(fingerprints)
num_features <- length(fingerprints[[1]])

data <- matrix(unlist(fingerprints), nrow = num_samples, byrow = TRUE)

# Generate labels for the fingerprints
labels <- rep(1:length(fingerprint_files))

# Train the SVM model
svm_model <- svm(data, labels)

# Predict the test fingerprint (Using the same fingerprint for demonstration)
# You can replace this with an actual test fingerprint from the database
test_fingerprint <- data[1, ] 

# Predict the class of the test fingerprint
prediction <- predict(svm_model, t(matrix(test_fingerprint)))

# Display the predicted class
print(paste("Predicted Class:", prediction))
## [1] "Predicted Class: 9.13157954186497"