Spaces:
Runtime error
Runtime error
ai.vietqt
commited on
Commit
·
af926f3
1
Parent(s):
76122f1
add application
Browse files- .idea/.gitignore +8 -0
- .idea/Background_Removal_and_Change.iml +10 -0
- .idea/deployment.xml +14 -0
- .idea/inspectionProfiles/Project_Default.xml +27 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/misc.xml +4 -0
- .idea/modules.xml +8 -0
- .idea/sshConfigs.xml +8 -0
- .idea/vcs.xml +9 -0
- .idea/webServers.xml +14 -0
- README.md +1 -13
- __pycache__/libs.cpython-39.pyc +0 -0
- __pycache__/metrics.cpython-310.pyc +0 -0
- __pycache__/metrics.cpython-39.pyc +0 -0
- __pycache__/utils_func.cpython-39.pyc +0 -0
- app.py +92 -0
- backgrounds/beach.jpg +0 -0
- backgrounds/camnou_stadium.jpg +0 -0
- backgrounds/office-bg.jpg +0 -0
- images/img8.jpg +0 -0
- images/messi.jpg +0 -0
- images/office.jpg +0 -0
- libs.py +10 -0
- metrics.py +23 -0
- remove_bg/R.jpg_1.png +0 -0
- remove_bg/R.jpg_2.png +0 -0
- remove_bg/R.jpg_3.png +0 -0
- remove_bg/R.jpg_4.png +0 -0
- remove_bg/R.jpg_5.png +0 -0
- remove_bg/R.jpg_6.png +0 -0
- requirements.txt +0 -0
- run_app.py +11 -0
- tempDir/R.jpg +0 -0
- utils_func.py +205 -0
- weight_files/clothes_detection_model.pt +3 -0
- weight_files/model.h5 +3 -0
.idea/.gitignore
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default ignored files
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
| 4 |
+
# Editor-based HTTP Client requests
|
| 5 |
+
/httpRequests/
|
| 6 |
+
# Datasource local storage ignored files
|
| 7 |
+
/dataSources/
|
| 8 |
+
/dataSources.local.xml
|
.idea/Background_Removal_and_Change.iml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<module type="PYTHON_MODULE" version="4">
|
| 3 |
+
<component name="NewModuleRootManager">
|
| 4 |
+
<content url="file://$MODULE_DIR$">
|
| 5 |
+
<excludeFolder url="file://$MODULE_DIR$/venvme" />
|
| 6 |
+
</content>
|
| 7 |
+
<orderEntry type="jdk" jdkName="Python 3.9 (pythonProject)" jdkType="Python SDK" />
|
| 8 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
| 9 |
+
</component>
|
| 10 |
+
</module>
|
.idea/deployment.xml
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="PublishConfigData" serverName="HAHALOLO_SERVER" remoteFilesAllowedToDisappearOnAutoupload="false">
|
| 4 |
+
<serverData>
|
| 5 |
+
<paths name="HAHALOLO_SERVER">
|
| 6 |
+
<serverdata>
|
| 7 |
+
<mappings>
|
| 8 |
+
<mapping local="$PROJECT_DIR$" web="/" />
|
| 9 |
+
</mappings>
|
| 10 |
+
</serverdata>
|
| 11 |
+
</paths>
|
| 12 |
+
</serverData>
|
| 13 |
+
</component>
|
| 14 |
+
</project>
|
.idea/inspectionProfiles/Project_Default.xml
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<profile version="1.0">
|
| 3 |
+
<option name="myName" value="Project Default" />
|
| 4 |
+
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
| 5 |
+
<option name="ignoredPackages">
|
| 6 |
+
<value>
|
| 7 |
+
<list size="14">
|
| 8 |
+
<item index="0" class="java.lang.String" itemvalue="scipy" />
|
| 9 |
+
<item index="1" class="java.lang.String" itemvalue="opencv-python" />
|
| 10 |
+
<item index="2" class="java.lang.String" itemvalue="Pillow" />
|
| 11 |
+
<item index="3" class="java.lang.String" itemvalue="pandas" />
|
| 12 |
+
<item index="4" class="java.lang.String" itemvalue="datetime" />
|
| 13 |
+
<item index="5" class="java.lang.String" itemvalue="tensorflow" />
|
| 14 |
+
<item index="6" class="java.lang.String" itemvalue="retina-face" />
|
| 15 |
+
<item index="7" class="java.lang.String" itemvalue="gdown" />
|
| 16 |
+
<item index="8" class="java.lang.String" itemvalue="matplotlib" />
|
| 17 |
+
<item index="9" class="java.lang.String" itemvalue="deepface" />
|
| 18 |
+
<item index="10" class="java.lang.String" itemvalue="cmake" />
|
| 19 |
+
<item index="11" class="java.lang.String" itemvalue="keras" />
|
| 20 |
+
<item index="12" class="java.lang.String" itemvalue="requests" />
|
| 21 |
+
<item index="13" class="java.lang.String" itemvalue="numpy" />
|
| 22 |
+
</list>
|
| 23 |
+
</value>
|
| 24 |
+
</option>
|
| 25 |
+
</inspection_tool>
|
| 26 |
+
</profile>
|
| 27 |
+
</component>
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<settings>
|
| 3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
| 4 |
+
<version value="1.0" />
|
| 5 |
+
</settings>
|
| 6 |
+
</component>
|
.idea/misc.xml
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (pythonProject)" project-jdk-type="Python SDK" />
|
| 4 |
+
</project>
|
.idea/modules.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectModuleManager">
|
| 4 |
+
<modules>
|
| 5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/Background_Removal_and_Change.iml" filepath="$PROJECT_DIR$/.idea/Background_Removal_and_Change.iml" />
|
| 6 |
+
</modules>
|
| 7 |
+
</component>
|
| 8 |
+
</project>
|
.idea/sshConfigs.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="SshConfigs">
|
| 4 |
+
<configs>
|
| 5 |
+
<sshConfig authType="PASSWORD" host="10.10.15.87" id="8b67aa76-bd20-4263-bc45-270bf4ea8b6e" port="22" nameFormat="DESCRIPTIVE" username="root" useOpenSSHConfig="true" />
|
| 6 |
+
</configs>
|
| 7 |
+
</component>
|
| 8 |
+
</project>
|
.idea/vcs.xml
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="VcsDirectoryMappings">
|
| 4 |
+
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
| 5 |
+
<mapping directory="$PROJECT_DIR$/Background_removal_and_change" vcs="Git" />
|
| 6 |
+
<mapping directory="$PROJECT_DIR$/Remove-Photo-Background-using-TensorFlow" vcs="Git" />
|
| 7 |
+
<mapping directory="$PROJECT_DIR$/yolov5" vcs="Git" />
|
| 8 |
+
</component>
|
| 9 |
+
</project>
|
.idea/webServers.xml
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="WebServers">
|
| 4 |
+
<option name="servers">
|
| 5 |
+
<webServer id="7c3f60f5-5354-4ef4-a283-182d579b3fe7" name="HAHALOLO_SERVER">
|
| 6 |
+
<fileTransfer rootFolder="/storage/test" accessType="SFTP" host="10.10.15.87" port="22" sshConfigId="8b67aa76-bd20-4263-bc45-270bf4ea8b6e" sshConfig="[email protected]:22 password">
|
| 7 |
+
<advancedOptions>
|
| 8 |
+
<advancedOptions dataProtectionLevel="Private" keepAliveTimeout="0" passiveMode="true" shareSSLContext="true" />
|
| 9 |
+
</advancedOptions>
|
| 10 |
+
</fileTransfer>
|
| 11 |
+
</webServer>
|
| 12 |
+
</option>
|
| 13 |
+
</component>
|
| 14 |
+
</project>
|
README.md
CHANGED
|
@@ -1,13 +1 @@
|
|
| 1 |
-
|
| 2 |
-
title: Background Removal And Change
|
| 3 |
-
emoji: 🐢
|
| 4 |
-
colorFrom: pink
|
| 5 |
-
colorTo: red
|
| 6 |
-
sdk: streamlit
|
| 7 |
-
sdk_version: 1.10.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
license: other
|
| 11 |
-
---
|
| 12 |
-
|
| 13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
+
# Background_removal_and_change
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__pycache__/libs.cpython-39.pyc
ADDED
|
Binary file (408 Bytes). View file
|
|
|
__pycache__/metrics.cpython-310.pyc
ADDED
|
Binary file (1.03 kB). View file
|
|
|
__pycache__/metrics.cpython-39.pyc
ADDED
|
Binary file (1.02 kB). View file
|
|
|
__pycache__/utils_func.cpython-39.pyc
ADDED
|
Binary file (4.16 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from libs import *
|
| 2 |
+
from utils_func import create_dir, main_processing
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
create_dir("tempDir")
|
| 7 |
+
|
| 8 |
+
def load_image(image_file):
|
| 9 |
+
img = Image.open(image_file)
|
| 10 |
+
return img
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def streamlit_app():
|
| 14 |
+
detection_model_path = "weight_files/clothes_detection_model.pt"
|
| 15 |
+
background_model_path = "weight_files/model.h5"
|
| 16 |
+
# save_path = ""
|
| 17 |
+
image_file = None
|
| 18 |
+
st.title("""WELCOME TO MY APP""")
|
| 19 |
+
st.subheader("""FOR BACKGROUND REMOVAL AND CHANGE!""")
|
| 20 |
+
col1 = None
|
| 21 |
+
col2 = None
|
| 22 |
+
final_img = None
|
| 23 |
+
with st.spinner("[UPLOAD] Image uploading"):
|
| 24 |
+
try:
|
| 25 |
+
image_file = st.file_uploader('[UPLOAD] Please upload your image:', type=["png", "jpg", "jpeg"])
|
| 26 |
+
time.sleep(1)
|
| 27 |
+
except:
|
| 28 |
+
print("[ERROR] Sorry, something went wrong!")
|
| 29 |
+
pass
|
| 30 |
+
# print(type(image_file))
|
| 31 |
+
|
| 32 |
+
if image_file is not None:
|
| 33 |
+
st.success("Load image successfully!...")
|
| 34 |
+
image = load_image(image_file)
|
| 35 |
+
# print(type(image))
|
| 36 |
+
col1, col2, col3 = st.columns(3)
|
| 37 |
+
with col1:
|
| 38 |
+
st.image(image, caption="Image before processing")
|
| 39 |
+
save_path = "tempDir\\"+ image_file.name
|
| 40 |
+
image.save(save_path)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
image_path, details = save_path, image_file
|
| 44 |
+
|
| 45 |
+
if details is not None:
|
| 46 |
+
with col2:
|
| 47 |
+
with st.spinner("[PROCESSING] Image processing"):
|
| 48 |
+
final_img_path = main_processing(col1, col2, col3, sport_bg_path=stadium_sport_bg_path, swim_bg_path=beach_swim_bg_path,
|
| 49 |
+
office_bg_path=office_bg_path, img_path=image_path, name=details.name,
|
| 50 |
+
detection_model_path=detection_model_path,
|
| 51 |
+
background_model_path=background_model_path)
|
| 52 |
+
time.sleep(1)
|
| 53 |
+
|
| 54 |
+
with col1:
|
| 55 |
+
if final_img_path is not None:
|
| 56 |
+
final_img = load_image(final_img_path)
|
| 57 |
+
st.image(final_img, caption="Image after processing")
|
| 58 |
+
st.balloons()
|
| 59 |
+
with col2:
|
| 60 |
+
with open(final_img_path, "rb") as file:
|
| 61 |
+
st.write('\n')
|
| 62 |
+
st.write('\n')
|
| 63 |
+
st.write('\n')
|
| 64 |
+
st.write('\n')
|
| 65 |
+
st.write('\n')
|
| 66 |
+
|
| 67 |
+
file_name = final_img_path.split("\\")[-1].split(".")[-2]
|
| 68 |
+
if st.download_button(
|
| 69 |
+
label="Download postprocessing image",
|
| 70 |
+
data=file,
|
| 71 |
+
file_name= file_name,
|
| 72 |
+
mime="image/png"
|
| 73 |
+
):
|
| 74 |
+
st.success('[DOWNLOAD] Download sucessfully!')
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
if __name__ == '__main__':
|
| 79 |
+
np.random.seed(42)
|
| 80 |
+
tf.random.set_seed(42)
|
| 81 |
+
|
| 82 |
+
bg_path = ""
|
| 83 |
+
background_model_path = "weight_files\\model.h5"
|
| 84 |
+
detection_model_path = "weight_files\\clothes_detection_model.pt"
|
| 85 |
+
|
| 86 |
+
stadium_sport_bg_path = "backgrounds\\camnou_stadium.jpg"
|
| 87 |
+
beach_swim_bg_path = "backgrounds\\beach.jpg"
|
| 88 |
+
office_bg_path = "backgrounds\\office-bg.jpg"
|
| 89 |
+
|
| 90 |
+
image_path = None
|
| 91 |
+
|
| 92 |
+
streamlit_app()
|
backgrounds/beach.jpg
ADDED
|
backgrounds/camnou_stadium.jpg
ADDED
|
backgrounds/office-bg.jpg
ADDED
|
images/img8.jpg
ADDED
|
images/messi.jpg
ADDED
|
images/office.jpg
ADDED
|
libs.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
import streamlit as st
|
| 8 |
+
import time
|
| 9 |
+
from PIL import Image
|
| 10 |
+
from tensorflow.keras.utils import CustomObjectScope
|
metrics.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from tensorflow.keras import backend as K
|
| 5 |
+
|
| 6 |
+
def iou(y_true, y_pred):
|
| 7 |
+
def f(y_true, y_pred):
|
| 8 |
+
intersection = (y_true * y_pred).sum()
|
| 9 |
+
union = y_true.sum() + y_pred.sum() - intersection
|
| 10 |
+
x = (intersection + 1e-15) / (union + 1e-15)
|
| 11 |
+
x = x.astype(np.float32)
|
| 12 |
+
return x
|
| 13 |
+
return tf.numpy_function(f, [y_true, y_pred], tf.float32)
|
| 14 |
+
|
| 15 |
+
smooth = 1e-15
|
| 16 |
+
def dice_coef(y_true, y_pred):
|
| 17 |
+
y_true = tf.keras.layers.Flatten()(y_true)
|
| 18 |
+
y_pred = tf.keras.layers.Flatten()(y_pred)
|
| 19 |
+
intersection = tf.reduce_sum(y_true * y_pred)
|
| 20 |
+
return (2. * intersection + smooth) / (tf.reduce_sum(y_true) + tf.reduce_sum(y_pred) + smooth)
|
| 21 |
+
|
| 22 |
+
def dice_loss(y_true, y_pred):
|
| 23 |
+
return 1.0 - dice_coef(y_true, y_pred)
|
remove_bg/R.jpg_1.png
ADDED
|
remove_bg/R.jpg_2.png
ADDED
|
remove_bg/R.jpg_3.png
ADDED
|
remove_bg/R.jpg_4.png
ADDED
|
remove_bg/R.jpg_5.png
ADDED
|
remove_bg/R.jpg_6.png
ADDED
|
requirements.txt
ADDED
|
Binary file (382 Bytes). View file
|
|
|
run_app.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import uvicorn
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
|
| 4 |
+
app = FastAPI()
|
| 5 |
+
|
| 6 |
+
@app.get("/")
|
| 7 |
+
def home():
|
| 8 |
+
return {"Hello": "World"}
|
| 9 |
+
|
| 10 |
+
if __name__ == "__main__":
|
| 11 |
+
uvicorn.run("hello_world_fastapi:app")
|
tempDir/R.jpg
ADDED
|
utils_func.py
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from metrics import dice_loss, dice_coef, iou
|
| 2 |
+
from libs import *
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
| 6 |
+
|
| 7 |
+
""" Global parameters """
|
| 8 |
+
H = 512
|
| 9 |
+
W = 512
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def load_image(image_file):
|
| 13 |
+
img = Image.open(image_file)
|
| 14 |
+
return img
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def choose_background(col1, col2, col3):
|
| 18 |
+
background_file = None
|
| 19 |
+
bg_image = None
|
| 20 |
+
with col3:
|
| 21 |
+
with st.spinner("[UPLOAD] Background uploading"):
|
| 22 |
+
try:
|
| 23 |
+
if background_file is None:
|
| 24 |
+
try:
|
| 25 |
+
background_file = st.file_uploader('[UPLOAD] Please upload your background:', type=["png", "jpg", "jpeg"])
|
| 26 |
+
time.sleep(1)
|
| 27 |
+
except:
|
| 28 |
+
pass
|
| 29 |
+
except:
|
| 30 |
+
print("[ERROR] Sorry, something went wrong!")
|
| 31 |
+
pass
|
| 32 |
+
|
| 33 |
+
if background_file is not None:
|
| 34 |
+
with col2:
|
| 35 |
+
st.success("Load background successfully!...")
|
| 36 |
+
|
| 37 |
+
bg_image = load_image(background_file)
|
| 38 |
+
print(type(bg_image))
|
| 39 |
+
save_path = "backgrounds\\" + background_file.name
|
| 40 |
+
bg_image.save(save_path)
|
| 41 |
+
|
| 42 |
+
return bg_image, save_path
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def create_dir(path):
|
| 46 |
+
if not os.path.exists(path):
|
| 47 |
+
os.makedirs(path)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def check_clothe(model_path, img_path):
|
| 51 |
+
# Model
|
| 52 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', model_path) # or yolov5n - yolov5x6, custom
|
| 53 |
+
|
| 54 |
+
# Inference
|
| 55 |
+
results = model(img_path)
|
| 56 |
+
|
| 57 |
+
# Results
|
| 58 |
+
new_results = results.pandas().xyxy[0].sort_values("confidence").to_json(orient="records")
|
| 59 |
+
new_results = json.loads(new_results)
|
| 60 |
+
|
| 61 |
+
classes_in_img = []
|
| 62 |
+
classes_count_dict = {}
|
| 63 |
+
item = ''
|
| 64 |
+
if len(new_results) != 0:
|
| 65 |
+
for i in range(len(new_results)):
|
| 66 |
+
classes_in_img.append(new_results[i]['name'])
|
| 67 |
+
set_of_classes = set(classes_in_img)
|
| 68 |
+
list_of_classes = list(set_of_classes)
|
| 69 |
+
|
| 70 |
+
older_value_count = 0
|
| 71 |
+
for idx in range(len(list_of_classes)):
|
| 72 |
+
value_count = classes_in_img.count(list_of_classes[idx])
|
| 73 |
+
if value_count > older_value_count:
|
| 74 |
+
item = list_of_classes[idx]
|
| 75 |
+
|
| 76 |
+
else:
|
| 77 |
+
item = None
|
| 78 |
+
|
| 79 |
+
return item
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def main_processing(col1, col2, col3, sport_bg_path, swim_bg_path, office_bg_path, img_path, name, detection_model_path, background_model_path):
|
| 84 |
+
""" Seeding """
|
| 85 |
+
bg_path = None
|
| 86 |
+
np.random.seed(42)
|
| 87 |
+
tf.random.set_seed(42)
|
| 88 |
+
|
| 89 |
+
model_path = detection_model_path
|
| 90 |
+
|
| 91 |
+
stadium_sport_bg_path = sport_bg_path
|
| 92 |
+
beach_swim_bg_path = swim_bg_path
|
| 93 |
+
office_bg_path = office_bg_path
|
| 94 |
+
|
| 95 |
+
""" Directory for storing files """
|
| 96 |
+
create_dir("remove_bg")
|
| 97 |
+
|
| 98 |
+
st.write('Auto detect or choosing background? ')
|
| 99 |
+
|
| 100 |
+
if bg_path is None:
|
| 101 |
+
if st.checkbox('Choose background'):
|
| 102 |
+
try:
|
| 103 |
+
bg_img, save_path = choose_background(col1, col2, col3)
|
| 104 |
+
bg_path = save_path
|
| 105 |
+
except:
|
| 106 |
+
pass
|
| 107 |
+
|
| 108 |
+
""" Directory for storing files """
|
| 109 |
+
elif st.checkbox('Automatic background'):
|
| 110 |
+
item = check_clothe(model_path, img_path)
|
| 111 |
+
if item == 'sport':
|
| 112 |
+
bg_path = stadium_sport_bg_path
|
| 113 |
+
st.write("Hãy tiếp tục luyện tập TDTT chăm chỉ nhé!...")
|
| 114 |
+
if item == 'swim':
|
| 115 |
+
bg_path = beach_swim_bg_path
|
| 116 |
+
st.write("Thời tiết thế này không đi biển hơi phí nhé!...")
|
| 117 |
+
if item == 'office':
|
| 118 |
+
bg_path = office_bg_path
|
| 119 |
+
st.write("Chơi nhiều roài, đi làm chăm chỉ thuii...")
|
| 120 |
+
if item == None:
|
| 121 |
+
st.warning("Sorry, mô hình chúng tôi không biết bạn đang mặc cái quái gì hết...")
|
| 122 |
+
st.warning("Chọn background bạn muốn nhé!")
|
| 123 |
+
try:
|
| 124 |
+
background_img, save_path = choose_background(col1, col2, col3)
|
| 125 |
+
bg_path = save_path
|
| 126 |
+
except:
|
| 127 |
+
pass
|
| 128 |
+
|
| 129 |
+
else:
|
| 130 |
+
pass
|
| 131 |
+
|
| 132 |
+
else:
|
| 133 |
+
pass
|
| 134 |
+
|
| 135 |
+
if bg_path is not None:
|
| 136 |
+
""" Loading model: DeepLabV3+ """
|
| 137 |
+
with CustomObjectScope({'iou': iou, 'dice_coef': dice_coef, 'dice_loss': dice_loss}):
|
| 138 |
+
model = tf.keras.models.load_model(background_model_path)
|
| 139 |
+
|
| 140 |
+
""" Read the image """
|
| 141 |
+
image = cv2.imread(img_path, cv2.IMREAD_COLOR)
|
| 142 |
+
h, w, _ = image.shape
|
| 143 |
+
x = cv2.resize(image, (W, H))
|
| 144 |
+
x = x / 255.0
|
| 145 |
+
x = x.astype(np.float32)
|
| 146 |
+
x = np.expand_dims(x, axis=0)
|
| 147 |
+
|
| 148 |
+
""" Prediction """
|
| 149 |
+
y = model.predict(x)[0]
|
| 150 |
+
y = cv2.resize(y, (w, h))
|
| 151 |
+
y = np.expand_dims(y, axis=-1)
|
| 152 |
+
y = y > 0.5
|
| 153 |
+
|
| 154 |
+
photo_mask = y
|
| 155 |
+
background_mask = np.abs(1 - y)
|
| 156 |
+
cv2.imwrite(
|
| 157 |
+
f"remove_bg\\{name}_1.png",
|
| 158 |
+
photo_mask * 255)
|
| 159 |
+
cv2.imwrite(
|
| 160 |
+
f"remove_bg\\{name}_2.png",
|
| 161 |
+
background_mask * 255)
|
| 162 |
+
|
| 163 |
+
cv2.imwrite(
|
| 164 |
+
f"remove_bg\\{name}_3.png",
|
| 165 |
+
image * photo_mask)
|
| 166 |
+
cv2.imwrite(
|
| 167 |
+
f"remove_bg\\{name}_4.png",
|
| 168 |
+
image * background_mask)
|
| 169 |
+
|
| 170 |
+
bg_img = cv2.imread(bg_path, cv2.IMREAD_COLOR)
|
| 171 |
+
print(bg_img.shape)
|
| 172 |
+
new_bg_img = cv2.resize(bg_img, (w, h))
|
| 173 |
+
|
| 174 |
+
new_new_bg_img = new_bg_img * background_mask
|
| 175 |
+
|
| 176 |
+
cv2.imwrite(
|
| 177 |
+
f"remove_bg\\{name}_5.png",
|
| 178 |
+
new_new_bg_img)
|
| 179 |
+
|
| 180 |
+
final_final_img = new_new_bg_img + image * photo_mask
|
| 181 |
+
|
| 182 |
+
final_img_path = f"remove_bg\\{name}_6.png"
|
| 183 |
+
cv2.imwrite(
|
| 184 |
+
final_img_path,
|
| 185 |
+
final_final_img)
|
| 186 |
+
|
| 187 |
+
return final_img_path
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
# if __name__ == '__main__':
|
| 191 |
+
# """ Seeding """
|
| 192 |
+
# np.random.seed(42)
|
| 193 |
+
# tf.random.set_seed(42)
|
| 194 |
+
#
|
| 195 |
+
# bg_path = ""
|
| 196 |
+
# background_model_path = "weight_files\\model.h5"
|
| 197 |
+
# detection_model_path = "weight_files\\clothes_detection_model.pt"
|
| 198 |
+
#
|
| 199 |
+
# stadium_sport_bg_path = "backgrounds\\camnou_stadium.jpg"
|
| 200 |
+
# beach_swim_bg_path = "backgrounds\\beach.jpg"
|
| 201 |
+
# office_bg_path = "backgrounds\\office-bg.jpg"
|
| 202 |
+
#
|
| 203 |
+
# img_path = "images\\truong-van-bang-10163832.jpg"
|
| 204 |
+
#
|
| 205 |
+
# main(sport_bg_path=stadium_sport_bg_path, swim_bg_path=beach_swim_bg_path, office_bg_path=office_bg_path, name="a", img_path=img_path, detection_model_path=detection_model_path, background_model_path=background_model_path)
|
weight_files/clothes_detection_model.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2a66f97acde7f676689cf65dd11175becebc2c35167bdbe47d0e610283eaa605
|
| 3 |
+
size 173184733
|
weight_files/model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:60a1b798e58cd022f78e891e94fa1b1dcbd84ca9d574d43ecfe50b678a31c6d1
|
| 3 |
+
size 215146600
|