File size: 1,267 Bytes
9c91b26
667c6df
9c91b26
d129e85
667c6df
f3a9b48
36ae1e2
9c91b26
36ae1e2
 
 
9c91b26
 
36ae1e2
9c91b26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3a9b48
 
 
9c91b26
 
 
 
cad188d
cce1cb0
36ae1e2
9c91b26
f6e90f5
c7f3283
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from fastai.vision.all import *
import gradio as gr
from PIL import Image, ImageChops, ImageEnhance
import cv2

#Converting the images to ELA format for prediction
def convert_to_ela_image(img_array, quality):
    resaved_filename = 'temp.jpg'
    img = Image.fromarray(img_array)  
    im = img.convert('RGB')
    im.save(resaved_filename, 'JPEG', quality=quality)
    resaved_im = Image.open(resaved_filename)
    
    ela_im = ImageChops.difference(im, resaved_im)
    
    extrema = ela_im.getextrema()
    max_diff = max([ex[1] for ex in extrema])
    if max_diff == 0:
        max_diff = 1
    scale = 255.0 / max_diff
    
    ela_im = ImageEnhance.Brightness(ela_im).enhance(scale)
    ela_im = ela_im.resize((256, 256))
    return ela_im

#Importing the model
learn = load_learner('resnet34_ela_images.pkl')

categories = ('Fake', 'Real')

#Defining the function to get the prediction values
def classify_image(img):
    im = convert_to_ela_image(img, 90)
    pred,idx,probs = learn.predict(im)
    return dict(zip(categories, map(float,probs)))

image = gr.Image()
label = gr.Label()
examples = ['lion_f.png', 'boat_r.jpg', 'potato_f.jpg']

intf = gr.Interface(fn=classify_image, inputs=image, outputs = label, examples = examples)
intf.launch(inline=False)