# zhaoshuang
import onnx
import onnxruntime as ort
import numpy as np
from PIL import Image
from pathlib import Path
import os
import cv2
import dlib
import torch
import torch.nn as nn
import pyautogui
import draw
global flag
flag: int = 1
def get_boundingbox(face, width, height, scale=1.3, minsize=None):
"""
Expects a dlib face to generate a quadratic bounding box.
:param face: dlib face class
:param width: frame width
:param height: frame height
:param scale: bounding box size multiplier to get a bigger face region
:param minsize: set minimum bounding box size
:return: x, y, bounding_box_size in opencv form
"""
x1 = face.left()
y1 = face.top()
x2 = face.right()
y2 = face.bottom()
size_bb = int(max(x2 - x1, y2 - y1) * scale)
if minsize:
if size_bb < minsize:
size_bb = minsize
center_x, center_y = (x1 + x2) // 2, (y1 + y2) // 2
# Check for out of bounds, x-y top left corner
x1 = max(int(center_x - size_bb // 2), 0)
y1 = max(int(center_y - size_bb // 2), 0)
# Check for too big bb size for given x, y
size_bb = min(width - x1, size_bb)
size_bb = min(height - y1, size_bb)
return x1, y1, size_bb
def detect(picname,session):
'''
Detection classification
:param picname:Screenshot image
:param session:use NPU
:return: label,real,fake,noface
'''
image = picname
# Face detector
face_detector = dlib.get_frontal_face_detector()
# Text variables
font_face = cv2.FONT_HERSHEY_SIMPLEX
thickness = 2
font_scale = 1
height, width = image.shape[:2]
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_detector(gray, 1)
if len(faces):
# For now only take biggest face
face = faces[0]
# Prediction
# Face crop with dlib and bounding box scale enlargement
x, y, size = get_boundingbox(face, width, height)
cropped_face = image[y:y+size, x:x+size]
cf = Image.fromarray(cropped_face)
imagex = cf.resize((299, 299))
image_array = np.array(imagex).astype(np.float32)
# image_array = image_array/255
# Reshape the array to match the input shape expected by the model
image_array = np.transpose(image_array, (2, 0, 1))
# Add a batch dimension to the input image
input_data = np.expand_dims(image_array, axis=0)
# Run the model
outputs = session.run(None, {'input': input_data})
#Process the outputs
output_array = outputs[0]
post_function=nn.Softmax(dim=1)
temp = torch.from_numpy(output_array)
output = post_function(temp)
_, prediction = torch.max(output, 1) # argmax
prediction = float(prediction.numpy())
x = face.left()
y = face.top()
w = face.right() - x
h = face.bottom() - y
label = 'fake' if prediction == 1 else 'real'
color = (0, 255, 0) if prediction == 0 else (0, 0, 255)
output_list = ['{0:.2f}'.format(float(x)) for x in
output.numpy()[0]]
print(output_list)
cv2.putText(image, str(output_list)+'=>'+label, (x, y+h+30),
font_face, font_scale,
color, thickness, 2)
# draw box over face
cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
print(x,y,w,h,label)
draw.juxing((x, y, x+w, y+h),1)
#cv2.imshow('DeepFake Detect', image)
#cv2.waitKey(10) # About 30 fps
return label
else:
draw.juxing((100, 100, 500, 500),1)
return 'noface'
print("done")
def init_session():
'''
Initialize NPU
:return: NPU session
'''
quantized_model_path = 'model_df_1.onnx'
model = onnx.load(quantized_model_path)
path = r'voe-4.0-win_amd64'
providers = ['VitisAIExecutionProvider']
cache_dir = Path(__file__).parent.resolve()
provider_options = [{
'config_file': os.path.join('..', path, 'vaip_config.json'),
'cacheDir': str(cache_dir),
'cacheKey': 'quickstart_modelcachekey'
}]
session = ort.InferenceSession(model.SerializeToString(), providers=providers,
provider_options=provider_options)
return session
def df(session):
'''
mian program
:param session:NPU session
'''
count = 0
res = []
global flag
while(count<3):
img = pyautogui.screenshot(region=None) # x,y,w,h
# img.save('screenshot.png')
img = cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)
lab = detect(img,session)
res.append(lab)
count = count + 1
resmax = max(res,key=res.count)
print(resmax)
from win11toast import toast
if resmax=='fake':
toast('Deepfake Detect', 'The face maybe fake!',icon='C:/zhaoshuang/app/icon.ico',duration='2000')
elif resmax=='noface':
toast('Deepfake Detect', 'Not detect face',icon='C:/zhaoshuang/app/icon.ico',duration='2000')
session = None
flag = 1
Comments