ElBeh commited on
Commit
0125f5b
·
verified ·
1 Parent(s): f996c82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -3
app.py CHANGED
@@ -1,21 +1,40 @@
1
  import keras
2
  import numpy as np
3
  import streamlit as st
 
4
  from PIL import Image
5
  import os
6
  from huggingface_hub import snapshot_download
7
 
8
- models = ['DDPM','Glide','Latent Diffusion','Palette','Echt','Stable Diffusion','Unseen Fake','VQ Diffusion']
 
9
 
10
- local_model_path = snapshot_download(repo_id="ElBeh/ma_basemodel")
11
- model = keras.models.load_model(local_model_path)
12
 
 
 
 
 
 
 
 
 
 
 
 
13
  def get_prediction(img):
14
  x = np.array(img)
15
  x = np.expand_dims(x, axis=0)
16
  predictions = model.predict(x)
17
  return predictions[0,:]
18
 
 
 
 
 
 
 
19
  st.title("Fake Detection")
20
 
21
  file_name = st.file_uploader("Choose an image...", ['jpg'])
@@ -24,6 +43,10 @@ if file_name is not None:
24
  col1, col2 = st.columns(2)
25
 
26
  image = Image.open(file_name)
 
 
 
 
27
  col1.image(image, use_column_width=True)
28
  predictions = get_prediction(image)
29
 
 
1
  import keras
2
  import numpy as np
3
  import streamlit as st
4
+ import random
5
  from PIL import Image
6
  import os
7
  from huggingface_hub import snapshot_download
8
 
9
+ def random_crop(img, min_size=160, max_size=2048, ratio=5/8):
10
+ width, height = img.size
11
 
12
+ crop_width = random.randint(min_size, min(max_size, width))
13
+ crop_height = int(crop_width * ratio)
14
 
15
+ if crop_height > height:
16
+ crop_height = height
17
+ crop_width = int(crop_height / ratio)
18
+
19
+ left = random.randint(0, width - crop_width)
20
+ top = random.randint(0, height - crop_height)
21
+ right = left + crop_width
22
+ bottom = top + crop_height
23
+
24
+ return img.crop((left, top, right, bottom))
25
+
26
  def get_prediction(img):
27
  x = np.array(img)
28
  x = np.expand_dims(x, axis=0)
29
  predictions = model.predict(x)
30
  return predictions[0,:]
31
 
32
+ models = ['DDPM', 'Glide', 'Latent Diffusion', 'Palette', 'Stable Diffusion', 'VQ Diffusion', 'real', 'unseen_fake']
33
+
34
+ local_model_path = snapshot_download(repo_id="ElBeh/ma_basemodel")
35
+ model = keras.models.load_model(local_model_path)
36
+
37
+
38
  st.title("Fake Detection")
39
 
40
  file_name = st.file_uploader("Choose an image...", ['jpg'])
 
43
  col1, col2 = st.columns(2)
44
 
45
  image = Image.open(file_name)
46
+ if image.size != (200, 200) or image.mode != 'RGB':
47
+ image = random_crop(image)
48
+ image = image.resize((200, 200))
49
+
50
  col1.image(image, use_column_width=True)
51
  predictions = get_prediction(image)
52