Upload 11 files
Browse files- .gitattributes +1 -0
- image_classifier.ipynb +0 -0
- image_classify.keras +3 -0
- main.py +64 -0
- model.py +22 -0
- requirements.txt +0 -0
- static/GantMan_0B31E09D-12C5-465A-9711-2AA32E31DB61.jpg +0 -0
- templates/index.html +54 -0
- web scrap scripts/ai_scrap.py +64 -0
- web scrap scripts/ai_script2.py +82 -0
- web scrap scripts/rename.py +17 -0
- web scrap scripts/resize.py +16 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
image_classify.keras filter=lfs diff=lfs merge=lfs -text
|
image_classifier.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
image_classify.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ae3afde9171a7b7f2fbcbee9cb2c3e7ac183308190a5f29c6f1d826e3275d745
|
| 3 |
+
size 574861816
|
main.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from flask import Flask, flash, request, redirect, url_for,render_template
|
| 3 |
+
from werkzeug.utils import secure_filename
|
| 4 |
+
from model import check
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
UPLOAD_FOLDER = 'static'
|
| 9 |
+
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
|
| 10 |
+
|
| 11 |
+
app = Flask(__name__)
|
| 12 |
+
app.secret_key = "secret key"
|
| 13 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 14 |
+
|
| 15 |
+
def allowed_file(filename):
|
| 16 |
+
return '.' in filename and \
|
| 17 |
+
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 18 |
+
|
| 19 |
+
@app.route("/")
|
| 20 |
+
def home():
|
| 21 |
+
static_folder = app.static_folder
|
| 22 |
+
for filename in os.listdir(static_folder):
|
| 23 |
+
file_path = os.path.join(static_folder, filename)
|
| 24 |
+
if os.path.isfile(file_path):
|
| 25 |
+
os.remove(file_path)
|
| 26 |
+
return render_template("index.html")
|
| 27 |
+
|
| 28 |
+
@app.route("/edit", methods = {"GET","POST"})
|
| 29 |
+
def edit():
|
| 30 |
+
if request.method == "POST":
|
| 31 |
+
static_folder = app.static_folder
|
| 32 |
+
for filename in os.listdir(static_folder):
|
| 33 |
+
file_path = os.path.join(static_folder, filename)
|
| 34 |
+
if os.path.isfile(file_path):
|
| 35 |
+
os.remove(file_path)
|
| 36 |
+
if 'file' not in request.files:
|
| 37 |
+
flash('No file part')
|
| 38 |
+
return "error"
|
| 39 |
+
file = request.files['file']
|
| 40 |
+
# If the user does not select a file, the browser submits an
|
| 41 |
+
# empty file without a filename.
|
| 42 |
+
if file.filename == '':
|
| 43 |
+
flash('No selected file')
|
| 44 |
+
return render_template("index.html")
|
| 45 |
+
if file and allowed_file(file.filename):
|
| 46 |
+
filename = secure_filename(file.filename)
|
| 47 |
+
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
| 48 |
+
image = Image.open(file)
|
| 49 |
+
pre = check(image)
|
| 50 |
+
if(pre >= 0.55):
|
| 51 |
+
flash('REAL IMAGE')
|
| 52 |
+
else:
|
| 53 |
+
flash('AI GENERATED IMAGE')
|
| 54 |
+
return render_template("index.html", filename = filename)
|
| 55 |
+
else:
|
| 56 |
+
flash('Allowed file types are: png, jpg, jpeg')
|
| 57 |
+
|
| 58 |
+
return render_template("index.html")
|
| 59 |
+
|
| 60 |
+
@app.route("/display/<filename>")
|
| 61 |
+
def display_image(filename):
|
| 62 |
+
return redirect(url_for('static', filename = filename), code = 301)
|
| 63 |
+
|
| 64 |
+
app.run(debug=True)
|
model.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import numpy as np
|
| 3 |
+
from tensorflow import keras
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def check(image):
|
| 7 |
+
# Load the image
|
| 8 |
+
|
| 9 |
+
# Preprocess the image
|
| 10 |
+
image = image.resize((300, 300)) # Resize the image to the desired dimensions
|
| 11 |
+
image = np.array(image) # Convert the image to a numpy array
|
| 12 |
+
image = image.astype('float32') / 255.0 # Normalize pixel values between 0 and 1
|
| 13 |
+
|
| 14 |
+
# Expand dimensions and create a batch
|
| 15 |
+
image = np.expand_dims(image, axis=0)
|
| 16 |
+
|
| 17 |
+
model = keras.models.load_model('.\\image_classify.keras')
|
| 18 |
+
|
| 19 |
+
# Make predictions
|
| 20 |
+
predictions = model.predict(image)
|
| 21 |
+
|
| 22 |
+
return predictions
|
requirements.txt
ADDED
|
Binary file (2.5 kB). View file
|
|
|
static/GantMan_0B31E09D-12C5-465A-9711-2AA32E31DB61.jpg
ADDED
|
templates/index.html
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="en" data-bs-theme="dark">
|
| 3 |
+
|
| 4 |
+
<head>
|
| 5 |
+
<meta charset="utf-8">
|
| 6 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 7 |
+
<title>Image Classifier</title>
|
| 8 |
+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"
|
| 9 |
+
integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
|
| 10 |
+
|
| 11 |
+
</head>
|
| 12 |
+
|
| 13 |
+
<body>
|
| 14 |
+
|
| 15 |
+
<center>
|
| 16 |
+
<div class="container my-4">
|
| 17 |
+
<h1 class="fs-2 text"> Image Classifier - Is your image AI generated?</h1>
|
| 18 |
+
<form action="/edit" method="post" enctype="multipart/form-data">
|
| 19 |
+
<div class="mb-3">
|
| 20 |
+
<label for="formFile" class="form-label">Select Image to classify</label>
|
| 21 |
+
<input class="form-control" type="file" name="file" id="formFile">
|
| 22 |
+
</div>
|
| 23 |
+
|
| 24 |
+
<button type="submit" class="btn btn-success">Submit</button>
|
| 25 |
+
</form>
|
| 26 |
+
</div>
|
| 27 |
+
|
| 28 |
+
<div>
|
| 29 |
+
{% if filename %}
|
| 30 |
+
<div style="padding: 20px;">
|
| 31 |
+
<img src="{{url_for('display_image', filename=filename)}} " style="height: 30%; width: 30%;">
|
| 32 |
+
</div>
|
| 33 |
+
{% endif %}
|
| 34 |
+
<p>
|
| 35 |
+
<h4>
|
| 36 |
+
{% with messages = get_flashed_messages() %}
|
| 37 |
+
{% if messages %}
|
| 38 |
+
<ul class=flashes>
|
| 39 |
+
{% for message in messages %}
|
| 40 |
+
<li>{{ message }}</li>
|
| 41 |
+
{% endfor %}
|
| 42 |
+
</ul>
|
| 43 |
+
{% endif %}
|
| 44 |
+
{% endwith %}
|
| 45 |
+
</h4>
|
| 46 |
+
</p>
|
| 47 |
+
</div>
|
| 48 |
+
</center>
|
| 49 |
+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"
|
| 50 |
+
integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz"
|
| 51 |
+
crossorigin="anonymous"></script>
|
| 52 |
+
</body>
|
| 53 |
+
|
| 54 |
+
</html>
|
web scrap scripts/ai_scrap.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import bs4
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from selenium.webdriver.chrome.service import Service
|
| 4 |
+
from selenium import webdriver
|
| 5 |
+
import requests
|
| 6 |
+
import os
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
urlW = "https://impossibleimages.ai/images/"
|
| 10 |
+
PATH = r"C:\Users\anujg\Downloads\Programs\chromedriver_win32\chromedriver.exe"
|
| 11 |
+
service = Service(PATH)
|
| 12 |
+
options = webdriver.ChromeOptions()
|
| 13 |
+
|
| 14 |
+
options.add_argument('--no-sandbox')
|
| 15 |
+
options.add_argument('--headless')
|
| 16 |
+
options.add_argument('--disable-gpu')
|
| 17 |
+
options.add_argument('--disable-dev-shm-usage')
|
| 18 |
+
options.add_argument("--window-size=1920,1080")
|
| 19 |
+
|
| 20 |
+
driver = webdriver.Chrome(service=service, options=options)
|
| 21 |
+
# driver.get(urlW)
|
| 22 |
+
|
| 23 |
+
# os.makedirs('ai-generated-images')
|
| 24 |
+
# time.sleep(2)
|
| 25 |
+
def dloadimg(url, name):
|
| 26 |
+
# write image to file
|
| 27 |
+
reponse = requests.get(url)
|
| 28 |
+
if reponse.status_code==200:
|
| 29 |
+
with open(os.path.join('ai2',name+".jpg"), 'wb') as file:
|
| 30 |
+
file.write(reponse.content)
|
| 31 |
+
|
| 32 |
+
def section_length(driver):
|
| 33 |
+
fig_xpath = '//*[@id="page"]/main/section/div/div/div[2]/article'
|
| 34 |
+
figures = driver.find_elements(by="xpath", value=fig_xpath)
|
| 35 |
+
return len(figures)
|
| 36 |
+
|
| 37 |
+
def dload(urlW,name):
|
| 38 |
+
driver.get(urlW)
|
| 39 |
+
driver.find_element(by="id", value="CybotCookiebotDialogBodyLevelButtonAccept").click()
|
| 40 |
+
for j in range(30):
|
| 41 |
+
j = j + 1
|
| 42 |
+
time.sleep(15)
|
| 43 |
+
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
|
| 44 |
+
time.sleep(5)
|
| 45 |
+
num_img = section_length(driver)
|
| 46 |
+
print(num_img)
|
| 47 |
+
for i in range(1, num_img+1):
|
| 48 |
+
|
| 49 |
+
ImageXPath = """//*[@id="page"]/main/section/div/div/div[2]/article[%s]/div/img"""%(i)
|
| 50 |
+
# ImageElement = driver.find_element_by_xpath(ImageXPath)
|
| 51 |
+
ImageElement = driver.find_element(by="xpath", value=ImageXPath)
|
| 52 |
+
ImageURL = ImageElement.get_attribute("src")
|
| 53 |
+
print(ImageURL)
|
| 54 |
+
try:
|
| 55 |
+
dloadimg(ImageURL, name+str(i))
|
| 56 |
+
print("Downloaded element %s out of %s total." % (i, num_img))
|
| 57 |
+
except:
|
| 58 |
+
print("Couldn't download an image %s, continuing downloading the next one"%(i))
|
| 59 |
+
|
| 60 |
+
dload(urlW,"AI_Img_imp")
|
| 61 |
+
|
| 62 |
+
# time.sleep(100)
|
| 63 |
+
driver.close()
|
| 64 |
+
|
web scrap scripts/ai_script2.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import bs4
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from selenium.webdriver.chrome.service import Service
|
| 4 |
+
from selenium import webdriver
|
| 5 |
+
import requests
|
| 6 |
+
import os
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
PATH = r"C:\Users\anujg\Downloads\Programs\chromedriver_win32\chromedriver.exe"
|
| 11 |
+
service = Service(PATH)
|
| 12 |
+
options = webdriver.ChromeOptions()
|
| 13 |
+
|
| 14 |
+
options.add_argument('--no-sandbox')
|
| 15 |
+
options.add_argument('--headless')
|
| 16 |
+
options.add_argument('--disable-gpu')
|
| 17 |
+
options.add_argument('--disable-dev-shm-usage')
|
| 18 |
+
options.add_argument("--window-size=1920,10800")
|
| 19 |
+
# Made window size so much that 50 images can fit in one window so that all images will be loaded
|
| 20 |
+
|
| 21 |
+
driver = webdriver.Chrome(service=service, options=options)
|
| 22 |
+
# driver.get(urlW)
|
| 23 |
+
# os.makedirs('ai-generated-images')
|
| 24 |
+
|
| 25 |
+
def dloadimg(url, name):
|
| 26 |
+
# write image to file
|
| 27 |
+
reponse = requests.get(url)
|
| 28 |
+
if reponse.status_code==200:
|
| 29 |
+
with open(os.path.join('resized',name+".jpg"), 'wb') as file:
|
| 30 |
+
# # file = file.resize((400, 400))
|
| 31 |
+
file.write(reponse.content)
|
| 32 |
+
# try:
|
| 33 |
+
# image = Image.open(reponse.content)
|
| 34 |
+
# image = image.resize((400, 400))
|
| 35 |
+
# image.save(file)
|
| 36 |
+
# except Exception as e:
|
| 37 |
+
# print(e)
|
| 38 |
+
|
| 39 |
+
def section_length(driver):
|
| 40 |
+
fig_xpath = '//*[@id="main"]/div[3]/div/div[2]/section/figure'
|
| 41 |
+
figures = driver.find_elements(by="xpath", value=fig_xpath)
|
| 42 |
+
return len(figures)
|
| 43 |
+
|
| 44 |
+
def dload(urlW,name,pc):
|
| 45 |
+
driver.get(urlW)
|
| 46 |
+
num_img = section_length(driver)
|
| 47 |
+
print(num_img)
|
| 48 |
+
for i in range(1, num_img+1):
|
| 49 |
+
ImageXPath = """//*[@id="main"]/div[3]/div/div[2]/section/figure[%s]/div/a/img"""%(i)
|
| 50 |
+
# ImageElement = driver.find_element_by_xpath(ImageXPath)
|
| 51 |
+
|
| 52 |
+
timeStarted = time.time()
|
| 53 |
+
while True:
|
| 54 |
+
try:
|
| 55 |
+
ImageElement = driver.find_element(by="xpath", value=ImageXPath)
|
| 56 |
+
ImageURL = ImageElement.get_attribute("src")
|
| 57 |
+
|
| 58 |
+
if ImageURL == "https://freepik.cdnpk.net/img/1px.png":
|
| 59 |
+
currentTime = time.time()
|
| 60 |
+
if currentTime - timeStarted > 10:
|
| 61 |
+
print("Timeout! moving to next image")
|
| 62 |
+
break
|
| 63 |
+
elif ImageURL != "https://freepik.cdnpk.net/img/1px.png":
|
| 64 |
+
dloadimg(ImageURL, name+"_pg_no."+str(pc)+"img"+str(i))
|
| 65 |
+
print("Downloaded image no. %s of page no. %s." %(i,pc))
|
| 66 |
+
break
|
| 67 |
+
else:
|
| 68 |
+
break
|
| 69 |
+
except:
|
| 70 |
+
print("Couldn't download an image %s, continuing downloading the next one"%(i))
|
| 71 |
+
break
|
| 72 |
+
|
| 73 |
+
pgcnt = 1
|
| 74 |
+
while pgcnt<23:
|
| 75 |
+
urlW = "https://www.freepik.com/photos/ai-generated/%s"%(pgcnt)
|
| 76 |
+
dload(urlW,"AI_Img",pgcnt)
|
| 77 |
+
pgcnt = pgcnt + 1
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
driver.close()
|
web scrap scripts/rename.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import PIL.Image
|
| 3 |
+
|
| 4 |
+
def rename_imgs(org_folder, renamed_folder, name):
|
| 5 |
+
i = 1
|
| 6 |
+
for file in os.listdir(org_folder):
|
| 7 |
+
img = PIL.Image.open(os.path.join(org_folder, file))
|
| 8 |
+
# img = img.resize((300,300))
|
| 9 |
+
# if img.mode == "RGBA":
|
| 10 |
+
# img = img.convert("RGB")
|
| 11 |
+
img.save(os.path.join(renamed_folder, name+str(i)+".jpg"))
|
| 12 |
+
i += 1
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
if __name__ == "__main__":
|
| 16 |
+
rename_imgs("real_small", "Real", "real_train")
|
| 17 |
+
rename_imgs("ai_small", "AI generated", "ai_train")
|
web scrap scripts/resize.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import PIL.Image
|
| 3 |
+
|
| 4 |
+
def resize_imgs(org_folder, resized_folder, name):
|
| 5 |
+
i = 1
|
| 6 |
+
for file in os.listdir(org_folder):
|
| 7 |
+
img = PIL.Image.open(os.path.join(org_folder, file))
|
| 8 |
+
img = img.resize((300,300))
|
| 9 |
+
if img.mode == "RGBA":
|
| 10 |
+
img = img.convert("RGB")
|
| 11 |
+
img.save(os.path.join(resized_folder, name+str(i)+".jpg"))
|
| 12 |
+
i += 1
|
| 13 |
+
|
| 14 |
+
if __name__ == "__main__":
|
| 15 |
+
resize_imgs("ai_imgs", "ai_resized", "ai_imgs_")
|
| 16 |
+
resize_imgs("real_imgs", "real_resized", "real_imgs_")
|