Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -828,143 +828,34 @@ def generate_funko_figurines(input_image):
|
|
| 828 |
|
| 829 |
return final_images
|
| 830 |
|
| 831 |
-
def Igenerate_funko_figurines(
|
| 832 |
|
| 833 |
-
predicted_women_hairstyle = None
|
| 834 |
-
predicted_women_haircolor = None
|
| 835 |
-
predicted_gender = None
|
| 836 |
-
predicted_style_label = None
|
| 837 |
-
predicted_color_label = None
|
| 838 |
-
predicted_hairstyle_label = None
|
| 839 |
-
predicted_menhaircolor_label = None
|
| 840 |
-
background_image_paths = None
|
| 841 |
-
|
| 842 |
# Capture video from the webcam for 7 seconds
|
| 843 |
-
|
| 844 |
|
| 845 |
-
|
| 846 |
-
frames =
|
| 847 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 848 |
|
| 849 |
-
# Classify women hairstyle
|
| 850 |
-
women_hairstyle_classifier = IWomenHairStyleClassifier('Data/FunkoSavedModels/WomenHairStyle.pt', ['MediumLength', 'ShortHair', 'SidePlait'])
|
| 851 |
-
|
| 852 |
-
# Classify women hair color
|
| 853 |
-
women_hair_color_classifier = IWomenHairColorClassifier('Data/FunkoSavedModels/WomenHairColor.pt', ['Black', 'Brown', 'Ginger', 'White'])
|
| 854 |
-
|
| 855 |
# Detect and classify gender
|
| 856 |
-
gender_classifier =
|
| 857 |
-
|
| 858 |
-
# Detect and classify beard style
|
| 859 |
-
beard_classifier = IBeardClassifier('Data/FunkoSavedModels/FunkoResnet18BeardStyle.pt', ['Bandholz', 'CleanShave', 'FullGoatee', 'Moustache', 'RapIndustryStandards', 'ShortBeard'])
|
| 860 |
-
|
| 861 |
-
# Detect and classify beard color
|
| 862 |
-
beard_color_classifier = IBeardColorClassifier('Data/FunkoSavedModels/FunkoResnet18BeardColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
|
| 863 |
-
|
| 864 |
-
# Classify hairstyle
|
| 865 |
-
hair_style_classifier = IHairStyleClassifier('Data/FunkoSavedModels/FunkoResnet18HairStyle.pt', ['Afro', 'Bald', 'Puff', 'Spike'])
|
| 866 |
-
|
| 867 |
-
#classify menHairColor
|
| 868 |
-
menhair_color_classifier = IMenHairColorClassifier('Data/FunkoSavedModels/FunkoResnet18MenHairColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
|
| 869 |
-
|
| 870 |
-
def predict_male_features_from_frames(frame):
|
| 871 |
-
return [
|
| 872 |
-
beard_classifier.classify_beard(image=frame,image_type=False),
|
| 873 |
-
beard_color_classifier.classify_beard_color(image=frame,image_type=False),
|
| 874 |
-
hair_style_classifier.classify_hair(image=frame,image_type=False),
|
| 875 |
-
menhair_color_classifier.classify_menHair_color(image=frame,image_type=False)
|
| 876 |
-
]
|
| 877 |
-
|
| 878 |
-
def predict_female_features_from_frames(frame):
|
| 879 |
-
return [
|
| 880 |
-
women_hairstyle_classifier.classify_hairStyle(image=frame,image_type=False),
|
| 881 |
-
women_hair_color_classifier.classify_hairColor(image=frame,image_type=False),
|
| 882 |
-
]
|
| 883 |
-
|
| 884 |
-
def predict_gender_from_frames(frame):
|
| 885 |
-
return gender_classifier.classify_gender(image=frame,image_type=False)
|
| 886 |
-
|
| 887 |
-
|
| 888 |
-
if image_input == True:
|
| 889 |
-
|
| 890 |
-
predicted_women_hairstyle = women_hairstyle_classifier.classify_hairStyle(image="<input as image>",image_type=image_input)
|
| 891 |
-
# Predict women hair color
|
| 892 |
-
predicted_women_haircolor = women_hair_color_classifier.classify_hairColor(image="<input as image>",image_type=image_input)
|
| 893 |
-
# Predict Gender
|
| 894 |
-
predicted_gender = gender_classifier.classify_gender(image="<input as image>",image_type=image_input)
|
| 895 |
-
# Predict beard style
|
| 896 |
-
predicted_style_label = beard_classifier.classify_beard(image="<input as image>",image_type=image_input)
|
| 897 |
-
# Predict beard color
|
| 898 |
-
predicted_color_label = beard_color_classifier.classify_beard_color(image="<input as image>",image_type=image_input)
|
| 899 |
-
# Classify hairstyle
|
| 900 |
-
predicted_hairstyle_label = hair_style_classifier.classify_hair(image="<input as image>",image_type=image_input)
|
| 901 |
-
#classify menHairColor
|
| 902 |
-
predicted_menhaircolor_label = menhair_color_classifier.classify_menHair_color(image="<input as image>",image_type=image_input)
|
| 903 |
-
|
| 904 |
-
if predicted_gender == 'Male':
|
| 905 |
-
background_image_paths = male_background_image_paths
|
| 906 |
-
if predicted_gender == 'Female':
|
| 907 |
-
background_image_paths = female_background_image_paths
|
| 908 |
-
|
| 909 |
-
else:
|
| 910 |
-
print("Predictions started")
|
| 911 |
-
# time counting
|
| 912 |
-
gp_start = time.time()
|
| 913 |
-
gender_predictions = map(predict_gender_from_frames, frames)
|
| 914 |
-
gender_counter = Counter(gender_predictions)
|
| 915 |
-
predicted_gender = gender_counter.most_common(1)[0][0]
|
| 916 |
-
# time counting
|
| 917 |
-
gp_end = time.time()
|
| 918 |
-
print(f'Predicted Gender: {predicted_gender} and it took {round(gp_end - gp_start)}s')
|
| 919 |
|
| 920 |
-
if predicted_gender == 'Male':
|
| 921 |
-
# time counting
|
| 922 |
-
mp_start = time.time()
|
| 923 |
-
background_image_paths = male_background_image_paths
|
| 924 |
-
|
| 925 |
-
facial_feature_predictions = map(predict_male_features_from_frames, frames)
|
| 926 |
-
beard_style_counter = Counter()
|
| 927 |
-
beard_color_counter = Counter()
|
| 928 |
-
hair_style_label_counter = Counter()
|
| 929 |
-
menhair_color_counter = Counter()
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
for pred in facial_feature_predictions:
|
| 933 |
-
beard_style_counter[pred[0]] += 1
|
| 934 |
-
beard_color_counter[pred[1]] += 1
|
| 935 |
-
hair_style_label_counter[pred[2]] += 1
|
| 936 |
-
menhair_color_counter[pred[3]] += 1
|
| 937 |
-
|
| 938 |
-
predicted_style_label = beard_style_counter.most_common(1)[0][0]
|
| 939 |
-
predicted_color_label = beard_color_counter.most_common(1)[0][0]
|
| 940 |
-
predicted_hairstyle_label = hair_style_label_counter.most_common(1)[0][0]
|
| 941 |
-
predicted_menhaircolor_label = menhair_color_counter.most_common(1)[0][0]
|
| 942 |
-
# time counting
|
| 943 |
-
mp_end = time.time()
|
| 944 |
-
|
| 945 |
-
|
| 946 |
-
print("Predictions are:\n")
|
| 947 |
-
print(predicted_style_label,predicted_color_label,predicted_hairstyle_label,predicted_menhaircolor_label)
|
| 948 |
-
print(f'\nand it took {round(mp_end - mp_start)}s')
|
| 949 |
|
| 950 |
-
if predicted_gender == 'Female':
|
| 951 |
-
background_image_paths = female_background_image_paths
|
| 952 |
-
women_hairstyle_counter = women_haircolor_counter = Counter()
|
| 953 |
-
facial_feature_predictions = map(predict_female_features_from_frames, frames)
|
| 954 |
-
|
| 955 |
-
for pred in facial_feature_predictions:
|
| 956 |
-
women_hairstyle_counter[pred[0]] += 1
|
| 957 |
-
women_haircolor_counter[pred[1]] += 1
|
| 958 |
-
|
| 959 |
-
predicted_women_hairstyle = women_hairstyle_counter.most_common(1)[0][0]
|
| 960 |
-
predicted_women_haircolor = women_haircolor_counter.most_common(1)[0][0]
|
| 961 |
-
|
| 962 |
# Process background images and apply beard style and color along with hair style and color
|
| 963 |
final_images = []
|
| 964 |
|
| 965 |
-
|
| 966 |
-
|
| 967 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 968 |
x_coordinate = 90
|
| 969 |
y_coordinate = 50
|
| 970 |
add_eyebrow(background_image, 115, 80, "Data/AdobeColorFunko/EyezBrowz/Eyebrow.png")
|
|
@@ -976,7 +867,21 @@ def Igenerate_funko_figurines(image_input):
|
|
| 976 |
x_coordinate = 90
|
| 977 |
y_coordinate = 50
|
| 978 |
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 979 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 980 |
if predicted_style_label == 'Bandholz':
|
| 981 |
process_image_Beard(background_image, 320,
|
| 982 |
f"Data/AdobeColorFunko/Beard/Bandholz/{predicted_color_label}.png",
|
|
@@ -1010,24 +915,24 @@ def Igenerate_funko_figurines(image_input):
|
|
| 1010 |
# Add other conditions for different beard styles
|
| 1011 |
|
| 1012 |
# Overlay hairstyle
|
| 1013 |
-
if
|
| 1014 |
process_image_menHair(background_image, 336, 420,
|
| 1015 |
-
f"Data/AdobeColorFunko/MenHairstyle/Afro/{
|
| 1016 |
41, 76)
|
| 1017 |
|
| 1018 |
-
if
|
| 1019 |
process_image_menHair(background_image, 305, 420,
|
| 1020 |
-
f"Data/AdobeColorFunko/MenHairstyle/Puff/{
|
| 1021 |
56, 68)
|
| 1022 |
|
| 1023 |
-
if
|
| 1024 |
process_image_menHair(background_image, 310, 420,
|
| 1025 |
-
f"Data/AdobeColorFunko/MenHairstyle/Spike/{
|
| 1026 |
52, 70)
|
| 1027 |
|
| 1028 |
-
if
|
| 1029 |
process_image_menHair(background_image, 310, 420,
|
| 1030 |
-
f"Data/AdobeColorFunko/MenHairstyle/Bald/{
|
| 1031 |
67, 120)
|
| 1032 |
|
| 1033 |
|
|
@@ -1038,19 +943,24 @@ def Igenerate_funko_figurines(image_input):
|
|
| 1038 |
x_coordinate = 90
|
| 1039 |
y_coordinate = 50
|
| 1040 |
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
| 1041 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1042 |
process_image_WomanHair(background_image, 300,460,
|
| 1043 |
-
f"Data/AdobeColorFunko/WomenHairstyle/MediumLength/{
|
| 1044 |
56, 50)
|
| 1045 |
|
| 1046 |
-
if
|
| 1047 |
process_image_WomanHair(background_image, 270,460,
|
| 1048 |
-
f"Data/AdobeColorFunko/WomenHairstyle/ShortHair/{
|
| 1049 |
61, 49)
|
| 1050 |
|
| 1051 |
-
if
|
| 1052 |
process_image_WomanHair(background_image, 300,450,
|
| 1053 |
-
f"Data/AdobeColorFunko/WomenHairstyle/SidePlait/{
|
| 1054 |
54, 56)
|
| 1055 |
|
| 1056 |
|
|
@@ -1064,7 +974,6 @@ def Igenerate_funko_figurines(image_input):
|
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
-
|
| 1068 |
with gr.Blocks() as demo:
|
| 1069 |
gr.Markdown(
|
| 1070 |
"""
|
|
@@ -1079,10 +988,5 @@ with gr.Blocks() as demo:
|
|
| 1079 |
RecordButton = gr.Button(value="Generate My Custom Funko POP")
|
| 1080 |
RecordButton.click(Igenerate_funko_figurines, outputs=MyOutputs)
|
| 1081 |
|
| 1082 |
-
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
|
| 1087 |
if __name__ == "__main__":
|
| 1088 |
demo.launch()
|
|
|
|
| 828 |
|
| 829 |
return final_images
|
| 830 |
|
| 831 |
+
def Igenerate_funko_figurines():
|
| 832 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 833 |
# Capture video from the webcam for 7 seconds
|
| 834 |
+
captured_duration = 5 # Duration in seconds
|
| 835 |
|
| 836 |
+
# Initialize variables to store frames and track time
|
| 837 |
+
#frames = []
|
| 838 |
+
#start_time = time.time()
|
| 839 |
+
frames = capture_frame_from_webcam(duration=5)
|
| 840 |
+
# Continuously capture frames for the specified duration
|
| 841 |
+
#while (time.time() - start_time) < captured_duration:
|
| 842 |
+
# frames.extend(capture_frame_from_webcam())
|
| 843 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 844 |
# Detect and classify gender
|
| 845 |
+
gender_classifier = GenderClassifier('Data/FunkoSavedModels/Gender.pt', ['Female', 'Male'])
|
| 846 |
+
predicted_gender = gender_classifier.classify_from_frames(frames)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 847 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 848 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 849 |
# Process background images and apply beard style and color along with hair style and color
|
| 850 |
final_images = []
|
| 851 |
|
| 852 |
+
if predicted_gender == 'Male':
|
| 853 |
+
background_image_paths = male_background_image_paths
|
| 854 |
+
if predicted_gender == 'Female':
|
| 855 |
+
background_image_paths = female_background_image_paths
|
| 856 |
+
|
| 857 |
+
for background_image_paths in background_image_paths:
|
| 858 |
+
background_image = Image.open(background_image_paths)
|
| 859 |
x_coordinate = 90
|
| 860 |
y_coordinate = 50
|
| 861 |
add_eyebrow(background_image, 115, 80, "Data/AdobeColorFunko/EyezBrowz/Eyebrow.png")
|
|
|
|
| 867 |
x_coordinate = 90
|
| 868 |
y_coordinate = 50
|
| 869 |
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
| 870 |
+
# Detect and classify beard style
|
| 871 |
+
beard_classifier = BeardClassifier('Data/FunkoSavedModels/FunkoResnet18BeardStyle.pt', ['Bandholz', 'CleanShave', 'FullGoatee', 'Moustache', 'RapIndustryStandards', 'ShortBeard'])
|
| 872 |
+
predicted_style_label = beard_classifier.classify_from_frames(frames)
|
| 873 |
+
|
| 874 |
+
# Detect and classify beard color
|
| 875 |
+
beard_color_classifier = BeardColorClassifier('Data/FunkoSavedModels/FunkoResnet18BeardColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
|
| 876 |
+
predicted_color_label = beard_color_classifier.classify_from_frames(frames)
|
| 877 |
|
| 878 |
+
# Classify hairstyle
|
| 879 |
+
hair_style_classifier = HairStyleClassifier('Data/FunkoSavedModels/FunkoResnet18HairStyle.pt', ['Afro', 'Bald', 'Puff', 'Spike'])
|
| 880 |
+
predicted_hairStyle_label = hair_style_classifier.classify_from_frames(frames)
|
| 881 |
+
|
| 882 |
+
#classify menHairColor
|
| 883 |
+
menhair_color_classifier = MenHairColorClassifier('Data/FunkoSavedModels/FunkoResnet18MenHairColor.pt', ['Black', 'DarkBrown', 'Ginger', 'LightBrown', 'SaltAndPepper', 'White'])
|
| 884 |
+
predicted_menhairColor_label = menhair_color_classifier.classify_from_frames(frames)
|
| 885 |
if predicted_style_label == 'Bandholz':
|
| 886 |
process_image_Beard(background_image, 320,
|
| 887 |
f"Data/AdobeColorFunko/Beard/Bandholz/{predicted_color_label}.png",
|
|
|
|
| 915 |
# Add other conditions for different beard styles
|
| 916 |
|
| 917 |
# Overlay hairstyle
|
| 918 |
+
if predicted_hairStyle_label == 'Afro':
|
| 919 |
process_image_menHair(background_image, 336, 420,
|
| 920 |
+
f"Data/AdobeColorFunko/MenHairstyle/Afro/{predicted_menhairColor_label}.png",
|
| 921 |
41, 76)
|
| 922 |
|
| 923 |
+
if predicted_hairStyle_label == 'Puff':
|
| 924 |
process_image_menHair(background_image, 305, 420,
|
| 925 |
+
f"Data/AdobeColorFunko/MenHairstyle/Puff/{predicted_menhairColor_label}.png",
|
| 926 |
56, 68)
|
| 927 |
|
| 928 |
+
if predicted_hairStyle_label == 'Spike':
|
| 929 |
process_image_menHair(background_image, 310, 420,
|
| 930 |
+
f"Data/AdobeColorFunko/MenHairstyle/Spike/{predicted_menhairColor_label}.png",
|
| 931 |
52, 70)
|
| 932 |
|
| 933 |
+
if predicted_hairStyle_label == 'Bald':
|
| 934 |
process_image_menHair(background_image, 310, 420,
|
| 935 |
+
f"Data/AdobeColorFunko/MenHairstyle/Bald/{predicted_menhairColor_label}.png",
|
| 936 |
67, 120)
|
| 937 |
|
| 938 |
|
|
|
|
| 943 |
x_coordinate = 90
|
| 944 |
y_coordinate = 50
|
| 945 |
dummy_eye(background_image, x, y, placeholder_image_path, x_coordinate, y_coordinate)
|
| 946 |
+
WomenHairStyle_classifier = WomenHairStyleClassifier('Data/FunkoSavedModels/WomenHairStyle.pt', ['MediumLength', 'ShortHair', 'SidePlait'])
|
| 947 |
+
predicted_WomenHairStyle = WomenHairStyle_classifier.classify_from_frames(frames)
|
| 948 |
+
|
| 949 |
+
WomenHairColor_classifier = WomenHairColorClassifier('Data/FunkoSavedModels/WomenHairColor.pt', ['Black', 'Brown', 'Ginger', 'White'])
|
| 950 |
+
predicted_WomenHairColor = WomenHairColor_classifier.classify_from_frames(frames)
|
| 951 |
+
if predicted_WomenHairStyle == 'MediumLength':
|
| 952 |
process_image_WomanHair(background_image, 300,460,
|
| 953 |
+
f"Data/AdobeColorFunko/WomenHairstyle/MediumLength/{predicted_WomenHairColor}.png",
|
| 954 |
56, 50)
|
| 955 |
|
| 956 |
+
if predicted_WomenHairStyle == 'ShortHair':
|
| 957 |
process_image_WomanHair(background_image, 270,460,
|
| 958 |
+
f"Data/AdobeColorFunko/WomenHairstyle/ShortHair/{predicted_WomenHairColor}.png",
|
| 959 |
61, 49)
|
| 960 |
|
| 961 |
+
if predicted_WomenHairStyle == 'SidePlait':
|
| 962 |
process_image_WomanHair(background_image, 300,450,
|
| 963 |
+
f"Data/AdobeColorFunko/WomenHairstyle/SidePlait/{predicted_WomenHairColor}.png",
|
| 964 |
54, 56)
|
| 965 |
|
| 966 |
|
|
|
|
| 974 |
|
| 975 |
|
| 976 |
|
|
|
|
| 977 |
with gr.Blocks() as demo:
|
| 978 |
gr.Markdown(
|
| 979 |
"""
|
|
|
|
| 988 |
RecordButton = gr.Button(value="Generate My Custom Funko POP")
|
| 989 |
RecordButton.click(Igenerate_funko_figurines, outputs=MyOutputs)
|
| 990 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 991 |
if __name__ == "__main__":
|
| 992 |
demo.launch()
|