natexcvi commited on
Commit
c7f7ce9
1 Parent(s): 0ca7c58

Compare with two images

Browse files
Files changed (2) hide show
  1. api.py +22 -0
  2. app.py +16 -16
api.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+
3
+
4
+ class FacialSyncService:
5
+ def __init__(self, access_token: str) -> None:
6
+ self.access_token = access_token
7
+
8
+ def similarity(self, img1, img2) -> float:
9
+ res = requests.post(
10
+ "https://natexcvi-facialexpressionsyncservice.hf.space/similarity",
11
+ files={
12
+ # "image1": img_file_buffer.getvalue(),
13
+ "image1": img1,
14
+ # "image2": static_image.getvalue(),
15
+ "image2": img2,
16
+ },
17
+ params={
18
+ "token": self.access_token,
19
+ },
20
+ )
21
+ res.raise_for_status()
22
+ return res.json()["score"]
app.py CHANGED
@@ -3,29 +3,29 @@ import stat
3
  import requests
4
  import streamlit as st
5
 
 
 
6
  access_token = st.experimental_get_query_params().get("token", [""])[0]
7
 
8
  if access_token == "":
9
  st.error("Access denied")
10
  st.stop()
11
 
12
- st.title("🤪 Facial Expression Sync Service")
13
 
14
- static_image = st.file_uploader("Upload image", type=["png", "jpg", "jpeg"])
15
- if static_image is not None:
16
- st.image(static_image, caption="Uploaded image", width=200)
17
 
18
  img_file_buffer = st.camera_input("Take a picture")
19
 
20
- if img_file_buffer is not None and static_image is not None:
21
- res = requests.post(
22
- "https://natexcvi-facialexpressionsyncservice.hf.space/similarity",
23
- files={
24
- "image1": img_file_buffer.getvalue(),
25
- "image2": static_image.getvalue(),
26
- },
27
- params={
28
- "token": access_token,
29
- },
30
- )
31
- st.metric("Similarity", res.json()["score"])
 
3
  import requests
4
  import streamlit as st
5
 
6
+ from api import FacialSyncService
7
+
8
  access_token = st.experimental_get_query_params().get("token", [""])[0]
9
 
10
  if access_token == "":
11
  st.error("Access denied")
12
  st.stop()
13
 
14
+ api = FacialSyncService(access_token)
15
 
16
+ st.title("🤪 Facial Expression Sync Service")
 
 
17
 
18
  img_file_buffer = st.camera_input("Take a picture")
19
 
20
+ st.write("Compare with static images")
21
+ col1, col2 = st.columns(2)
22
+ for col in [col1, col2]:
23
+ with col:
24
+ static_image = st.file_uploader("Upload image", type=["png", "jpg", "jpeg"])
25
+ if static_image is not None:
26
+ st.image(static_image, caption="Uploaded image", width=200)
27
+ if img_file_buffer is not None:
28
+ st.metric(
29
+ "Similarity",
30
+ f"{api.similarity(img_file_buffer.getvalue(), static_image.getvalue()):.2f}",
31
+ )