Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"files": [
"README.md"
],
"imageSize": 100,
"commit": false,
"commitType": "docs",
"commitConvention": "angular",
"contributors": [
{
"login": "abhijitjadhav1998",
"name": "Abhijit Jadhav",
"avatar_url": "https://avatars.githubusercontent.com/u/38549908?v=4",
"profile": "https://www.linkedin.com/in/abhijitjadhav1998/",
"contributions": [
"projectManagement"
]
},
{
"login": "vthonte",
"name": "Vishwesh Thonte",
"avatar_url": "https://avatars.githubusercontent.com/u/43621438?v=4",
"profile": "http://vthonte.vercel.app/",
"contributions": [
"maintenance"
]
}
],
"contributorsPerLine": 7,
"skipCi": true,
"repoType": "github",
"repoHost": "https://github.com",
"projectName": "Deepfake_detection_using_deep_learning",
"projectOwner": "abhijitjadhav1998"
}
2 changes: 1 addition & 1 deletion Django Application/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ docker run -p 80:80 --volumes-from deepfakeapplication -v static_volume:/home/ap

### Step 5: Star⭐ this repo 😉 on <a href="https://github.com/abhijitjadhav1998/Deepfake_detection_using_deep_learning" > <img src="https://img.shields.io/badge/GitHub-100000?style=for-the-badge&logo=github&logoColor=white" /> </a> and Star⭐ this image on <a href="https://hub.docker.com/r/abhijitjadhav1998/deefake-detection-20framemodel"> <img src="https://img.shields.io/badge/Docker-2CA5E0?style=for-the-badge&logo=docker&logoColor=white" /> </a>

## We deserve a Coffee ☕ <a href="https://www.buymeacoffee.com/abhijitjadhav" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 35px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a> <a href="https://github.com/abhijitjadhav1998/Deepfake_detection_using_deep_learning" target="_blank"><img src="https://img.shields.io/badge/UPI-abhijit.jadhav1%40ybl-brightgreen" alt="Medium" style="height: 35px !important;width: 250px !important;"/></a>&nbsp;
## We deserve a Coffee ☕ <a href="https://www.buymeacoffee.com/abhijitjadhav" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 35px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>


Please note that currently we have only pushed the image of 20 Frames model, If you can to create your own image of other frames model follow the steps given in the [blog](https://abhijithjadhav.medium.com/dockerise-deepfake-detection-django-application-using-nvidia-cuda-40cdda3b6d38).
Expand Down
194 changes: 110 additions & 84 deletions Django Application/ml_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@

index_template_name = 'index.html'
predict_template_name = 'predict.html'
about_template_name = "about.html"

im_size = 112
mean=[0.485, 0.456, 0.406]
std=[0.229, 0.224, 0.225]
sm = nn.Softmax()
inv_normalize = transforms.Normalize(mean=-1*np.divide(mean,std),std=np.divide([1,1,1],std))
if torch.cuda.is_available():
device = 'gpu'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

device = 'cuda'

else:
device = 'cpu'

train_transforms = transforms.Compose([
transforms.ToPILImage(),
Expand Down Expand Up @@ -124,12 +129,12 @@ def im_plot(tensor):
image = cv2.merge((r,g,b))
image = image*[0.22803, 0.22145, 0.216989] + [0.43216, 0.394666, 0.37645]
image = image*255.0
plt.imshow(image.astype(int))
plt.imshow(image.astype('uint8'))
plt.show()


def predict(model,img,path = './', video_file_name=""):
fmap,logits = model(img.to('cuda'))
fmap,logits = model(img.to(device))
img = im_convert(img[:,-1,:,:,:], video_file_name)
params = list(model.parameters())
weight_softmax = model.linear1.weight.detach().cpu().numpy()
Expand All @@ -140,7 +145,7 @@ def predict(model,img,path = './', video_file_name=""):
return [int(prediction.item()),confidence]

def plot_heat_map(i, model, img, path = './', video_file_name=''):
fmap,logits = model(img.to('cuda'))
fmap,logits = model(img.to(device))
params = list(model.parameters())
weight_softmax = model.linear1.weight.detach().cpu().numpy()
logits = sm(logits)
Expand Down Expand Up @@ -173,25 +178,30 @@ def get_accurate_model(sequence_length):
sequence_model = []
final_model = ""
list_models = glob.glob(os.path.join(settings.PROJECT_DIR, "models", "*.pt"))
for i in list_models:
model_name.append(i.split("\\")[-1])
for i in model_name:

for model_path in list_models:
model_name.append(os.path.basename(model_path))

for model_filename in model_name:
try:
seq = i.split("_")[3]
if (int(seq) == sequence_length):
sequence_model.append(i)
except:
pass
seq = model_filename.split("_")[3]
if int(seq) == sequence_length:
sequence_model.append(model_filename)
except IndexError:
pass # Handle cases where the filename format doesn't match expected

if len(sequence_model) > 1:
accuracy = []
for i in sequence_model:
acc = i.split("_")[1]
accuracy.append(acc)
for filename in sequence_model:
acc = filename.split("_")[1]
accuracy.append(acc) # Convert accuracy to float for proper comparison
max_index = accuracy.index(max(accuracy))
final_model = sequence_model[max_index]
final_model = os.path.join(settings.PROJECT_DIR, "models", sequence_model[max_index])
elif len(sequence_model) == 1:
final_model = os.path.join(settings.PROJECT_DIR, "models", sequence_model[0])
else:
final_model = sequence_model[0]
print("No model found for the specified sequence length.") # Handle no models found case

return final_model

ALLOWED_VIDEO_EXTENSIONS = set(['mp4','gif','webm','avi','3gp','wmv','flv','mkv'])
Expand Down Expand Up @@ -248,114 +258,130 @@ def index(request):

def predict_page(request):
if request.method == "GET":
# Redirect to 'home' if 'file_name' is not in session
if 'file_name' not in request.session:
return redirect("ml_app:home")
if 'file_name' in request.session:
video_file = request.session['file_name']
if 'sequence_length' in request.session:
sequence_length = request.session['sequence_length']
path_to_videos = [video_file]
video_file_name = video_file.split('\\')[-1]
if settings.DEBUG == False:
production_video_name = video_file_name.split('/')[3:]
production_video_name = '/'.join([str(elem) for elem in production_video_name])
print("Production file name",production_video_name)
video_file_name_only = video_file_name.split('.')[0]
video_dataset = validation_dataset(path_to_videos, sequence_length=sequence_length,transform= train_transforms)
model = Model(2).cuda()
model_name = os.path.join(settings.PROJECT_DIR,'models', get_accurate_model(sequence_length))
models_location = os.path.join(settings.PROJECT_DIR,'models')
video_file_name = os.path.basename(video_file)
video_file_name_only = os.path.splitext(video_file_name)[0]
# Production environment adjustments
if not settings.DEBUG:
production_video_name = os.path.join('/home/app/staticfiles/', video_file_name.split('/')[3])
print("Production file name", production_video_name)
else:
production_video_name = video_file_name

# Load validation dataset
video_dataset = validation_dataset(path_to_videos, sequence_length=sequence_length, transform=train_transforms)

# Load model
if(device == "gpu"):
model = Model(2).cuda() # Adjust the model instantiation according to your model structure
else:
model = Model(2).cpu() # Adjust the model instantiation according to your model structure
model_name = os.path.join(settings.PROJECT_DIR, 'models', get_accurate_model(sequence_length))
path_to_model = os.path.join(settings.PROJECT_DIR, model_name)
model.load_state_dict(torch.load(path_to_model))
model.load_state_dict(torch.load(path_to_model, map_location=torch.device('cpu')))
model.eval()
start_time = time.time()
# Start: Displaying preprocessing images
# Display preprocessing images
print("<=== | Started Videos Splitting | ===>")
preprocessed_images = []
faces_cropped_images = []
cap = cv2.VideoCapture(video_file)

frames = []
while(cap.isOpened()):
while cap.isOpened():
ret, frame = cap.read()
if ret==True:
if ret:
frames.append(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()

for i in range(1, sequence_length+1):
frame = frames[i]
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = pImage.fromarray(image, 'RGB')
image_name = video_file_name_only+"_preprocessed_"+str(i)+'.png'
if settings.DEBUG:
image_path = os.path.join(settings.PROJECT_DIR, 'uploaded_images', image_name)
else:
print("image_name",image_name)
image_path = "/home/app/staticfiles" + image_name
img.save(image_path)
preprocessed_images.append(image_name)
print("<=== | Videos Splitting Done | ===>")
print("--- %s seconds ---" % (time.time() - start_time))
# End: Displaying preprocessing images


# Start: Displaying Faces Cropped Images
print("<=== | Started Face Cropping Each Frame | ===>")
print(f"Number of frames: {len(frames)}")
# Process each frame for preprocessing and face cropping
padding = 40
faces_found = 0
for i in range(1, sequence_length+1):
for i in range(sequence_length):
if i >= len(frames):
break
frame = frames[i]
#fig, ax = plt.subplots(1,1, figsize=(5, 5))
face_locations = face_recognition.face_locations(frame)

# Convert BGR to RGB
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

# Save preprocessed image
image_name = f"{video_file_name_only}_preprocessed_{i+1}.png"
image_path = os.path.join(settings.PROJECT_DIR, 'uploaded_images', image_name)
img_rgb = pImage.fromarray(rgb_frame, 'RGB')
img_rgb.save(image_path)
preprocessed_images.append(image_name)

# Face detection and cropping
face_locations = face_recognition.face_locations(rgb_frame)
if len(face_locations) == 0:
continue

top, right, bottom, left = face_locations[0]
frame_face = frame[top-padding:bottom+padding, left-padding:right+padding]
image = cv2.cvtColor(frame_face, cv2.COLOR_BGR2RGB)
frame_face = frame[top - padding:bottom + padding, left - padding:right + padding]

img = pImage.fromarray(image, 'RGB')
image_name = video_file_name_only+"_cropped_faces_"+str(i)+'.png'
if settings.DEBUG:
image_path = os.path.join(settings.PROJECT_DIR, 'uploaded_images', video_file_name_only+"_cropped_faces_"+str(i)+'.png')
else:
image_path = "/home/app/staticfiles" + image_name
img.save(image_path)
faces_found = faces_found + 1
# Convert cropped face image to RGB and save
rgb_face = cv2.cvtColor(frame_face, cv2.COLOR_BGR2RGB)
img_face_rgb = pImage.fromarray(rgb_face, 'RGB')
image_name = f"{video_file_name_only}_cropped_faces_{i+1}.png"
image_path = os.path.join(settings.PROJECT_DIR, 'uploaded_images', image_name)
img_face_rgb.save(image_path)
faces_found += 1
faces_cropped_images.append(image_name)
print("<=== | Face Cropping Each Frame Done | ===>")

print("<=== | Videos Splitting and Face Cropping Done | ===>")
print("--- %s seconds ---" % (time.time() - start_time))

# No face is detected
# No face detected
if faces_found == 0:
return render(request, predict_template_name, {"no_faces": True})
return render(request, 'predict_template_name.html', {"no_faces": True})

# End: Displaying Faces Cropped Images
# Perform prediction
try:
heatmap_images = []
for i in range(0, len(path_to_videos)):
output = ""
print("<=== | Started Predicition | ===>")
output = ""
confidence = 0.0

for i in range(len(path_to_videos)):
print("<=== | Started Prediction | ===>")
prediction = predict(model, video_dataset[i], './', video_file_name_only)
confidence = round(prediction[1], 1)
print("<=== | Predicition Done | ===>")
# print("<=== | Heat map creation started | ===>")
# for j in range(0, sequence_length):
# heatmap_images.append(plot_heat_map(j, model, video_dataset[i], './', video_file_name_only))
if prediction[0] == 1:
output = "REAL"
else:
output = "FAKE"
print("Prediction : " , prediction[0],"==",output ,"Confidence : " , confidence)
output = "REAL" if prediction[0] == 1 else "FAKE"
print("Prediction:", prediction[0], "==", output, "Confidence:", confidence)
print("<=== | Prediction Done | ===>")
print("--- %s seconds ---" % (time.time() - start_time))

# Uncomment if you want to create heat map images
# for j in range(sequence_length):
# heatmap_images.append(plot_heat_map(j, model, video_dataset[i], './', video_file_name_only))

# Render results
context = {
'preprocessed_images': preprocessed_images,
'faces_cropped_images': faces_cropped_images,
'heatmap_images': heatmap_images,
'original_video': production_video_name,
'models_location': os.path.join(settings.PROJECT_DIR, 'models'),
'output': output,
'confidence': confidence
}

if settings.DEBUG:
return render(request, predict_template_name, {'preprocessed_images': preprocessed_images, 'heatmap_images': heatmap_images, "faces_cropped_images": faces_cropped_images, "original_video": video_file_name, "models_location": models_location, "output": output, "confidence": confidence})
return render(request, predict_template_name, context)
else:
return render(request, predict_template_name, {'preprocessed_images': preprocessed_images, 'heatmap_images': heatmap_images, "faces_cropped_images": faces_cropped_images, "original_video": production_video_name, "models_location": models_location, "output": output, "confidence": confidence})
except:
return render(request, predict_template_name, context)

except Exception as e:
print(f"Exception occurred during prediction: {e}")
return render(request, 'cuda_full.html')
def about(request):
return render(request, about_template_name)
Expand Down
Loading