Folle, Lukas commited on
Commit
14e27af
1 Parent(s): 64e2ba8

Added first working version of application, NAPSI is dummy.

Browse files
Files changed (6) hide show
  1. .gitignore +2 -0
  2. DummyModel.py +1 -1
  3. app.py +30 -20
  4. assets/hand_example.jpg +0 -0
  5. backend.py +34 -0
  6. requirements.txt +5 -2
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
1
+ .vscode/launch.json
2
+ *.pyc
DummyModel.py CHANGED
@@ -7,7 +7,7 @@ class DummyModel(torch.nn.Module):
7
  super().__init__()
8
 
9
  def forward(self, x):
10
- return 1
11
 
12
  def __call__(self, x):
13
  return self.forward(x)
7
  super().__init__()
8
 
9
  def forward(self, x):
10
+ return torch.softmax(torch.rand(5), 0)
11
 
12
  def __call__(self, x):
13
  return self.forward(x)
app.py CHANGED
@@ -1,26 +1,36 @@
1
- import torch
2
- import os
3
-
4
  import gradio as gr
5
- from huggingface_hub import hf_hub_download
6
 
7
- from DummyModel import DummyModel
 
 
 
8
 
9
 
10
- file_path = hf_hub_download("lfolle/DeepNAPSIModel", "dummy_model.pth",
11
- use_auth_token=os.environ['DeepNAPSIModel'])
12
- model = DummyModel()
13
- model.load_state_dict(torch.load(file_path))
14
 
15
- def predict(data):
16
- return model(data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- iface = gr.Interface(
19
- predict,
20
- title="DeepNAPSI Application",
21
- inputs=gr.Image(),
22
- outputs=gr.Number(label="DeepNAPSI prediction"),
23
- description="",
24
- examples=["assets/hand_example.jpg"],
25
- )
26
- iface.launch()
 
 
 
1
  import gradio as gr
 
2
 
3
+ from backend import Infer
4
+
5
+
6
+ DEBUG = True
7
 
8
 
9
+ infer = Infer(DEBUG)
 
 
 
10
 
11
+ with gr.Blocks(analytics_enabled=False, title="DeepNAPSI Prediction") as demo:
12
+ with gr.Column():
13
+ gr.Markdown("Upload an image of the hand and click **Predict NAPSI** to see the output.")
14
+ with gr.Column():
15
+ image_input = gr.Image()
16
+ image_button = gr.Button("Predict NAPSI")
17
+ outputs = []
18
+ with gr.Row():
19
+ with gr.Column():
20
+ outputs.append(gr.Image())
21
+ outputs.append(gr.Number(label="DeepNAPSI Thumb"))
22
+ with gr.Column():
23
+ outputs.append(gr.Image())
24
+ outputs.append(gr.Number(label="DeepNAPSI Index"))
25
+ with gr.Column():
26
+ outputs.append(gr.Image())
27
+ outputs.append(gr.Number(label="DeepNAPSI Middle"))
28
+ with gr.Column():
29
+ outputs.append(gr.Image())
30
+ outputs.append(gr.Number(label="DeepNAPSI Ring"))
31
+ with gr.Column():
32
+ outputs.append(gr.Image())
33
+ outputs.append(gr.Number(label="DeepNAPSI Pinky"))
34
+ image_button.click(infer.predict, inputs=image_input, outputs=outputs)
35
 
36
+ demo.launch(share=True)
 
 
 
 
 
 
 
 
assets/hand_example.jpg DELETED
Binary file (867 kB)
backend.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import cv2
3
+ import numpy as np
4
+ from huggingface_hub import hf_hub_download
5
+ from nail_detection.main import get_nails
6
+
7
+ from DummyModel import DummyModel
8
+
9
+
10
+ def load_model(DEBUG):
11
+ model = DummyModel()
12
+ if not DEBUG:
13
+ file_path = hf_hub_download("lfolle/DeepNAPSIModel", "dummy_model.pth",
14
+ use_auth_token=os.environ['DeepNAPSIModel'])
15
+ model.load_state_dict(torch.load(file_path))
16
+ return model
17
+
18
+
19
+ class Infer():
20
+ def __init__(self, DEBUG):
21
+ self.model = load_model(DEBUG)
22
+
23
+ def predict(self, data):
24
+ nails = get_nails(cv2.cvtColor(data, cv2.COLOR_RGB2BGR))
25
+ predictions = []
26
+ if nails is None:
27
+ for _ in range(5):
28
+ predictions.append(np.zeros((64, 64, 3)))
29
+ predictions.append(-1)
30
+ else:
31
+ for nail in nails:
32
+ predictions.append(nail)
33
+ predictions.append(int(torch.argmax(self.model(nail))))
34
+ return predictions
requirements.txt CHANGED
@@ -1,2 +1,5 @@
1
- torch
2
- numpy
 
 
 
1
+ cvs2
2
+ numpy
3
+ gradio
4
+ huggingface_hub
5
+ torch