File size: 2,642 Bytes
4bae07e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
## Imported Modules ##
import streamlit as st
from tensorflow import keras
from keras.models import load_model
from keras.utils import load_img, img_to_array
import numpy as np
import cv2
from PIL import Image, ImageOps
import matplotlib.pyplot as plt
from  matplotlib.image import imread 

## Functions ##

@st.cache(allow_output_mutation = True) 
def load_VGG():
    model= load_model('model.h5')
    return model

@st.cache(allow_output_mutation = True) 
def load_Inception():
    model= load_model('inception_v3_model.h5')
    return model

 
def import_and_predict(image_data, model):
    size = (224,224)    
    image = ImageOps.fit(image_data, size, Image.ANTIALIAS)
    image = np.asarray(image)
    img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    img_reshape = img[np.newaxis,...]

    prediction = model.predict(img_reshape)
    
    if prediction[0]>=0.5:
        final_result= 'Real'
    else:
        final_result= 'AI Generated'

    return final_result,prediction[0]


       
## Layout##
rad = st.sidebar.radio("Navigation",["Home","Predict AI/Real"])


if rad=="Home":
    st.title("AI_Image_Classifier")
    st.subheader("By Hardik Pahwa")
    st.markdown("""Differentiating between AI-generated content and real content is crucial in today's digital era.<br> As AI algorithms and deepfake technology advance, it becomes increasingly difficult to discern authenticity.<br> This ability is essential for preserving information integrity, combating misinformation, and maintaining trust in various fields.<br> Reliable tools and techniques are necessary to identify AI-generated content, ensuring transparency and protecting against manipulation.""",True)
    st.markdown("Infact the text  above and image below was also generated using AI :wink:")
    st.image("sample.jpg")
    
elif rad=="Predict AI/Real":
    st.write("""
            # Image Classification
    """)
    v1 = st.radio("Choose Model",["VGG16","Inceptionv3"],index=0)
    with st.spinner("Loading Model..."):
        if v1=="VGG16":
            model = load_VGG()
        else:
            model = load_Inception()
    
        
    file = st.file_uploader("Upload an Image")
    st.write(file)
    st.set_option('deprecation.showfileUploaderEncoding', False)
        
    if file != None:
        image = Image.open(file)
        st.image(file,use_column_width=True)
        final_result= import_and_predict(image,model)[0]
        result = import_and_predict(image,model)[1]
        st.write("The image is :")
        st.write(final_result)
        st.write(result)
        
       
        
            
    else:
        st.write("No file uploaded")