ozyman commited on
Commit
622fc67
·
1 Parent(s): 435d591

added logging

Browse files
Files changed (2) hide show
  1. app.py +62 -19
  2. requirements.txt +1 -0
app.py CHANGED
@@ -20,20 +20,27 @@ from tddfa.TDDFA import TDDFA
20
  import torch.optim as optim
21
  from DSDG.DUM.models.CDCNs_u import Conv2d_cd, CDCN_u
22
 
 
 
 
 
 
 
23
  import os
24
  os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
25
  os.environ['OMP_NUM_THREADS'] = '4'
26
 
 
27
 
28
  device = torch.device("cpu")
29
  labels = ['Live', 'Spoof']
30
  pix_threshhold = 0.45
31
  dsdg_threshold = 0.003
32
  examples = [
33
- ['examples/1_1_21_2_33_scene_fake.jpg', "DeePixBiS"],
34
- ['examples/frame150_real.jpg', "DeePixBiS"],
35
- ['examples/1_2.avi_125_real.jpg', "DeePixBiS"],
36
- ['examples/1_3.avi_25_fake.jpg', "DeePixBiS"]]
37
  faceClassifier = cv.CascadeClassifier('./DeePixBiS/Classifiers/haarface.xml')
38
  tfms = transforms.Compose([
39
  transforms.ToPILImage(),
@@ -104,7 +111,7 @@ def find_largest_face(faces):
104
  return largest_face
105
 
106
 
107
- def inference(img, btn_res):
108
  grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
109
  faces = faceClassifier.detectMultiScale(
110
  grey, scaleFactor=1.1, minNeighbors=4)
@@ -169,26 +176,62 @@ def inference(img, btn_res):
169
  cv.putText(img_dsdg, label_dsdg, (x, y + h + 30),
170
  cv.FONT_HERSHEY_COMPLEX, 1, color_dsdg)
171
 
172
- return img_deepix, confidences_deepix, img_dsdg, confidences_dsdg
 
 
173
  else:
174
- return img, {}, img, {}
175
-
176
- def open_link():
177
- import webbrowser
178
- webbrowser.open("https://forms.gle/oEum7W2bQQZ8ctAr7")
179
 
180
- # Create a button that opens a link when clicked
181
- button = gr.Button(label="Open link", onclick=open_link)
182
 
 
 
 
183
 
184
- if __name__ == '__main__':
185
- demo = gr.Interface(
186
- fn=inference,
187
- inputs=[gr.Image(source='webcam', shape=None, type='numpy'), button],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  outputs=[
189
  gr.Image(label='DeePixBiS', type='numpy'),
190
  gr.Label(num_top_classes=2, label='DeePixBiS'),
191
  gr.Image(label='DSDG', type='numpy'),
192
- gr.Label(num_top_classes=2, label='DSDG')],
193
- examples=examples).queue(concurrency_count=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  demo.launch(share=False)
 
20
  import torch.optim as optim
21
  from DSDG.DUM.models.CDCNs_u import Conv2d_cd, CDCN_u
22
 
23
+ import io
24
+ import uuid
25
+ import numpy as np
26
+ from PIL import Image
27
+ import boto3
28
+
29
  import os
30
  os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
31
  os.environ['OMP_NUM_THREADS'] = '4'
32
 
33
+ app_version = 'ddn1'
34
 
35
  device = torch.device("cpu")
36
  labels = ['Live', 'Spoof']
37
  pix_threshhold = 0.45
38
  dsdg_threshold = 0.003
39
  examples = [
40
+ ['examples/1_1_21_2_33_scene_fake.jpg'],
41
+ ['examples/frame150_real.jpg'],
42
+ ['examples/1_2.avi_125_real.jpg'],
43
+ ['examples/1_3.avi_25_fake.jpg']]
44
  faceClassifier = cv.CascadeClassifier('./DeePixBiS/Classifiers/haarface.xml')
45
  tfms = transforms.Compose([
46
  transforms.ToPILImage(),
 
111
  return largest_face
112
 
113
 
114
+ def inference(img):
115
  grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
116
  faces = faceClassifier.detectMultiScale(
117
  grey, scaleFactor=1.1, minNeighbors=4)
 
176
  cv.putText(img_dsdg, label_dsdg, (x, y + h + 30),
177
  cv.FONT_HERSHEY_COMPLEX, 1, color_dsdg)
178
 
179
+ cls_deepix, cls_dsdg = [1 if cls_ == 'Real' else 0 for cls_ in [cls_deepix, cls_dsdg]]
180
+
181
+ return img_deepix, confidences_deepix, img_dsdg, confidences_dsdg, cls_deepix, cls_dsdg
182
  else:
183
+ return img, {}, img, {}, None, None
 
 
 
 
184
 
 
 
185
 
186
+ def upload_to_s3(image_array, app_version, *labels):
187
+ folder = 'demo'
188
+ bucket_name = 'livenessng'
189
 
190
+ # Initialize S3 client
191
+ s3 = boto3.client('s3')
192
+
193
+ # Encode labels and app version in image file name
194
+ encoded_labels = '_'.join([str(label) for label in labels])
195
+ random_string = str(uuid.uuid4()).split('-')[-1]
196
+ image_name = f"{folder}/{app_version}/{encoded_labels}_{random_string}.jpg"
197
+
198
+ # Save image as JPEG
199
+ image = Image.fromarray(np.uint8(image_array * 255))
200
+ image_bytes = io.BytesIO()
201
+ image.save(image_bytes, format='JPEG')
202
+ image_bytes.seek(0)
203
+
204
+ # Upload image to S3
205
+ res = s3.upload_fileobj(image_bytes, bucket_name, image_name)
206
+ # Return the S3 URL of the uploaded image
207
+ status = 'Successfully uploaded'
208
+ return status
209
+
210
+
211
+ # interface = .queue(concurrency_count=2)
212
+ demo = gr.Blocks()
213
+
214
+ with demo:
215
+ input_img = gr.Image(source='webcam', shape=None, type='numpy')
216
+ btn_run = gr.Button(value="Run")
217
+ with gr.Column():
218
  outputs=[
219
  gr.Image(label='DeePixBiS', type='numpy'),
220
  gr.Label(num_top_classes=2, label='DeePixBiS'),
221
  gr.Image(label='DSDG', type='numpy'),
222
+ gr.Label(num_top_classes=2, label='DSDG')]
223
+ labels = [gr.Number(visible=False), gr.Number(visible=False)]
224
+ btn_run.click(inference, [input_img], outputs+labels)
225
+
226
+ app_version_block = gr.Textbox(value=app_version, visible=False)
227
+ with gr.Column():
228
+ radio = gr.Radio(
229
+ ["Real", "Spoof", "None"], label="True label", type='index'
230
+ )
231
+ flag = gr.Button(value="Flag")
232
+ status = gr.Textbox()
233
+ flag.click(upload_to_s3, [input_img, app_version_block, radio]+labels, [status], show_progress=True)
234
+
235
+
236
+ if __name__ == '__main__':
237
  demo.launch(share=False)
requirements.txt CHANGED
@@ -9,4 +9,5 @@ scipy
9
  onnx
10
  onnxruntime
11
  cython
 
12
  --index-url=https://download.pytorch.org/whl/cpu --extra-index-url=https://pypi.org/simple
 
9
  onnx
10
  onnxruntime
11
  cython
12
+ boto3
13
  --index-url=https://download.pytorch.org/whl/cpu --extra-index-url=https://pypi.org/simple