diff --git a/.gitattributes b/.gitattributes index 7468044478da1f247f117ebb10ce97865c80933a..b134b57addccdd44fa753b99e231f71383170244 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,27 +2,20 @@ *.arrow filter=lfs diff=lfs merge=lfs -text *.bin filter=lfs diff=lfs merge=lfs -text *.bz2 filter=lfs diff=lfs merge=lfs -text -*.ckpt filter=lfs diff=lfs merge=lfs -text *.ftz filter=lfs diff=lfs merge=lfs -text *.gz filter=lfs diff=lfs merge=lfs -text *.h5 filter=lfs diff=lfs merge=lfs -text *.joblib filter=lfs diff=lfs merge=lfs -text *.lfs.* filter=lfs diff=lfs merge=lfs -text -*.mlmodel filter=lfs diff=lfs merge=lfs -text *.model filter=lfs diff=lfs merge=lfs -text *.msgpack filter=lfs diff=lfs merge=lfs -text -*.npy filter=lfs diff=lfs merge=lfs -text -*.npz filter=lfs diff=lfs merge=lfs -text *.onnx filter=lfs diff=lfs merge=lfs -text *.ot filter=lfs diff=lfs merge=lfs -text *.parquet filter=lfs diff=lfs merge=lfs -text *.pb filter=lfs diff=lfs merge=lfs -text -*.pickle filter=lfs diff=lfs merge=lfs -text -*.pkl filter=lfs diff=lfs merge=lfs -text *.pt filter=lfs diff=lfs merge=lfs -text *.pth filter=lfs diff=lfs merge=lfs -text *.rar filter=lfs diff=lfs merge=lfs -text -*.safetensors filter=lfs diff=lfs merge=lfs -text saved_model/**/* filter=lfs diff=lfs merge=lfs -text *.tar.* filter=lfs diff=lfs merge=lfs -text *.tflite filter=lfs diff=lfs merge=lfs -text @@ -30,8 +23,8 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text *.wasm filter=lfs diff=lfs merge=lfs -text *.xz filter=lfs diff=lfs merge=lfs -text *.zip filter=lfs diff=lfs merge=lfs -text -*.zst filter=lfs diff=lfs merge=lfs -text +*.zstandard filter=lfs diff=lfs merge=lfs -text *tfevents* filter=lfs diff=lfs merge=lfs -text -Background_Removal_and_Change/remove_bg/messi.jpg_4.png filter=lfs diff=lfs merge=lfs -text -Background_Removal_and_Change/remove_bg/messi.jpg_5.png filter=lfs diff=lfs merge=lfs -text -Background_Removal_and_Change/remove_bg/messi.jpg_6.png filter=lfs diff=lfs merge=lfs -text +remove_bg/messi.jpg_4.png filter=lfs diff=lfs merge=lfs -text +remove_bg/messi.jpg_5.png filter=lfs diff=lfs merge=lfs -text +remove_bg/messi.jpg_6.png filter=lfs diff=lfs merge=lfs -text diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..13566b81b018ad684f3a35fee301741b2734c8f4 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/Background_Removal_and_Change.iml b/.idea/Background_Removal_and_Change.iml new file mode 100644 index 0000000000000000000000000000000000000000..e82daebc8498e441626b470bdf08582c696557e8 --- /dev/null +++ b/.idea/Background_Removal_and_Change.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/deployment.xml b/.idea/deployment.xml new file mode 100644 index 0000000000000000000000000000000000000000..973c9698d74f030c209975f13a0999f9050d0b17 --- /dev/null +++ b/.idea/deployment.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000000000000000000000000000000000000..55a9fdb5b3c13dad5ff6d77f3579d8baefb68ecc --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,27 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000000000000000000000000000000000000..105ce2da2d6447d11dfe32bfb846c3d5b199fc99 --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..13fecf304bc8719c038c9a91fd31bec2797d5402 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..1ad20121ed2d1528e7b1d2726170ba4e638efabc --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sshConfigs.xml b/.idea/sshConfigs.xml new file mode 100644 index 0000000000000000000000000000000000000000..c4d89a88ded33f0b1cd0078cfa4be5111037b468 --- /dev/null +++ b/.idea/sshConfigs.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..30c80f1c99e518e42d1cd6b96b738fb472b326e0 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/webServers.xml b/.idea/webServers.xml new file mode 100644 index 0000000000000000000000000000000000000000..cce5561611c2c04cc103384926bf4cd67ca6b653 --- /dev/null +++ b/.idea/webServers.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/Procfile b/Procfile new file mode 100644 index 0000000000000000000000000000000000000000..265de8638b4e9ff83dc0a0495bdbf708ce04dc8b --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: sh setup.sh && streamlit run app.py diff --git a/README.md b/README.md index c91ff5a3f7f8ad59ac1cbc6a9a4220006fcc117e..a12b95ae1f0f9638ee15c1c70d954e4be2e5adba 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,9 @@ --- -title: Background Swapping -emoji: 🌖 -colorFrom: red +title: ABC- Automatic Background Change +emoji: ⚡ +colorFrom: yellow colorTo: gray sdk: streamlit -sdk_version: 1.17.0 app_file: app.py pinned: false ---- - -Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference +--- \ No newline at end of file diff --git a/__pycache__/libs.cpython-38.pyc b/__pycache__/libs.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6c0725581b75b98670798c5773d9f6514b426e0d Binary files /dev/null and b/__pycache__/libs.cpython-38.pyc differ diff --git a/__pycache__/libs.cpython-39.pyc b/__pycache__/libs.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..78438d876afe86373b0d599b4ff1b0a829540587 Binary files /dev/null and b/__pycache__/libs.cpython-39.pyc differ diff --git a/__pycache__/metrics.cpython-310.pyc b/__pycache__/metrics.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a62e41d1fbea75540a496d721d9a8aa775ab49f4 Binary files /dev/null and b/__pycache__/metrics.cpython-310.pyc differ diff --git a/__pycache__/metrics.cpython-38.pyc b/__pycache__/metrics.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..14532650679e5fa68779a59aeab18d484b12e8a1 Binary files /dev/null and b/__pycache__/metrics.cpython-38.pyc differ diff --git a/__pycache__/metrics.cpython-39.pyc b/__pycache__/metrics.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..bd9ad883acd7a18040c240dfbfa821c5761d63f4 Binary files /dev/null and b/__pycache__/metrics.cpython-39.pyc differ diff --git a/__pycache__/utils_func.cpython-38.pyc b/__pycache__/utils_func.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..964f220e38a12b8abbba5b4a13cd34949f83f2ac Binary files /dev/null and b/__pycache__/utils_func.cpython-38.pyc differ diff --git a/__pycache__/utils_func.cpython-39.pyc b/__pycache__/utils_func.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4be094aa3d23f26c178068ec62833b2522cb991a Binary files /dev/null and b/__pycache__/utils_func.cpython-39.pyc differ diff --git a/app.py b/app.py new file mode 100644 index 0000000000000000000000000000000000000000..c21d823242778c253aa05c980f195392eb558531 --- /dev/null +++ b/app.py @@ -0,0 +1,94 @@ +from libs import * +from utils_func import create_dir, main_processing + + + +create_dir("tempDir") + + +def load_image(image_file): + img = Image.open(image_file) + return img + + +def streamlit_app(): + detection_model_path = "weight_files/clothes_detection_model.pt" + background_model_path = "weight_files/model.h5" + save_path = "" + image_file = None + st.title("""WELCOME TO MY APP""") + st.subheader("""FOR BACKGROUND REMOVAL AND CHANGE!""") + col1 = None + col2 = None + final_img = None + with st.spinner("[UPLOAD] Image uploading"): + try: + image_file = st.file_uploader('[UPLOAD] Please upload your image:', type=["png", "jpg", "jpeg"]) + time.sleep(1) + except: + print("[ERROR] Sorry, something went wrong!") + pass + # print(type(image_file)) + + if image_file is not None: + st.success("Load image successfully!...") + image = load_image(image_file) + # print(type(image)) + col1, col2, col3 = st.columns(3) + with col1: + st.image(image, caption="Image before processing") + save_path = "tempDir/"+ image_file.name + image.save(save_path) + + + image_path, details = save_path, image_file + + if details is not None: + with col2: + with st.spinner("[PROCESSING] Image processing"): + final_img_path = main_processing(col1, col2, col3, sport_bg_path=stadium_sport_bg_path, swim_bg_path=beach_swim_bg_path, + office_bg_path=office_bg_path, img_path=image_path, name=details.name, + detection_model_path=detection_model_path, + background_model_path=background_model_path) + time.sleep(1) + + with col1: + if final_img_path is not None: + final_img = load_image(final_img_path) + st.image(final_img, caption="Image after processing") + st.balloons() + with col2: + with open(final_img_path, "rb") as file: + st.write('\n') + st.write('\n') + st.write('\n') + st.write('\n') + st.write('\n') + + file_name = save_path.split("/")[-1].split(".")[-2] +"_from_abc" + ".png" + + if st.download_button( + label="Download postprocessing image", + data=file, + file_name= file_name, + mime="image/png" + ): + st.success('[DOWNLOAD] Download sucessfully!') + + + +if __name__ == '__main__': + np.random.seed(42) + tf.random.set_seed(42) + + bg_path = "" + background_model_path = "weight_files/model.h5" + detection_model_path = "weight_files/clothes_detection_model.pt" + + stadium_sport_bg_path = "backgrounds/camnou_stadium.jpg" + beach_swim_bg_path = "backgrounds/beach.jpg" + office_bg_path = "backgrounds/office-bg.jpg" + + image_path = None + + streamlit_app() diff --git a/backgrounds/beach.jpg b/backgrounds/beach.jpg new file mode 100644 index 0000000000000000000000000000000000000000..07de4aee643e3c4506bacf55c5f31b5a0afada3f Binary files /dev/null and b/backgrounds/beach.jpg differ diff --git a/backgrounds/camnou_stadium.jpg b/backgrounds/camnou_stadium.jpg new file mode 100644 index 0000000000000000000000000000000000000000..c9e16cc104ede301a8db6ef552b3d1acaf03dcc2 Binary files /dev/null and b/backgrounds/camnou_stadium.jpg differ diff --git a/backgrounds/office-bg.jpg b/backgrounds/office-bg.jpg new file mode 100644 index 0000000000000000000000000000000000000000..f03c130f04934d00e696143174bf01bc6abaf937 Binary files /dev/null and b/backgrounds/office-bg.jpg differ diff --git a/images/img8.jpg b/images/img8.jpg new file mode 100644 index 0000000000000000000000000000000000000000..666d2e753e412a66804d41f6ec07f26f118820cb Binary files /dev/null and b/images/img8.jpg differ diff --git a/images/messi.jpg b/images/messi.jpg new file mode 100644 index 0000000000000000000000000000000000000000..85bfba127ee96824cb74b40917dae28093d1e69f Binary files /dev/null and b/images/messi.jpg differ diff --git a/images/office.jpg b/images/office.jpg new file mode 100644 index 0000000000000000000000000000000000000000..c4683efef08283c38e6ca8f2dd4ef2fca7425631 Binary files /dev/null and b/images/office.jpg differ diff --git a/libs.py b/libs.py new file mode 100644 index 0000000000000000000000000000000000000000..0bcb0f9c02c2914c336bac33c3058d6577c73f85 --- /dev/null +++ b/libs.py @@ -0,0 +1,10 @@ +import torch +import json +import os +import numpy as np +import cv2 +import tensorflow as tf +import streamlit as st +import time +from PIL import Image +from tensorflow.keras.utils import CustomObjectScope diff --git a/metrics.py b/metrics.py new file mode 100644 index 0000000000000000000000000000000000000000..b3e5d3d3557a15661934e1c109c9baa2d7e57503 --- /dev/null +++ b/metrics.py @@ -0,0 +1,25 @@ +from libs import * + +smooth = 1e-15 + + +def iou(y_true, y_pred): + def f(y_true, y_pred): + intersection = (y_true * y_pred).sum() + union = y_true.sum() + y_pred.sum() - intersection + x = (intersection + 1e-15) / (union + 1e-15) + x = x.astype(np.float32) + return x + return tf.numpy_function(f, [y_true, y_pred], tf.float32) + + + +def dice_coef(y_true, y_pred): + y_true = tf.keras.layers.Flatten()(y_true) + y_pred = tf.keras.layers.Flatten()(y_pred) + intersection = tf.reduce_sum(y_true * y_pred) + return (2. * intersection + smooth) / (tf.reduce_sum(y_true) + tf.reduce_sum(y_pred) + smooth) + + +def dice_loss(y_true, y_pred): + return 1.0 - dice_coef(y_true, y_pred) diff --git a/remove_bg/R.jpg_1.png b/remove_bg/R.jpg_1.png new file mode 100644 index 0000000000000000000000000000000000000000..97ab4bc5404d2e349885717b218818f600543d5b Binary files /dev/null and b/remove_bg/R.jpg_1.png differ diff --git a/remove_bg/R.jpg_2.png b/remove_bg/R.jpg_2.png new file mode 100644 index 0000000000000000000000000000000000000000..c703e718f35776a27c27d97d9c19a86784d6ed49 Binary files /dev/null and b/remove_bg/R.jpg_2.png differ diff --git a/remove_bg/R.jpg_3.png b/remove_bg/R.jpg_3.png new file mode 100644 index 0000000000000000000000000000000000000000..198deff89f97e2a03c1d65f726f368f892d3cb4d Binary files /dev/null and b/remove_bg/R.jpg_3.png differ diff --git a/remove_bg/R.jpg_4.png b/remove_bg/R.jpg_4.png new file mode 100644 index 0000000000000000000000000000000000000000..a4146d9b0ea8b63f658cd810b4d0fd314a428864 Binary files /dev/null and b/remove_bg/R.jpg_4.png differ diff --git a/remove_bg/R.jpg_5.png b/remove_bg/R.jpg_5.png new file mode 100644 index 0000000000000000000000000000000000000000..b5312f1719f03f393c562ab801b38b5684f1118a Binary files /dev/null and b/remove_bg/R.jpg_5.png differ diff --git a/remove_bg/R.jpg_6.png b/remove_bg/R.jpg_6.png new file mode 100644 index 0000000000000000000000000000000000000000..28e03628fe353d6f2a7ad9058194fe79e06cba8a Binary files /dev/null and b/remove_bg/R.jpg_6.png differ diff --git a/remove_bg/beach.jpg_1.png b/remove_bg/beach.jpg_1.png new file mode 100644 index 0000000000000000000000000000000000000000..eb4338c0b71f582952eef2cb6be5277cc97d043a Binary files /dev/null and b/remove_bg/beach.jpg_1.png differ diff --git a/remove_bg/beach.jpg_2.png b/remove_bg/beach.jpg_2.png new file mode 100644 index 0000000000000000000000000000000000000000..29c6c2992fc0fd218773be79703a29fd46ba966f Binary files /dev/null and b/remove_bg/beach.jpg_2.png differ diff --git a/remove_bg/beach.jpg_3.png b/remove_bg/beach.jpg_3.png new file mode 100644 index 0000000000000000000000000000000000000000..a6ba9076ba534e5e545235bd53827ae1bb9be760 Binary files /dev/null and b/remove_bg/beach.jpg_3.png differ diff --git a/remove_bg/beach.jpg_4.png b/remove_bg/beach.jpg_4.png new file mode 100644 index 0000000000000000000000000000000000000000..cb7bb46b2db57098304aa914f3c3906f074d5e20 Binary files /dev/null and b/remove_bg/beach.jpg_4.png differ diff --git a/remove_bg/beach.jpg_5.png b/remove_bg/beach.jpg_5.png new file mode 100644 index 0000000000000000000000000000000000000000..040d00faf4353918f382fd05fda7f4e1066f1595 Binary files /dev/null and b/remove_bg/beach.jpg_5.png differ diff --git a/remove_bg/beach.jpg_6.png b/remove_bg/beach.jpg_6.png new file mode 100644 index 0000000000000000000000000000000000000000..2fd83803c5cc60f486e623a8f75e043596c95eb8 Binary files /dev/null and b/remove_bg/beach.jpg_6.png differ diff --git a/remove_bg/messi.jpg_1.png b/remove_bg/messi.jpg_1.png new file mode 100644 index 0000000000000000000000000000000000000000..c9340586188e77249958083c8450f971e4fc4c2e Binary files /dev/null and b/remove_bg/messi.jpg_1.png differ diff --git a/remove_bg/messi.jpg_2.png b/remove_bg/messi.jpg_2.png new file mode 100644 index 0000000000000000000000000000000000000000..1ac489e4fffa26d45fae1647f934c1d55bb5a762 Binary files /dev/null and b/remove_bg/messi.jpg_2.png differ diff --git a/remove_bg/messi.jpg_3.png b/remove_bg/messi.jpg_3.png new file mode 100644 index 0000000000000000000000000000000000000000..fb5485f07f8261994c1c5560c9fa7770fafb4985 Binary files /dev/null and b/remove_bg/messi.jpg_3.png differ diff --git a/remove_bg/messi.jpg_4.png b/remove_bg/messi.jpg_4.png new file mode 100644 index 0000000000000000000000000000000000000000..36c19f39b1775643a3b0ed19f05a1387258a80cf --- /dev/null +++ b/remove_bg/messi.jpg_4.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:377ef7b1443a9573bfef12b9dddb90d7345eebb7fd58e440a4340ab6481df09e +size 2057239 diff --git a/remove_bg/messi.jpg_5.png b/remove_bg/messi.jpg_5.png new file mode 100644 index 0000000000000000000000000000000000000000..bc2d512599e6fca4808378158a9cf831a0951a4f --- /dev/null +++ b/remove_bg/messi.jpg_5.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:652ab3fd4a66cc60a4d4fe234613a480939713e2487d6d3caac2497e9807de4c +size 1096458 diff --git a/remove_bg/messi.jpg_6.png b/remove_bg/messi.jpg_6.png new file mode 100644 index 0000000000000000000000000000000000000000..e212f7a2496c6bb3448d32ee81d6a631b3979eb5 --- /dev/null +++ b/remove_bg/messi.jpg_6.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d954f831634ff0fe2f22b9ea01fe98dad8891858da42b3e285bd80f1a9b6dc93 +size 1587906 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..3ef8e00426b9cfe51c13e78fac2912794d3b6b0d Binary files /dev/null and b/requirements.txt differ diff --git a/setup.sh b/setup.sh new file mode 100644 index 0000000000000000000000000000000000000000..c8650a8b74a58d9a5f53b185fd711c5668e1cd52 --- /dev/null +++ b/setup.sh @@ -0,0 +1,13 @@ +mkdir -p ~/.streamlit/ + +echo "\ +[general]\n\ +email = \"your-email@domain.com\"\n\ +" > ~/.streamlit/credentials.toml + +echo "\ +[server]\n\ +headless = true\n\ +enableCORS=false\n\ +port = $PORT\n\ +" > ~/.streamlit/config.toml \ No newline at end of file diff --git a/tempDir/R.jpg b/tempDir/R.jpg new file mode 100644 index 0000000000000000000000000000000000000000..24a288d27be9d47ae60094d25cff8d026f664b55 Binary files /dev/null and b/tempDir/R.jpg differ diff --git a/tempDir/beach.jpg b/tempDir/beach.jpg new file mode 100644 index 0000000000000000000000000000000000000000..07de4aee643e3c4506bacf55c5f31b5a0afada3f Binary files /dev/null and b/tempDir/beach.jpg differ diff --git a/tempDir/messi.jpg b/tempDir/messi.jpg new file mode 100644 index 0000000000000000000000000000000000000000..4681edd28b936c40d91afe1d1228b05cf91ebc87 Binary files /dev/null and b/tempDir/messi.jpg differ diff --git a/utils_func.py b/utils_func.py new file mode 100644 index 0000000000000000000000000000000000000000..9ade11b9be1df83a1860075bc4c7d4ea9903299b --- /dev/null +++ b/utils_func.py @@ -0,0 +1,205 @@ +from metrics import dice_loss, dice_coef, iou +from libs import * + + +os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" + +""" Global parameters """ +H = 512 +W = 512 + + +def load_image(image_file): + img = Image.open(image_file) + return img + + +def choose_background(col1, col2, col3): + background_file = None + bg_image = None + with col3: + with st.spinner("[UPLOAD] Background uploading"): + try: + if background_file is None: + try: + background_file = st.file_uploader('[UPLOAD] Please upload your background:', type=["png", "jpg", "jpeg"]) + time.sleep(1) + except: + pass + except: + print("[ERROR] Sorry, something went wrong!") + pass + + if background_file is not None: + with col2: + st.success("Load background successfully!...") + + bg_image = load_image(background_file) + print(type(bg_image)) + save_path = "backgrounds/" + background_file.name + bg_image.save(save_path) + + return bg_image, save_path + + +def create_dir(path): + if not os.path.exists(path): + os.makedirs(path) + + +def check_clothe(model_path, img_path): + # Model + model = torch.hub.load('ultralytics/yolov5', 'custom', model_path) # or yolov5n - yolov5x6, custom + + # Inference + results = model(img_path) + + # Results + new_results = results.pandas().xyxy[0].sort_values("confidence").to_json(orient="records") + new_results = json.loads(new_results) + + classes_in_img = [] + classes_count_dict = {} + item = '' + if len(new_results) != 0: + for i in range(len(new_results)): + classes_in_img.append(new_results[i]['name']) + set_of_classes = set(classes_in_img) + list_of_classes = list(set_of_classes) + + older_value_count = 0 + for idx in range(len(list_of_classes)): + value_count = classes_in_img.count(list_of_classes[idx]) + if value_count > older_value_count: + item = list_of_classes[idx] + + else: + item = None + + return item + + + +def main_processing(col1, col2, col3, sport_bg_path, swim_bg_path, office_bg_path, img_path, name, detection_model_path, background_model_path): + """ Seeding """ + bg_path = None + np.random.seed(42) + tf.random.set_seed(42) + + model_path = detection_model_path + + stadium_sport_bg_path = sport_bg_path + beach_swim_bg_path = swim_bg_path + office_bg_path = office_bg_path + + """ Directory for storing files """ + create_dir("remove_bg") + + st.write('Auto detect or choosing background? ') + + if bg_path is None: + if st.checkbox('Choose background'): + try: + bg_img, save_path = choose_background(col1, col2, col3) + bg_path = save_path + except: + pass + + """ Directory for storing files """ + elif st.checkbox('Automatic background'): + item = check_clothe(model_path, img_path) + if item == 'sport': + bg_path = stadium_sport_bg_path + st.write("Hãy tiếp tục luyện tập TDTT chăm chỉ nhé!...") + if item == 'swim': + bg_path = beach_swim_bg_path + st.write("Thời tiết thế này không đi biển hơi phí nhé!...") + if item == 'office': + bg_path = office_bg_path + st.write("Chơi nhiều roài, đi làm chăm chỉ thuii...") + if item == None: + 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...") + st.warning("Chọn background bạn muốn nhé!") + try: + background_img, save_path = choose_background(col1, col2, col3) + bg_path = save_path + except: + pass + + else: + pass + + else: + pass + + if bg_path is not None: + """ Loading model: DeepLabV3+ """ + with CustomObjectScope({'iou': iou, 'dice_coef': dice_coef, 'dice_loss': dice_loss}): + model = tf.keras.models.load_model(background_model_path) + + """ Read the image """ + image = cv2.imread(img_path, cv2.IMREAD_COLOR) + h, w, _ = image.shape + x = cv2.resize(image, (W, H)) + x = x / 255.0 + x = x.astype(np.float32) + x = np.expand_dims(x, axis=0) + + """ Prediction """ + y = model.predict(x)[0] + y = cv2.resize(y, (w, h)) + y = np.expand_dims(y, axis=-1) + y = y > 0.5 + + photo_mask = y + background_mask = np.abs(1 - y) + cv2.imwrite( + f"remove_bg\\{name}_1.png", + photo_mask * 255) + cv2.imwrite( + f"remove_bg\\{name}_2.png", + background_mask * 255) + + cv2.imwrite( + f"remove_bg\\{name}_3.png", + image * photo_mask) + cv2.imwrite( + f"remove_bg\\{name}_4.png", + image * background_mask) + + bg_img = cv2.imread(bg_path, cv2.IMREAD_COLOR) + print(bg_img.shape) + new_bg_img = cv2.resize(bg_img, (w, h)) + + new_new_bg_img = new_bg_img * background_mask + + cv2.imwrite( + f"remove_bg\\{name}_5.png", + new_new_bg_img) + + final_final_img = new_new_bg_img + image * photo_mask + + final_img_path = f"remove_bg\\{name}_6.png" + cv2.imwrite( + final_img_path, + final_final_img) + + return final_img_path + + +# if __name__ == '__main__': +# """ Seeding """ +# np.random.seed(42) +# tf.random.set_seed(42) +# +# bg_path = "" +# background_model_path = "weight_files\\model.h5" +# detection_model_path = "weight_files\\clothes_detection_model.pt" +# +# stadium_sport_bg_path = "backgrounds\\camnou_stadium.jpg" +# beach_swim_bg_path = "backgrounds\\beach.jpg" +# office_bg_path = "backgrounds\\office-bg.jpg" +# +# img_path = "images\\truong-van-bang-10163832.jpg" +# +# 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) \ No newline at end of file diff --git a/weight_files/clothes_detection_model.pt b/weight_files/clothes_detection_model.pt new file mode 100644 index 0000000000000000000000000000000000000000..5c88c7b8d727b3c3733826d3b563b7e06b582bc4 --- /dev/null +++ b/weight_files/clothes_detection_model.pt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2a66f97acde7f676689cf65dd11175becebc2c35167bdbe47d0e610283eaa605 +size 173184733 diff --git a/weight_files/model.h5 b/weight_files/model.h5 new file mode 100644 index 0000000000000000000000000000000000000000..3b69efe2c9587edc236e706771089f582d1e147d --- /dev/null +++ b/weight_files/model.h5 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:60a1b798e58cd022f78e891e94fa1b1dcbd84ca9d574d43ecfe50b678a31c6d1 +size 215146600