ongkn commited on
Commit
6b67e23
1 Parent(s): 3dd668e

remove unnecessary stuff from app.py

Browse files
Files changed (2) hide show
  1. app.py +1 -60
  2. gradcam.py +4 -0
app.py CHANGED
@@ -2,11 +2,8 @@ import gradio as gr
2
  from transformers import pipeline, ViTForImageClassification, ViTImageProcessor
3
  import numpy as np
4
  from PIL import Image
5
- import cv2 as cv
6
- import dlib
7
  import warnings
8
  import logging
9
- from typing import Optional
10
  from pytorch_grad_cam import run_dff_on_image, GradCAM
11
  from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
12
  from pytorch_grad_cam.utils.image import show_cam_on_image
@@ -15,64 +12,8 @@ from face_grab import FaceGrabber
15
  from gradcam import GradCam
16
  from torchvision import transforms
17
 
18
-
19
  logging.basicConfig(level=logging.INFO)
20
 
21
- def grab_faces(img: np.ndarray) -> Optional[np.ndarray]:
22
- cascades = [
23
- "haarcascade_frontalface_default.xml",
24
- "haarcascade_frontalface_alt.xml",
25
- "haarcascade_frontalface_alt2.xml",
26
- "haarcascade_frontalface_alt_tree.xml"
27
- ]
28
-
29
- detector = dlib.get_frontal_face_detector() # load face detector
30
- predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks_GTX.dat") # load face predictor
31
- mmod = dlib.cnn_face_detection_model_v1("mmod_human_face_detector.dat") # load face detector
32
-
33
- paddingBy = 0.1 # padding by 10%
34
-
35
- gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # convert to grayscale
36
-
37
- detected = None
38
-
39
- if detected is None:
40
- faces = detector(gray) # detect faces
41
- if len(faces) > 0:
42
- detected = faces[0]
43
- detected = (detected.left(), detected.top(), detected.width(), detected.height())
44
- logging.info("Face detected by dlib")
45
-
46
- if detected is None:
47
- faces = mmod(img)
48
- if len(faces) > 0:
49
- detected = faces[0]
50
- detected = (detected.rect.left(), detected.rect.top(), detected.rect.width(), detected.rect.height())
51
- logging.info("Face detected by mmod")
52
-
53
- for cascade in cascades:
54
- cascadeClassifier = cv.CascadeClassifier(cv.data.haarcascades + cascade)
55
- faces = cascadeClassifier.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) # detect faces
56
- if len(faces) > 0:
57
- detected = faces[0]
58
- logging.info(f"Face detected by {cascade}")
59
- break
60
-
61
- if detected is not None: # if face detected
62
- x, y, w, h = detected # grab first face
63
- padW = int(paddingBy * w) # get padding width
64
- padH = int(paddingBy * h) # get padding height
65
- imgH, imgW, _ = img.shape # get image dims
66
- x = max(0, x - padW)
67
- y = max(0, y - padH)
68
- w = min(imgW - x, w + 2 * padW)
69
- h = min(imgH - y, h + 2 * padH)
70
- x = max(0, x - (w - detected[2]) // 2) # center the face horizontally
71
- y = max(0, y - (h - detected[3]) // 2) # center the face vertically
72
- face = img[y:y+h, x:x+w] # crop face
73
- return face
74
-
75
- return None
76
 
77
  model = ViTForImageClassification.from_pretrained("ongkn/attraction-classifier")
78
  processor = ViTImageProcessor.from_pretrained("ongkn/attraction-classifier")
@@ -92,7 +33,6 @@ def classify_image(input):
92
  if face is None:
93
  return "No face detected", 0, input
94
  face = Image.fromarray(face)
95
- imgTensor = transforms.ToTensor()(face)
96
  tensor = transforms.ToTensor()(face)
97
  dffImage = run_dff_on_image(model=model,
98
  target_layer=targetLayerDff,
@@ -122,4 +62,5 @@ iface = gr.Interface(
122
  title="Attraction Classifier - subjective",
123
  description=f"Takes in a (224, 224) image and outputs an attraction class: {'pos', 'neg'}, along with a GradCam/DFF explanation. Face detection, cropping, and resizing are done internally. Uploaded images are not stored by us, but may be stored by HF. Refer to their privacy policy for details."
124
  )
 
125
  iface.launch()
 
2
  from transformers import pipeline, ViTForImageClassification, ViTImageProcessor
3
  import numpy as np
4
  from PIL import Image
 
 
5
  import warnings
6
  import logging
 
7
  from pytorch_grad_cam import run_dff_on_image, GradCAM
8
  from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
9
  from pytorch_grad_cam.utils.image import show_cam_on_image
 
12
  from gradcam import GradCam
13
  from torchvision import transforms
14
 
 
15
  logging.basicConfig(level=logging.INFO)
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  model = ViTForImageClassification.from_pretrained("ongkn/attraction-classifier")
19
  processor = ViTImageProcessor.from_pretrained("ongkn/attraction-classifier")
 
33
  if face is None:
34
  return "No face detected", 0, input
35
  face = Image.fromarray(face)
 
36
  tensor = transforms.ToTensor()(face)
37
  dffImage = run_dff_on_image(model=model,
38
  target_layer=targetLayerDff,
 
62
  title="Attraction Classifier - subjective",
63
  description=f"Takes in a (224, 224) image and outputs an attraction class: {'pos', 'neg'}, along with a GradCam/DFF explanation. Face detection, cropping, and resizing are done internally. Uploaded images are not stored by us, but may be stored by HF. Refer to their privacy policy for details."
64
  )
65
+
66
  iface.launch()
gradcam.py CHANGED
@@ -21,6 +21,8 @@ warnings.filterwarnings("ignore")
21
 
22
  logging.basicConfig(level=logging.INFO)
23
 
 
 
24
  class HuggingfaceToTensorModelWrapper(torch.nn.Module):
25
  def __init__(self, model):
26
  super(HuggingfaceToTensorModelWrapper, self).__init__()
@@ -29,6 +31,8 @@ class HuggingfaceToTensorModelWrapper(torch.nn.Module):
29
  def forward(self, x):
30
  return self.model(x).logits
31
 
 
 
32
  class GradCam():
33
  def __init__(self):
34
  pass
 
21
 
22
  logging.basicConfig(level=logging.INFO)
23
 
24
+
25
+
26
  class HuggingfaceToTensorModelWrapper(torch.nn.Module):
27
  def __init__(self, model):
28
  super(HuggingfaceToTensorModelWrapper, self).__init__()
 
31
  def forward(self, x):
32
  return self.model(x).logits
33
 
34
+
35
+
36
  class GradCam():
37
  def __init__(self):
38
  pass